基于 NodeJS 的插件化框架 minimajs

码农软件 · 软件分类 · Node.js 扩展 · 2019-04-14 06:12:39

软件介绍

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.`);
    }
}

本文地址:https://codercto.com/soft/d/3533.html

世界是平的(3.0版)

世界是平的(3.0版)

[美] 托马斯·弗里德曼 / 何帆、肖莹莹、郝正非 / 湖南科学技术出版社 / 2008-9 / 58.00元

世界变得平坦,是不是迫使我们跑得更快才能拥有一席之地? 在《世界是平的》中,托马斯·弗里德曼描述了当代世界发生的重大变化。科技和通信领域如闪电般迅速的进步,使全世界的人们可以空前地彼此接近——在印度和中国创造爆炸式增长的财富;挑战我们中的一些人,比他们更快占领地盘。3.0版新增两章,更新了报告和注释方面的内容,这些内容均采自作者考察世界各地特别是整个美国中心地带的见闻,在美国本土,世界的平坦......一起来看看 《世界是平的(3.0版)》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具