@ace-util/core
Version:
Utils.
154 lines • 5.47 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventBus = void 0;
var tools_1 = require("./tools");
/**
* Event bus
*/
var EventBus = /** @class */ (function () {
function EventBus() {
this.events = new Map();
}
/**
* 发射事件
* @param {string} event 事件名称
* @param {any} payload 事件载荷
*/
EventBus.prototype.emit = function (event) {
var e_1, _a;
var payload = [];
for (var _i = 1; _i < arguments.length; _i++) {
payload[_i - 1] = arguments[_i];
}
if (!this.events.has(event)) {
(0, tools_1.warn)(process.env.NODE_ENV === 'production', "event \"".concat(event, "\" is triggered, but no listeners\uFF0Cnothing will be happening."));
return this;
}
try {
for (var _b = __values(this.events.get(event)), _c = _b.next(); !_c.done; _c = _b.next()) {
var handler = _c.value;
handler.apply(void 0, __spreadArray([], __read(payload), false));
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return this;
};
/**
* 事件监听
* @param event 事件名称
* @param handler 处理事件的方法
*/
EventBus.prototype.on = function (event, handler) {
if (typeof handler === 'function') {
if (!this.events.has(event))
this.events.set(event, []);
this.events.get(event).push(handler);
}
else {
(0, tools_1.warn)(process.env.NODE_ENV === 'production', "handler must be a function, but recived ".concat(typeof handler));
}
return this;
};
/**
* 事件监听(只执行1次)
* @param event 事件名称
* @param handler 处理事件的方法
*/
EventBus.prototype.once = function (event, handler) {
var _this = this;
if (typeof handler === 'function') {
if (!this.events.has(event))
this.events.set(event, []);
this.events.get(event).push(function () {
var payload = [];
for (var _i = 0; _i < arguments.length; _i++) {
payload[_i] = arguments[_i];
}
handler.apply(void 0, __spreadArray([], __read(payload), false));
_this.off(event, handler);
});
}
else {
(0, tools_1.warn)(process.env.NODE_ENV === 'production', "handler must be a function, but recived ".concat(typeof handler));
}
return this;
};
/**
* 取消事件监听
* @param event 事件名称
* @param handler 处理事件的方法
*/
EventBus.prototype.off = function (event, handler) {
var hasHandler = false, isHandlerFunction = false, handles = [];
if ((hasHandler = this.events.has(event) && !!(handles = this.events.get(event)).length) &&
(isHandlerFunction = typeof handler === 'function')) {
var i = handles.findIndex(function (fn) { return fn === handler; });
i >= 0 && handles.splice(i, 1);
}
else if (!hasHandler) {
(0, tools_1.warn)(process.env.NODE_ENV === 'production', "no event name of \"".concat(event, "\" is on listening"));
}
else if (!isHandlerFunction) {
(0, tools_1.warn)(process.env.NODE_ENV === 'production', "handler must be a function, but recived ".concat(typeof handler));
}
return this;
};
/**
* 清理事件总线
*/
EventBus.prototype.clear = function () {
this.events = new Map();
return this;
};
/**
* 获取当前事件总线详情
*/
EventBus.prototype.getEvents = function () {
return Object.freeze(this.events);
};
return EventBus;
}());
exports.EventBus = EventBus;
//# sourceMappingURL=event-bus.js.map
;