minimajs基于 NodeJS 的插件化框架
Minima 是由 ES6 开发的基于 NodeJS 的简单而强大的插件框架。
Minima 有三个功能:(1)动态插件:定义插件结构,插件配置,插件依赖,插件生命周期,插件类加载; (2)服务:插件与SOA之间的沟通; (3)扩展:支持插件扩展。
安装:
Install with npm:
$ npm install --save minimajs
使用:
Minima是一个插件框架容器。 我们需要创建一个插件框架实例去启动它。
import { Minima } from 'minimajs'; let minima = new Minima(__dirname + '/plugins'); minima.start();
Examples
在plugins目录中创建一个简单的插件,如下所示。
// 1 plugin.json { "id": "demoPlugin", "startLevel": 3, "version": "1.0.0" } // 2 Activator.js import { ServiceAction, ExtensionAction, PluginContext, Plugin, log } from 'minimajs'; export default class Activator { /** * 插件上下文缓存 * * @type {PluginContext} * @static * @memberof Activator */ static context = null; constructor() { this.start = this.start.bind(this); this.stop = this.stop.bind(this); this.serviceChangedListener = this.serviceChangedListener.bind(this); this.extensionChangedListener = this.extensionChangedListener.bind(this); } /** * 插件入口 * * @param {PluginContext} context 插件上下文 * @memberof Activator */ start(context) { Activator.context = context; Activator.context.addServiceChangedListener(this.serviceChangedListener); Activator.context.addExtensionChangedListener(this.extensionChangedListener); log.logger.info(`INFO: The plugin ${context.plugin.id} is started.`); } /** * 服务监听器 * * @param {string} name 服务名称 * @param {ServiceAction} action 服务变化活动 * @memberof Activator */ serviceChangedListener(name, action) { if (name === 'myService' && action === ServiceAction.ADDED) { let myService = Activator.context.getDefaultService(name); if (myService) { log.logger.info(`Get the myService instance successfully.`); } } else if (action === ServiceAction.REMOVED) { log.logger.info(`The service ${name} is removed.`); } } /** * 扩展变更监听器 * * @param {Extension} extension 扩展对象 * @param {ExtensionAction} action 扩展对象变化活动 * @memberof Activator */ extensionChangedListener(extension, action) { if (action === ExtensionAction.ADDED) { log.logger.info(`The extension ${extension.id} is added.`); let extensions = Activator.context.getExtensions('myExtension'); log.logger.info(`The extension count is ${extensions.size}.`); } if (action === ExtensionAction.REMOVED) { log.logger.info(`The extension ${extension.id} is removed.`); } } /** * 插件出口 * * @param {PluginContext} context 插件上下文 * @memberof Activator */ stop(context) { Activator.context = null; log.logger.info(`INFO: The plugin ${context.plugin.id} is stopped.`); } }
构建另一个插件如下。
// 1 plugin.config { "id": "demoPlugin2", "name": "demoPlugin2Test", "description": "The demo plugin2.", "version": "1.0.1", "startLevel": 5, "initializedState": "active", "activator": "PluginActivator.js", "dependencies": [{ "id": "demoPlugin", "version": "1.0.0" }], "services": [{ "name": "myService", "service": "MyService.js", "properties": { "vendor": "lorry" } }], "extensions": [{ "id": "myExtension", "data": { "extensionData": "lorry" } }, { "id": "myExtension2", "data": { "extensionData": "lorry2" } }] } // 2 MyService.js export default class MyService { } // 3 PluginActivator.js import { ServiceAction, PluginContext, Plugin, log } from 'minimajs'; export default class PluginActivator { constructor() { this.start = this.start.bind(this); this.stop = this.stop.bind(this); this.serviceChanged = this.serviceChanged.bind(this); } /** * 启动插件 * * @param {PluginContext} context 插件上下文 * @memberof PluginActivator */ start(context) { log.logger.info(`INFO: The plugin ${context.plugin.id} is started.`); context.addServiceChangedListener(this.serviceChangedListener); } /** * 服务监听 * * @param {string} name 服务名称 * @param {ServiceAction} action 服务活动 * @memberof PluginActivator */ serviceChangedListener(name, action) { if (action === ServiceAction.ADDED) { log.logger.info(`Service ${name} is register.`); } else { log.logger.info(`Service ${name} is unregister.`); } } /** * 停止插件 * * @param {PluginContext} context 插件上下文 * @memberof PluginActivator */ stop(context) { log.logger.info(`INFO: The plugin ${context.plugin.id} is stopped.`); } }
评论