Commit 1bf8b80c authored by JC Brand's avatar JC Brand

Refactored converse-pluggable to remove all deferreds

by not attempting to load `optional_dependencies` via require.js.

Instead, we just expect them to be plugins and to have been loaded already.
parent 454f8ef0
...@@ -633,6 +633,11 @@ ...@@ -633,6 +633,11 @@
// XXX: Deprecate in favor of init_deferred // XXX: Deprecate in favor of init_deferred
this.callback(); this.callback();
} }
if (converse.connection.service === 'jasmine tests') {
init_deferred.resolve(converse);
} else {
init_deferred.resolve();
}
converse.emit('initialized'); converse.emit('initialized');
}; };
...@@ -1780,15 +1785,9 @@ ...@@ -1780,15 +1785,9 @@
converse.pluggable.initializePlugins({ converse.pluggable.initializePlugins({
'updateSettings': updateSettings, 'updateSettings': updateSettings,
'converse': converse 'converse': converse
}).then(function () {
converse._initialize();
converse.registerGlobalEventHandlers();
if (converse.connection.service === 'jasmine tests') {
init_deferred.resolve(converse);
} else {
init_deferred.resolve();
}
}); });
converse._initialize();
converse.registerGlobalEventHandlers();
return init_deferred.promise(); return init_deferred.promise();
}; };
return converse; return converse;
......
...@@ -43,14 +43,13 @@ ...@@ -43,14 +43,13 @@
Strophe.addNamespace('MUC_USER', Strophe.NS.MUC + "#user"); Strophe.addNamespace('MUC_USER', Strophe.NS.MUC + "#user");
converse_api.plugins.add('converse-muc', { converse_api.plugins.add('converse-muc', {
/* Optional dependencies are require.js dependencies which might be /* Optional dependencies are other plugins which might be
* overridden or relied upon if they exist, but otherwise ignored. * overridden or relied upon, if they exist, otherwise they're ignored.
* *
* However, if the setting "strict_plugin_dependencies" is set to true, * However, if the setting "strict_plugin_dependencies" is set to true,
* then these dependencies will be considered required. * an error will be raised if the plugin is not found.
* *
* Optional dependencies will be available in the initialize method as * NB: These plugins need to have already been loaded via require.js.
* a the "optional_dependencies" attribute of the plugin.
*/ */
optional_dependencies: ["converse-controlbox"], optional_dependencies: ["converse-controlbox"],
......
...@@ -84,33 +84,23 @@ ...@@ -84,33 +84,23 @@
}.bind(this)); }.bind(this));
}, },
setOptionalDependencies: function (plugin, dependencies) { loadOptionalDependencies: function (plugin) {
plugin.optional_dependencies = dependencies; _.each(plugin.optional_dependencies, function (name) {
return plugin; var dep = this.plugins[name];
}, if (dep) {
if (_.contains(dep.optional_dependencies, plugin.__name__)) {
loadOptionalDependencies: function (plugins) { // FIXME: circular dependency checking is only one level deep.
var deferred = new $.Deferred(); throw "Found a circular dependency between the plugins \""+
require(plugins, plugin.__name__+"\" and \""+name+"\"";
function () {
_.each(plugins, function (name) {
var plugin = this.plugins[name];
if (plugin) {
this.initializePlugin(plugin).then(
deferred.resolve.bind(this, plugins)
);
}
}.bind(this));
}.bind(this),
function () {
if (this.plugged.strict_plugin_dependencies) {
deferred.fail.apply(this, arguments);
this.throwUndefinedDependencyError(arguments[0]);
} else {
deferred.resolve.apply(this, [plugins]);
} }
}.bind(this)); this.initializePlugin(dep);
return deferred.promise(); } else {
this.throwUndefinedDependencyError(
"Could not find optional dependency \""+name+"\" "+
"for the plugin \""+plugin.__name__+"\". "+
"If it's needed, make sure it's loaded by require.js");
}
}.bind(this));
}, },
throwUndefinedDependencyError: function (msg) { throwUndefinedDependencyError: function (msg) {
...@@ -140,63 +130,32 @@ ...@@ -140,63 +130,32 @@
}.bind(this)); }.bind(this));
}, },
_initializePlugin: function (plugin) {
this.applyOverrides(plugin);
if (typeof plugin.initialize === "function") {
plugin.initialize.bind(plugin)(this);
}
this.initialized_plugins.push(plugin.__name__);
},
asyncInitializePlugin: function (plugin) {
var deferred = new $.Deferred();
this.loadOptionalDependencies(plugin.optional_dependencies).then(
_.compose(
deferred.resolve,
this._initializePlugin.bind(this),
_.partial(this.setOptionalDependencies, plugin)
));
return deferred.promise();
},
initializePlugin: function (plugin) { initializePlugin: function (plugin) {
var deferred = new $.Deferred();
if (_.contains(this.initialized_plugins, plugin.__name__)) { if (_.contains(this.initialized_plugins, plugin.__name__)) {
// Don't initialize plugins twice, otherwise we get // Don't initialize plugins twice, otherwise we get
// infinite recursion in overridden methods. // infinite recursion in overridden methods.
return deferred.resolve().promise(); return;
} }
_.extend(plugin, this.properties); _.extend(plugin, this.properties);
if (plugin.optional_dependencies) { if (plugin.optional_dependencies) {
this.asyncInitializePlugin(plugin).then(deferred.resolve); this.loadOptionalDependencies(plugin);
} else {
this._initializePlugin(plugin);
deferred.resolve();
} }
return deferred.promise(); this.applyOverrides(plugin);
}, if (typeof plugin.initialize === "function") {
plugin.initialize.bind(plugin)(this);
initNextPlugin: function (remaining, deferred) {
if (remaining.length === 0) {
deferred.resolve();
return;
} }
var plugin = remaining.pop(); this.initialized_plugins.push(plugin.__name__);
this.initializePlugin(plugin).then(
this.initNextPlugin.bind(this, remaining, deferred));
}, },
initializePlugins: function (properties) { initializePlugins: function (properties) {
/* The properties variable is an object of attributes and methods /* The properties variable is an object of attributes and methods
* which will be attached to the plugins. * which will be attached to the plugins.
*/ */
var deferred = new $.Deferred();
if (!_.size(this.plugins)) { if (!_.size(this.plugins)) {
return deferred.promise(); return;
} }
this.properties = properties; this.properties = properties;
this.initNextPlugin(_.values(this.plugins).reverse(), deferred); _.each(_.values(this.plugins), this.initializePlugin.bind(this));
return deferred;
} }
}); });
return { return {
......
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