UNPKG

@bos-alpha/data

Version:

数据管理

45 lines (44 loc) 1.41 kB
var EventBus = /** @class */ (function () { function EventBus() { this.events = this.events || {}; } EventBus.prototype.emit = function (type) { var args = []; for (var _i = 1; _i < arguments.length; _i++) { args[_i - 1] = arguments[_i]; } var e; e = this.events[type]; // 查看这个type的event有多少个回调函数,如果有多个需要依次调用。 if (Array.isArray(e)) { for (var i = 0; i < e.length; i++) { e[i].apply(this, args); } } else { e[0].apply(this, args); } }; // 然后我们需要写监听函数,参数是事件type和触发时需要执行的回调函数 EventBus.prototype.on = function (type, fun) { var events = this.events[type]; if (!events) { //如果从未注册过监听函数,则将函数放入数组存入对应的键名下 this.events[type] = [fun]; } else { // 如果注册过,则直接放入 events.push(fun); } }; // 取消监听 清空所有 EventBus.prototype.off = function (type) { var events = this.events[type]; if (events) { this.events[type] = []; } }; return EventBus; }()); var eventBus = new EventBus(); export default eventBus;