mm_machine
Version:
这是超级美眉框架机制构建辅助模块,用于快速构建一个机制
211 lines (154 loc) • 4.29 kB
Markdown
javascript
new Item(dir, dir_base)
```
参数:
- `dir` {String} 当前目录
- `dir_base` {String} 模块目录
- `dir` {String} 当前路径
- `default_file` {String} 默认配置文件路径,默认为 "./sys.json"
- `filename` {String} 当前文件名
- `config` {Object} 配置参数对象
- `name` {String} 模块名称
- `title` {String} 模块标题
- `description` {String} 模块描述
- `func_file` {String} 函数文件路径
- `func_name` {String} 回调函数名
- `sort` {Number} 排序值
- `state` {Number} 状态(0:未启用, 1:启用)
- `show` {Number} 显示状态(0:不显示, 1:显示)
- `mode` {Number} 运行模式
- 1: 生产模式,文件改变不会重新加载
- 2: 热更新模式,文件改变时重新加载
- 3: 重载模式,执行完后重新加载
- 4: 热更新+重载模式
`Index` 类是机制管理的核心类,用于管理多个模块。
```javascript
new Index(scope, dir_base)
```
参数:
- `scope` {Object} 作用域
- `dir_base` {String} 模块目录
- `scope` {String} 作用域
- `list` {Array} 模块列表
- `type` {String} 机制类型
- `sort_key` {String} 排序键名
- `mode` {Number} 运行模式,同 Item 类的 mode
```javascript
const { Item, Index } = require('mm_machine');
// 创建自定义驱动类
class Drive extends Item {
constructor(dir, dir_base) {
super(dir, dir_base);
this.default_file = "./demo.json";
}
}
// 创建自定义引擎类
class Engine extends Index {
constructor(scope, dir_base) {
super(scope, dir_base);
this.mode = 0;
this.type = "demo";
}
}
Engine.prototype.Drive = Drive;
// 使用示例
async function demo() {
const engine = new Engine();
// 加载模块
await engine.update("./");
// 执行特定模块的方法
await engine.run('demo1', 'init');
await engine.exec('demo1', 'init');
// 执行所有模块的方法
await engine.run(null, 'main');
await engine.exec(null, 'main');
// 获取特定模块
const plug = engine.get('demo1');
if (plug) {
plug.loadFile(plug.filename);
}
}
```
```json
{
"name": "demo1",
"title": "示例脚本1",
"description": "用于测试动态加载、更新、卸载、删除脚本",
"func_file": "./index.js",
"func_name": "",
"sort": 10,
"state": 0,
"show": 0
}
```
```javascript
var i = 0;
function test() {
console.log("你好", i++)
}
function main() {
test();
return i;
}
exports.main = main;
exports.main_before = function() {
console.log("请求前")
}
exports.main_after = async function(ret) {
console.log("请求后", ret)
}
exports.init = function() {
console.log("初始化");
}
```
系统支持多种运行模式,可以通过设置 `mode` 属性来控制:
1. 生产模式 (mode = 1):文件改变不会触发重新加载
2. 热更新模式 (mode = 2):文件改变时自动重新加载
3. 重载模式 (mode = 3):执行完后重新加载,避免变量污染
4. 热更新+重载模式 (mode = 4):结合了模式2和3的特性
模块支持以下生命周期方法:
- `init`: 初始化时调用
- `main`: 主要执行方法
- `main_before`: 主方法执行前的钩子
- `main_after`: 主方法执行后的钩子
```javascript
// 重载模块
engine.reload("demo1");
// 卸载模块
engine.unload("demo2");
// 卸载并删除模块
engine.unload("demo1", true);
```
- mm_config: ^1.1.4
- mm_hot_reload: ^1.0.5
ISC
这是超级美眉框架机制构建辅助模块,用于快速构建一个机制。本模块提供了一个灵活的机制管理系统,支持动态加载、热更新和模块管理等功能。
- 支持动态加载和卸载模块
- 支持热更新
- 支持模块状态管理
- 灵活的配置系统
- 支持多种运行模式
```bash
npm install mm_machine
```
`Item` 类是模块的基础类,用于管理单个模块的配置和状态。
```