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,34 +827,40 @@ ...@@ -827,34 +827,40 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// privateDeclarePublicGadget // privateDeclarePublicGadget
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
function privateDeclarePublicGadget(url, options, parent_gadget) { function createPrivateInstanceFromKlass(Klass, options, parent_gadget) {
// Get the gadget class and instanciate it
if (options.element === undefined) {
options.element = document.createElement("div");
}
var i,
gadget_instance,
template_node_list = Klass.__template_element.body.childNodes,
fragment = document.createDocumentFragment();
gadget_instance = new Klass();
gadget_instance.element = options.element;
gadget_instance.state = {};
for (i = 0; i < template_node_list.length; i += 1) {
fragment.appendChild(
template_node_list[i].cloneNode(true)
);
}
gadget_instance.element.appendChild(fragment);
setAqParent(gadget_instance, parent_gadget);
return gadget_instance;
}
return renderJS.declareGadgetKlass(url) function privateDeclarePublicGadget(url, options, parent_gadget) {
// gadget loading should not be interrupted var klass = renderJS.declareGadgetKlass(url);
// if not, gadget's definition will not be complete // gadget loading should not be interrupted
//.then will return another promise // if not, gadget's definition will not be complete
//so loading_klass_promise can't be cancel //.then will return another promise
.then(function createPrivateInstanceFromKlass(Klass) { //so loading_klass_promise can't be cancel
// Get the gadget class and instanciate it if (typeof klass.then === 'function') {
if (options.element === undefined) { return klass.then(function createAsyncPrivateInstanceFromKlass(Klass) {
options.element = document.createElement("div"); return createPrivateInstanceFromKlass(Klass, options, parent_gadget);
}
var i,
gadget_instance,
template_node_list = Klass.__template_element.body.childNodes,
fragment = document.createDocumentFragment();
gadget_instance = new Klass();
gadget_instance.element = options.element;
gadget_instance.state = {};
for (i = 0; i < template_node_list.length; i += 1) {
fragment.appendChild(
template_node_list[i].cloneNode(true)
);
}
gadget_instance.element.appendChild(fragment);
setAqParent(gadget_instance, parent_gadget);
return gadget_instance;
}); });
}
return createPrivateInstanceFromKlass(klass, options, parent_gadget);
} }
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -1016,9 +1022,68 @@ ...@@ -1016,9 +1022,68 @@
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// RenderJSGadget.declareGadget // RenderJSGadget.declareGadget
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
function setGadgetInstanceHTMLContext(gadget_instance, options,
parent_gadget, url) {
var i,
scope,
queue;
clearGadgetInternalParameters(gadget_instance);
// Store local reference to the gadget instance
scope = options.scope;
if (scope === undefined) {
scope = 'RJS_' + scope_increment;
scope_increment += 1;
while (parent_gadget.__sub_gadget_dict.hasOwnProperty(scope)) {
scope = 'RJS_' + scope_increment;
scope_increment += 1;
}
}
parent_gadget.__sub_gadget_dict[scope] = gadget_instance;
gadget_instance.element.setAttribute("data-gadget-scope",
scope);
// Put some attribute to ease page layout comprehension
gadget_instance.element.setAttribute("data-gadget-url", url);
gadget_instance.element.setAttribute("data-gadget-sandbox",
options.sandbox);
gadget_instance.element._gadget = gadget_instance;
function ready_executable_wrapper(fct) {
return function executeReadyWrapper() {
return fct.call(gadget_instance, gadget_instance);
};
}
function ready_wrapper() {
if (document.contains(gadget_instance.element)) {
startService(gadget_instance);
}
// Always return the gadget instance after ready function
return gadget_instance;
}
if (gadget_instance.constructor.__ready_list.length) {
queue = new RSVP.Queue();
// Trigger calling of all ready callback
for (i = 0; i < gadget_instance.constructor.__ready_list.length;
i += 1) {
// Put a timeout?
queue.push(ready_executable_wrapper(
gadget_instance.constructor.__ready_list[i]
));
}
queue.push(ready_wrapper);
return queue;
}
return ready_wrapper();
}
RenderJSGadget RenderJSGadget
.declareMethod('declareGadget', function declareGadget(url, options) { .declareMethod('declareGadget', function declareGadget(url, options) {
var parent_gadget = this; var parent_gadget = this,
method,
result;
if (options === undefined) { if (options === undefined) {
options = {}; options = {};
...@@ -1030,77 +1095,30 @@ ...@@ -1030,77 +1095,30 @@
// transform url to absolute url if it is relative // transform url to absolute url if it is relative
url = renderJS.getAbsoluteURL(url, this.__path); url = renderJS.getAbsoluteURL(url, this.__path);
return new RSVP.Queue() if (options.sandbox === "public") {
.push(function waitForPrivateDeclareGadget() { method = privateDeclarePublicGadget;
var method; } else if (options.sandbox === "iframe") {
if (options.sandbox === "public") { method = privateDeclareIframeGadget;
method = privateDeclarePublicGadget; } else if (options.sandbox === "dataurl") {
} else if (options.sandbox === "iframe") { method = privateDeclareDataUrlGadget;
method = privateDeclareIframeGadget; } else {
} else if (options.sandbox === "dataurl") { throw new Error("Unsupported sandbox options '" +
method = privateDeclareDataUrlGadget; options.sandbox + "'");
} else { }
throw new Error("Unsupported sandbox options '" + result = method(url, options, parent_gadget);
options.sandbox + "'"); // Set the HTML context
} if (typeof result.then === 'function') {
return method(url, options, parent_gadget); return new RSVP.Queue()
}) .push(function () {
// Set the HTML context return result;
.push(function setGadgetInstanceHTMLContext(gadget_instance) { })
var i, .push(function setAsyncGadgetInstanceHTMLContext(gadget_instance) {
scope, return setGadgetInstanceHTMLContext(gadget_instance, options,
queue; parent_gadget, url);
clearGadgetInternalParameters(gadget_instance); });
}
// Store local reference to the gadget instance return setGadgetInstanceHTMLContext(result, options,
scope = options.scope; parent_gadget, url);
if (scope === undefined) {
scope = 'RJS_' + scope_increment;
scope_increment += 1;
while (parent_gadget.__sub_gadget_dict.hasOwnProperty(scope)) {
scope = 'RJS_' + scope_increment;
scope_increment += 1;
}
}
parent_gadget.__sub_gadget_dict[scope] = gadget_instance;
gadget_instance.element.setAttribute("data-gadget-scope",
scope);
// Put some attribute to ease page layout comprehension
gadget_instance.element.setAttribute("data-gadget-url", url);
gadget_instance.element.setAttribute("data-gadget-sandbox",
options.sandbox);
gadget_instance.element._gadget = gadget_instance;
function ready_executable_wrapper(fct) {
return function executeReadyWrapper() {
return fct.call(gadget_instance, gadget_instance);
};
}
function ready_wrapper() {
if (document.contains(gadget_instance.element)) {
startService(gadget_instance);
}
// Always return the gadget instance after ready function
return gadget_instance;
}
if (gadget_instance.constructor.__ready_list.length) {
queue = new RSVP.Queue();
// Trigger calling of all ready callback
for (i = 0; i < gadget_instance.constructor.__ready_list.length;
i += 1) {
// Put a timeout?
queue.push(ready_executable_wrapper(
gadget_instance.constructor.__ready_list[i]
));
}
queue.push(ready_wrapper);
return queue;
}
return ready_wrapper();
});
}) })
.declareMethod('getDeclaredGadget', .declareMethod('getDeclaredGadget',
function getDeclaredGadget(gadget_scope) { function getDeclaredGadget(gadget_scope) {
...@@ -1300,9 +1318,9 @@ ...@@ -1300,9 +1318,9 @@
return defer.promise; return defer.promise;
} }
if (gadget_model_defer_dict[url].is_resolved) { 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] = { 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