Wallet/js/models/core/PluginManager.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-09-01 16:31:35 -03:00
'use strict';
var preconditions = require('preconditions').singleton();
function PluginManager(config) {
this.registered = {};
for(var ii in config.plugins){
var pluginName = ii;
if (!config.plugins[pluginName])
continue;
2014-09-01 23:44:35 -03:00
console.log('Loading plugin: ' + pluginName);
2014-09-01 16:31:35 -03:00
var pluginClass = require('../plugins/' + pluginName);
var pluginObj = new pluginClass();
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 = {};
PluginManager.TYPE['STORAGE'] = KIND_UNIQUE;
2014-09-01 23:44:35 -03:00
PluginManager.prototype._register = function(obj, name) {
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-01 23:44:35 -03:00
preconditions.checkArgument(kind, 'Plugin has unkown 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-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;