mm_os
Version:
这是超级美眉服务端框架,用于快速构建应用程序。
345 lines (323 loc) • 7.63 kB
JavaScript
const Index = require('mm_machine').Index;
const Drive = require('./drive');
/**
* Event事件类
* @extends {Index}
* @class
*/
class Event extends Index {
/**
* 构造函数
* @param {Object} scope 作用域
* @param {String} title 标题
* @constructor
*/
constructor(scope, title) {
super(scope, __dirname);
this.Drive = Drive;
this.type = "event";
this.title = title;
// 默认启用热更新
this.mode = 3;
/* === 验证 === */
// 验证前
this.list_before = [];
// 验证
this.list_check = [];
// 执行
this.list_main = [];
// 渲染
this.list_render = [];
// 渲染后
this.list_after = [];
}
}
/**
* 清除事件
* @param {String} stage 阶段
*/
Event.prototype.clear = function(stage) {
if (stage) {
if (this[stage]) {
this[stage] = [];
} else {
$.log.debug('清空失败,事件发展阶段{0}不存在!'.replace("{0}", stage));
}
} else {
this.list_before = [];
this.list_check = [];
this.list_main = [];
this.list_render = [];
this.list_after = [];
}
};
/**
* 更新脚本子函数
*/
Event.prototype.update_script_sub = async function(list) {
for (var i = 0; i < list.length; i++) {
var o = list[i];
if (o.config.state === 1) {
await o.exec('load');
}
}
}
/**
* 更新脚本
*/
Event.prototype.update_script = async function() {
this.update_script_sub(this.list_before);
this.update_script_sub(this.list_check);
this.update_script_sub(this.list_main);
this.update_script_sub(this.list_render);
this.update_script_sub(this.list_after);
}
/**
* 加载项
* @param {String} dir 文件路径
* @param {Object} cg 配置参数
* @param {String} file 配置文件
*/
Event.prototype.load_item = async function(dir, cg, file) {
if (this.Drive) {
var drive = new this.Drive(dir, this.dir_base.fullname());
drive.mode = this.mode;
if (cg) {
await drive.exec('load_config', file, cg.name);
await drive.exec('set_config', cg);
} else {
await drive.exec('load_config', file);
}
var key = 'list_' + drive.config.stage;
var list = this[key];
if (!list) {
return;
}
list.push(drive);
return drive;
} else {
var json = file.loadJson();
if (!json) {
var fl = this.dir_base + "/config.tpl.json";
if (fl.hasFile()) {
fl.copyFile(file);
json = file.loadJson();
}
}
if (json) {
var key = 'list_' + json.stage;
var list = this[key];
if (!list) {
return;
}
list.push(json);
}
return json;
}
};
/**
* 事件排序
* @param {String} list 列表
*/
Event.prototype.sort_sub = function(list) {
list.sort(function(o1, o2) {
var sort = o1.config.sort - o2.config.sort;
if (sort === 0) {
sort = o2.config.target.length - o1.config.target.length;
}
return sort;
});
};
/**
* 事件排序
* @param {String} stage 阶段
*/
Event.prototype.sort = function(stage) {
if (stage) {
if (this[stage]) {
this[stage] = [];
} else {
$.log.debug('清空失败,事件发展阶段{0}不存在!'.replace("{0}", stage));
}
} else {
this.sort_sub(this.list_before);
this.sort_sub(this.list_check);
this.sort_sub(this.list_main);
this.sort_sub(this.list_render);
this.sort_sub(this.list_after);
}
};
/**
* 执行事件
*/
Event.prototype.doing = async function(o, ctx, db) {
var ret = o.run(ctx, db);
if (types.isPromise(ret)) {
ret = await ret;
}
if (this.mode > 4) {
o.exec('reload', o.filename);
}
return ret;
}
/**
* 执行函数
* @param {Array} list 列表
* @param {String} target 目标
* @param {Object} ctx 请求上下文
* @param {Object} db 数据管理器
* @return {Object} 执行结果
*/
Event.prototype.run_sub = async function(list, target, ctx, db) {
for (var i = 0, o; o = list[i++];) {
if (o.config.state === 1 && target.has(o.config.target)) {
var ret = await o.run(ctx, db);
if (this.mode > 4) {
o.exec('reload', o.filename);
}
if (ret) {
db.ret = ret;
if (o.config.end) {
break;
}
}
}
}
return db.ret;
};
/**
* 之前
* @param {String} target 目标
* @param {Object} ctx 请求上下文
* @param {Object} db 数据管理器
* @return {Object} 执行结果
*/
Event.prototype.before = async function(target, ctx, db) {
return await this.run_sub(this.list_before, target, ctx, db);
};
/**
* 验证
* @param {String} target 目标
* @param {Object} ctx 请求上下文
* @param {Object} db 数据管理器
* @return {Object} 执行结果
*/
Event.prototype.check = async function(target, ctx, db) {
return await this.run_sub(this.list_check, target, ctx, db);
};
/**
* 主要
* @param {String} target 目标
* @param {Object} ctx 请求上下文
* @param {Object} db 数据管理器
* @return {Object} 执行结果
*/
Event.prototype.main = async function(target, ctx, db) {
return await this.run_sub(this.list_main, target, ctx, db);
};
/**
* 渲染
* @param {String} target 目标
* @param {Object} ctx 请求上下文
* @param {Object} db 数据管理器
* @return {Object} 执行结果
*/
Event.prototype.render = async function(target, ctx, db) {
return await this.run_sub(this.list_render, target, ctx, db);
};
/**
* 之后
* @param {String} target 目标
* @param {Object} ctx 请求上下文
* @param {Object} db 数据管理器
* @return {Object} 执行结果
*/
Event.prototype.after = async function(target, ctx, db) {
return await this.run_sub(this.list_after, target, ctx, db);
};
/**
* 执行事件
* @param {String} target 目标
* @param {Object} ctx 请求上下文
* @param {Object} db 数据管理器
* @return {Object} 执行结果
*/
Event.prototype.run = async function(target, ctx, db) {
if (!db) {
db = {
ret: null
};
}
var ret = await this.before(target, ctx, db);
if (!ret) {
ret = await this.check(target, ctx, db);
if (!ret) {
ret = await this.main(target, ctx, db);
}
ret = await this.render(target, ctx, db);
if (ret) {
ret = await this.after(target, ctx, db);
}
}
return ret;
};
/**
* @description 获取事件
* @param {String} name 名称
* @param {String} event_type 事件类型
* @return {Object} 返回单项配置
*/
Event.prototype.get = function(name, event_type = 'main') {
var obj;
var lt = this['list_' + event_type];
var len = lt.length;
for (var i = 0; i < len; i++) {
var o = lt[i];
if (name === o.config.name) {
obj = o;
break;
}
}
return obj;
};
/**
* @description 重载脚本和配置
* @param {String} file 文件名
* @return {String} 重载失败返回错误提示,重载成功返回null
*/
Event.prototype.reload = async function(name, event_type = 'main') {
var o = this.get(name, event_type);
if (o) {
await o.exec('reload');
return null;
}
return '没有找到模块';
}
/**
* @module 导出Event类
*/
module.exports = Event;
/**
* Event事件池
*/
if (!$.pool.event) {
$.pool.event = {};
}
function event_admin(scope, title) {
if (!scope) {
scope = $.val.scope + '';
}
var obj = $.pool.event[scope];
if (!obj) {
$.pool.event[scope] = new Event(scope, title);
obj = $.pool.event[scope];
}
return obj;
}
/**
* Event管理器,用于创建缓存
* @param {String} scope 作用域
* @param {string} title 标题
* @return {Object} 返回一个缓存类
*/
$.event_admin = event_admin;