copay/js/models/PluginManager.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2014-09-01 12:31:35 -07:00
'use strict';
var preconditions = require('preconditions').singleton();
2014-12-02 06:17:03 -08:00
var log = require('../util/log');
2014-09-01 12:31:35 -07:00
function PluginManager(config) {
this.registered = {};
2014-09-02 21:25:08 -07:00
this.scripts = [];
2014-09-01 12:31:35 -07:00
2014-09-02 21:25:08 -07:00
for (var ii in config.plugins) {
2014-09-01 12:31:35 -07:00
var pluginName = ii;
if (!config.plugins[pluginName])
continue;
log.info('Loading plugin: ' + pluginName);
2014-10-20 08:55:18 -07:00
var pluginClass;
if(config.pluginsPath){
pluginClass = require(config.pluginsPath + pluginName);
} else {
2014-12-03 11:30:43 -08:00
pluginClass = require('../js/plugins/' + pluginName);
2014-10-20 08:55:18 -07:00
}
2014-09-02 21:25:08 -07:00
var pluginObj = new pluginClass(config[pluginName]);
2014-09-01 12:31:35 -07:00
pluginObj.init();
2014-09-01 19:44:35 -07:00
this._register(pluginObj, pluginName);
2014-09-01 12:31:35 -07:00
}
};
var KIND_UNIQUE = PluginManager.KIND_UNIQUE = 1;
var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2;
PluginManager.TYPE = {};
2014-09-27 14:00:27 -07:00
PluginManager.TYPE['DB'] = KIND_UNIQUE;
2014-09-01 12:31:35 -07:00
2014-09-01 19:44:35 -07:00
PluginManager.prototype._register = function(obj, name) {
2014-09-02 21:25:08 -07:00
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);
2014-09-01 12:31:35 -07:00
var type = obj.type;
var kind = PluginManager.TYPE[type];
2014-09-02 21:25:08 -07:00
2014-09-30 11:28:02 -07:00
preconditions.checkArgument(kind, 'Unknown plugin type:' + name);
preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered:' + name);
2014-09-01 12:31:35 -07:00
if (kind === PluginManager.KIND_UNIQUE) {
this.registered[type] = obj;
} else {
this.registered[type] = this.registered[type] || [];
this.registered[type].push(obj);
}
2014-09-02 21:25:08 -07:00
this.scripts = this.scripts.concat(obj.scripts || []);
2014-09-01 12:31:35 -07:00
};
2014-09-02 21:25:08 -07:00
2014-09-01 19:44:35 -07:00
PluginManager.prototype.get = function(type) {
return this.registered[type];
};
2014-09-01 12:31:35 -07:00
module.exports = PluginManager;