minimajs基于 NodeJS 的插件化框架
Minima 是由 ES6 開發(fā)的基于 NodeJS 的簡單而強(qiáng)大的插件框架。
Minima 有三個功能:(1)動態(tài)插件:定義插件結(jié)構(gòu),插件配置,插件依賴,插件生命周期,插件類加載; (2)服務(wù):插件與SOA之間的溝通; (3)擴(kuò)展:支持插件擴(kuò)展。
安裝:
Install with npm:
$ npm install --save minimajs
使用:
Minima是一個插件框架容器。 我們需要創(chuàng)建一個插件框架實(shí)例去啟動它。
import { Minima } from 'minimajs';
let minima = new Minima(__dirname + '/plugins');
minima.start();
Examples
在plugins目錄中創(chuàng)建一個簡單的插件,如下所示。
// 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.`);
}
/**
* 服務(wù)監(jiān)聽器
*
* @param {string} name 服務(wù)名稱
* @param {ServiceAction} action 服務(wù)變化活動
* @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.`);
}
}
/**
* 擴(kuò)展變更監(jiān)聽器
*
* @param {Extension} extension 擴(kuò)展對象
* @param {ExtensionAction} action 擴(kuò)展對象變化活動
* @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.`);
}
}
構(gòu)建另一個插件如下。
// 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);
}
/**
* 服務(wù)監(jiān)聽
*
* @param {string} name 服務(wù)名稱
* @param {ServiceAction} action 服務(wù)活動
* @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.`);
}
}評論
圖片
表情
