Files
fc/dev/project/library/imports/04/04d85073-da8d-4068-845c-477249f3ea00.js
2026-05-24 10:21:26 +08:00

119 lines
2.9 KiB
JavaScript

"use strict";
cc._RF.push(module, '04d85Bz2o1AaIRcR3JJ8+oA', 'nx.event');
// Scripts/nx/kernel/nx.event.js
"use strict";
/*******************************************************************************
*
* Nx事件服务
*
*
*
* 2021.12.10
******************************************************************************/
var NxObject = require("nx.object");
var nxEventor = cc.Class({
"extends": NxObject,
name: "nxEventor",
// 初始化
initialize: function initialize(_args) {
// USPER
if (!this._super(_args)) {
return false;
}
this.binders = {};
// 全局服务开启
nx.event = this;
return true;
},
// 销毁
uninitialize: function uninitialize() {
this.clear();
// 全局服务关闭
nx.event = null;
// USPER
return this._super();
},
// 是否存在监听
isbinded: function isbinded(_binder) {
return this.binders[_binder] ? true : false;
},
// 监听
bind: function bind(_binder, _lst) {
var key = nx.factory.getUUID(_binder, true);
if (nx.dt.strEmpty(key)) {
nx.logger.error("[事件]监听事件失败,无效的监听对象!" + key);
return false;
}
if (this.binders[key]) {
nx.logger.error("[事件]监听关键字重复!" + key);
return false;
}
if (!nx.dt.arrGood(_lst)) {
nx.logger.error("[事件]监听事件失败,序列无效!" + key);
return false;
}
var bds = {};
for (var i in _lst) {
var bd = _lst[i];
var id = "" + bd[0];
if (nx.dt.strEmpty(id) || !nx.dt.fnGood(bd[1])) {
nx.logger.error("[事件]监听事件失败,无效的监听项!" + key);
return false;
}
bds[id] = bd[1];
}
this.binders[key] = bds;
nx.logger.debug("[事件]增加监听:", key);
return true;
},
// 解除
unbind: function unbind(_binder) {
var key = nx.factory.getUUID(_binder, false);
if (nx.dt.strEmpty(key)) {
nx.logger.error("[事件]解除监听事件失败,无效的监听对象!" + key);
return false;
}
if (this.binders[key]) {
nx.logger.debug("[事件]解除监听:", key);
delete this.binders[key];
}
return true;
},
// 发送
send: function send(id, args, log) {
var _this = this;
if (log === void 0) {
log = true;
}
id = id + '';
if (nx.dt.strEmpty(id)) {
nx.logger.error("[事件]消息发送失败,参数非法!");
return false;
}
if (log) {
nx.logger.debug("[事件]发布:%s 参数:%s", id, args ? nx.dt.enjson(args) : "");
}
nx.dt.trycatch(function () {
for (var k in _this.binders) {
var bs = _this.binders[k];
if (typeof bs[id] == "function") {
bs[id](args);
}
}
});
return true;
},
// 清理
clear: function clear() {
this.binders = {};
}
});
module.exports = nxEventor;
cc._RF.pop();