基于 NodeJS 的插件化框架 minimajs
- 授权协议: MIT
- 开发语言: JavaScript
- 操作系统: 跨平台
- 软件首页: https://gitee.com/lorrychen/minimajs
- 软件文档: https://gitee.com/lorrychen/minimajs/blob/master/README.md
软件介绍
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.`);
}
}
函数式算法设计珠玑
Richard Bird / 苏统华、孙芳媛、郝文超、徐琴 / 机械工业出版社 / 2017-4-1 / 69.00
本书采用完全崭新的方式介绍算法设计。全书由30个珠玑构成,每个珠玑单独列为一章,用于解决一个特定编程问题。这些问题的出处五花八门,有的来自游戏或拼图,有的是有趣的组合任务,还有的是散落于数据压缩及字串匹配等领域的更为熟悉的算法。每个珠玑以使用函数式编程语言Haskell对问题进行描述作为开始,每个解答均是诉诸于函数式编程法则从问题表述中计算得到。本书适用于那些喜欢学习算法设计思想的函数式编程人员、......一起来看看 《函数式算法设计珠玑》 这本书的介绍吧!
