Commit 27095855 authored by Romain Courteaud's avatar Romain Courteaud

Speed up private gadget creation.

No need to execute async code if the gadget class is already loaded.
parent ca47096f
......@@ -827,14 +827,7 @@
/////////////////////////////////////////////////////////////////
// privateDeclarePublicGadget
/////////////////////////////////////////////////////////////////
function privateDeclarePublicGadget(url, options, parent_gadget) {
return renderJS.declareGadgetKlass(url)
// gadget loading should not be interrupted
// if not, gadget's definition will not be complete
//.then will return another promise
//so loading_klass_promise can't be cancel
.then(function createPrivateInstanceFromKlass(Klass) {
function createPrivateInstanceFromKlass(Klass, options, parent_gadget) {
// Get the gadget class and instanciate it
if (options.element === undefined) {
options.element = document.createElement("div");
......@@ -854,8 +847,21 @@
gadget_instance.element.appendChild(fragment);
setAqParent(gadget_instance, parent_gadget);
return gadget_instance;
}
function privateDeclarePublicGadget(url, options, parent_gadget) {
var klass = renderJS.declareGadgetKlass(url);
// gadget loading should not be interrupted
// if not, gadget's definition will not be complete
//.then will return another promise
//so loading_klass_promise can't be cancel
if (typeof klass.then === 'function') {
return klass.then(function createAsyncPrivateInstanceFromKlass(Klass) {
return createPrivateInstanceFromKlass(Klass, options, parent_gadget);
});
}
return createPrivateInstanceFromKlass(klass, options, parent_gadget);
}
/////////////////////////////////////////////////////////////////
// RenderJSIframeGadget
......@@ -1016,37 +1022,8 @@
/////////////////////////////////////////////////////////////////
// RenderJSGadget.declareGadget
/////////////////////////////////////////////////////////////////
RenderJSGadget
.declareMethod('declareGadget', function declareGadget(url, options) {
var parent_gadget = this;
if (options === undefined) {
options = {};
}
if (options.sandbox === undefined) {
options.sandbox = "public";
}
// transform url to absolute url if it is relative
url = renderJS.getAbsoluteURL(url, this.__path);
return new RSVP.Queue()
.push(function waitForPrivateDeclareGadget() {
var method;
if (options.sandbox === "public") {
method = privateDeclarePublicGadget;
} else if (options.sandbox === "iframe") {
method = privateDeclareIframeGadget;
} else if (options.sandbox === "dataurl") {
method = privateDeclareDataUrlGadget;
} else {
throw new Error("Unsupported sandbox options '" +
options.sandbox + "'");
}
return method(url, options, parent_gadget);
})
// Set the HTML context
.push(function setGadgetInstanceHTMLContext(gadget_instance) {
function setGadgetInstanceHTMLContext(gadget_instance, options,
parent_gadget, url) {
var i,
scope,
queue;
......@@ -1100,7 +1077,48 @@
return queue;
}
return ready_wrapper();
}
RenderJSGadget
.declareMethod('declareGadget', function declareGadget(url, options) {
var parent_gadget = this,
method,
result;
if (options === undefined) {
options = {};
}
if (options.sandbox === undefined) {
options.sandbox = "public";
}
// transform url to absolute url if it is relative
url = renderJS.getAbsoluteURL(url, this.__path);
if (options.sandbox === "public") {
method = privateDeclarePublicGadget;
} else if (options.sandbox === "iframe") {
method = privateDeclareIframeGadget;
} else if (options.sandbox === "dataurl") {
method = privateDeclareDataUrlGadget;
} else {
throw new Error("Unsupported sandbox options '" +
options.sandbox + "'");
}
result = method(url, options, parent_gadget);
// Set the HTML context
if (typeof result.then === 'function') {
return new RSVP.Queue()
.push(function () {
return result;
})
.push(function setAsyncGadgetInstanceHTMLContext(gadget_instance) {
return setGadgetInstanceHTMLContext(gadget_instance, options,
parent_gadget, url);
});
}
return setGadgetInstanceHTMLContext(result, options,
parent_gadget, url);
})
.declareMethod('getDeclaredGadget',
function getDeclaredGadget(gadget_scope) {
......@@ -1300,9 +1318,9 @@
return defer.promise;
}
if (gadget_model_defer_dict[url].is_resolved) {
return RSVP.resolve(gadget_model_defer_dict[url].result);
return gadget_model_defer_dict[url].result;
}
return RSVP.reject(gadget_model_defer_dict[url].result);
throw gadget_model_defer_dict[url].result;
}
gadget_model_defer_dict[url] = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment