copay/js/models/core/PluginManager.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-09-01 12:31:35 -07:00
'use strict';
var preconditions = require('preconditions').singleton();
var log = require('../../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-09-01 12:31:35 -07:00
var pluginClass = require('../plugins/' + pluginName);
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 = {};
PluginManager.TYPE['STORAGE'] = KIND_UNIQUE;
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-20 03:36:05 -07:00
preconditions.checkArgument(kind, 'Plugin has unknown type' + name);
2014-09-01 19:44:35 -07:00
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;