Wallet/js/models/PluginManager.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2014-09-01 16:31:35 -03:00
'use strict';
var preconditions = require('preconditions').singleton();
2014-09-23 15:24:57 -03:00
var log = require('../log');
2014-09-01 16:31:35 -03:00
function PluginManager(config) {
this.registered = {};
2014-09-03 01:25:08 -03:00
this.scripts = [];
2014-09-01 16:31:35 -03:00
2014-09-03 01:25:08 -03:00
for (var ii in config.plugins) {
2014-09-01 16:31:35 -03:00
var pluginName = ii;
if (!config.plugins[pluginName])
continue;
log.info('Loading plugin: ' + pluginName);
2014-10-20 12:55:18 -03:00
var pluginClass;
if(config.pluginsPath){
pluginClass = require(config.pluginsPath + pluginName);
} else {
pluginClass = require('../plugins/' + pluginName);
}
2014-09-03 01:25:08 -03:00
var pluginObj = new pluginClass(config[pluginName]);
2014-09-01 16:31:35 -03:00
pluginObj.init();
2014-09-01 23:44:35 -03:00
this._register(pluginObj, pluginName);
2014-09-01 16:31:35 -03:00
}
};
var KIND_UNIQUE = PluginManager.KIND_UNIQUE = 1;
var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2;
PluginManager.TYPE = {};
2014-09-27 18:00:27 -03:00
PluginManager.TYPE['DB'] = KIND_UNIQUE;
2014-09-01 16:31:35 -03:00
2014-09-01 23:44:35 -03:00
PluginManager.prototype._register = function(obj, name) {
2014-09-03 01:25:08 -03:00
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);
2014-09-01 16:31:35 -03:00
var type = obj.type;
var kind = PluginManager.TYPE[type];
2014-09-03 01:25:08 -03:00
2014-09-30 15:28:02 -03: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 16:31:35 -03:00
if (kind === PluginManager.KIND_UNIQUE) {
this.registered[type] = obj;
} else {
this.registered[type] = this.registered[type] || [];
this.registered[type].push(obj);
}
2014-09-03 01:25:08 -03:00
this.scripts = this.scripts.concat(obj.scripts || []);
2014-09-01 16:31:35 -03:00
};
2014-09-03 01:25:08 -03:00
2014-09-01 23:44:35 -03:00
PluginManager.prototype.get = function(type) {
return this.registered[type];
};
2014-09-01 16:31:35 -03:00
module.exports = PluginManager;