WaterControl/assets/Script/module/Notification/Notification.ts
2024-07-10 18:35:07 +08:00

45 lines
1.2 KiB
TypeScript

//全局通知
var Notifications = {
_eventMap: [],
on: function (masgId, callback, target) {
if (this._eventMap[masgId] === undefined) {
this._eventMap[masgId] = [];
}
this._eventMap[masgId].push({ callback: callback, target: target });
},
emit: function (masgId, parameter) {
let array = this._eventMap[masgId];
if (array === undefined) return;
for (let i = 0; i < array.length; i++) {
let element = array[i];
if (element) element.callback.call(element.target, parameter);
}
},
off: function (masgId, callback) {
let array = this._eventMap[masgId];
if (array === undefined) return;
for (let i = 0; i < array.length; i++) {
let element = array[i];
if (element && element.callback === callback) {
array[i] = undefined;
break;
}
}
},
offMasgId: function (masgId) {
this._eventMap[masgId] = undefined;
},
removeAllMsg: function () {
for (let k in this._eventMap) {
if (this._eventMap[k]) {
this.offMasgId(k);
}
}
}
};
export { Notifications };