minimajs

基于 NodeJS 的插件化框架
授权协议 MIT
开发语言 JavaScript
所属分类 Web应用开发、 Node.js 扩展
软件类型 开源软件
地区 国产
投 递 者 归浩博
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

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.`);
    }
}
 相关资料
  • 本文向大家介绍详解开源的JavaScript插件化框架MinimaJS,包括了详解开源的JavaScript插件化框架MinimaJS的使用技巧和注意事项,需要的朋友参考一下 本文介绍我开发的一个JavaScript编写的插件化框架——MinimaJS,完全开源,源码下载地址:https://github.com/lorry2018/minimajs。该框架参考OSGi规范,将该规范定义的三大插件

  • 本文向大家介绍基于bootstrap风格的弹框插件,包括了基于bootstrap风格的弹框插件的使用技巧和注意事项,需要的朋友参考一下 自己写的一款基于bootstrap风格的弹框插件,暂时只有确认框、提示框。后续功能扩展、bug修改再更新。 html页面中调用: 感觉写的不是很好,后面修改了或者扩展了功能再更新。源码会上传到文件。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多

  • WebUI automation testing framework based on Selenium 介绍: pyse基于selenium(webdriver)进行了简单的二次封装,比selenium提供的方法操作更简洁。 起因:   python + selenium 自动化测试写久了发现selenium(webdriver)提供原生的方法并简便,于是,产生了二次封装的想法。想不到太炫酷的名

  • 本文向大家介绍基于Jquery和html5的7款个性化地图插件,包括了基于Jquery和html5的7款个性化地图插件的使用技巧和注意事项,需要的朋友参考一下 1、HTML5世界地图 划分世界区域并显示国家名 这是一款基于HTML5的世界地图应用,它的特点是可以将地图中的各个国家区域进行划分,鼠标滑过时即可显示该区域对应的国家名称,你也可以对弹出的标签进行自定义文字和自定义样式。   在线演示  

  • 本文向大家介绍基于Koa(nodejs框架)对json文件进行增删改查的示例代码,包括了基于Koa(nodejs框架)对json文件进行增删改查的示例代码的使用技巧和注意事项,需要的朋友参考一下 想使用nodejs(koa)搭建一个完整的前后端,完成数据的增删改查,又不想使用数据库,那使用json文件吧。 本文介绍了基于koa的json文件的增、删、改、查。 代码准备 json示例 1.新增和修改