@ima/core
Version:
IMA.js framework for isomorphic javascript application
87 lines (86 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
PluginLoader: function() {
return PluginLoader;
},
pluginLoader: function() {
return pluginLoader;
}
});
const _Namespace = require("./Namespace");
class PluginLoader {
_plugins;
_bootstrap;
/**
* Initializes the plugin loader.
*
* This is private constructor and should not be used outside of this file.
* You should use the exported instance to register ima.js plugins.
*
* @example
* import { pluginLoader } from '@ima/core';
*
* @private
*/ constructor(){
this._plugins = {};
}
/**
* Initializes the plugin loader with bootstrap instance. Which is later used
* to handle dynamically loaded IMA.js plugins.
*
* @param bootstrap App bootstrap instance.
*/ init(bootstrap) {
this._bootstrap = bootstrap;
}
/**
* Registers plugin into IMA.js bootstrap sequence.
*
* @example
* pluginLoader.register('@ima/plugin-logger', ns => {
* ns.set('ima.plugin.logger', logger);
*
* return {
* initSettings,
* initServices,
* initBind,
* };
* });
*
* @param {string} name Plugin name.
* @param {function} registerFn Plugin initialization function.
*/ register(name, registerFn) {
if (typeof name !== 'string') {
throw new Error(`ima.core.pluginLoader:register moduleName is not a string, '${typeof name}' was given.`);
}
if (typeof registerFn !== 'function') {
throw new Error(`ima.core.pluginLoader:register registerFn is not a function, '${typeof registerFn}' was given.`);
}
const plugin = registerFn(_Namespace.ns);
// Bootstrap plugin if imported dynamically (only if it's not already loaded)
if (this._bootstrap && !this._plugins[name]) {
this._bootstrap.initPlugin(name, plugin ?? undefined);
}
this._plugins[name] = {
name,
plugin: plugin || {}
};
}
/**
* Returns array of registered IMA.js plugins.
*
* @returns {Array} Array of IMA.js plugins.
*/ getPlugins() {
return Object.values(this._plugins);
}
}
const pluginLoader = new PluginLoader();
//# sourceMappingURL=pluginLoader.js.map