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.
This commit is contained in:
parent
454f8ef034
commit
1bf8b80cec
@ -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 @@
|
|||||||
converse.pluggable.initializePlugins({
|
converse.pluggable.initializePlugins({
|
||||||
'updateSettings': updateSettings,
|
'updateSettings': updateSettings,
|
||||||
'converse': converse
|
'converse': converse
|
||||||
}).then(function () {
|
});
|
||||||
converse._initialize();
|
converse._initialize();
|
||||||
converse.registerGlobalEventHandlers();
|
converse.registerGlobalEventHandlers();
|
||||||
if (converse.connection.service === 'jasmine tests') {
|
|
||||||
init_deferred.resolve(converse);
|
|
||||||
} else {
|
|
||||||
init_deferred.resolve();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return init_deferred.promise();
|
return init_deferred.promise();
|
||||||
};
|
};
|
||||||
return converse;
|
return converse;
|
||||||
|
@ -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 @@
|
|||||||
}.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));
|
this.initializePlugin(dep);
|
||||||
}.bind(this),
|
|
||||||
function () {
|
|
||||||
if (this.plugged.strict_plugin_dependencies) {
|
|
||||||
deferred.fail.apply(this, arguments);
|
|
||||||
this.throwUndefinedDependencyError(arguments[0]);
|
|
||||||
} else {
|
} else {
|
||||||
deferred.resolve.apply(this, [plugins]);
|
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));
|
}.bind(this));
|
||||||
return deferred.promise();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
throwUndefinedDependencyError: function (msg) {
|
throwUndefinedDependencyError: function (msg) {
|
||||||
@ -140,7 +130,16 @@
|
|||||||
}.bind(this));
|
}.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
_initializePlugin: function (plugin) {
|
initializePlugin: function (plugin) {
|
||||||
|
if (_.contains(this.initialized_plugins, plugin.__name__)) {
|
||||||
|
// Don't initialize plugins twice, otherwise we get
|
||||||
|
// infinite recursion in overridden methods.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_.extend(plugin, this.properties);
|
||||||
|
if (plugin.optional_dependencies) {
|
||||||
|
this.loadOptionalDependencies(plugin);
|
||||||
|
}
|
||||||
this.applyOverrides(plugin);
|
this.applyOverrides(plugin);
|
||||||
if (typeof plugin.initialize === "function") {
|
if (typeof plugin.initialize === "function") {
|
||||||
plugin.initialize.bind(plugin)(this);
|
plugin.initialize.bind(plugin)(this);
|
||||||
@ -148,55 +147,15 @@
|
|||||||
this.initialized_plugins.push(plugin.__name__);
|
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) {
|
|
||||||
var deferred = new $.Deferred();
|
|
||||||
if (_.contains(this.initialized_plugins, plugin.__name__)) {
|
|
||||||
// Don't initialize plugins twice, otherwise we get
|
|
||||||
// infinite recursion in overridden methods.
|
|
||||||
return deferred.resolve().promise();
|
|
||||||
}
|
|
||||||
_.extend(plugin, this.properties);
|
|
||||||
if (plugin.optional_dependencies) {
|
|
||||||
this.asyncInitializePlugin(plugin).then(deferred.resolve);
|
|
||||||
} else {
|
|
||||||
this._initializePlugin(plugin);
|
|
||||||
deferred.resolve();
|
|
||||||
}
|
|
||||||
return deferred.promise();
|
|
||||||
},
|
|
||||||
|
|
||||||
initNextPlugin: function (remaining, deferred) {
|
|
||||||
if (remaining.length === 0) {
|
|
||||||
deferred.resolve();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var plugin = remaining.pop();
|
|
||||||
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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user