file-lane
Version:
File conversion tool, can be one-to-one, one to N, N to one
62 lines (61 loc) • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
class AsyncEventDispatcher {
_eventMap = (() => new Map())();
async dispatch(event) {
return new Promise(resolve => {
const {
type
} = event;
const callList = this.getCallList(type);
let callCount = 0;
if (callList) {
for (let item of callList) {
item(event, () => {
callCount++;
if (callCount === callList.length) {
resolve();
}
});
}
}
});
}
addEventListener(type, handler) {
if (!this.hasListener(type)) {
this._eventMap.set(type, []);
}
const callList = this.getCallList(type);
if (callList && !callList.includes(handler)) {
callList.push(handler);
}
}
removeEventListener(type, handler) {
if (!this.hasListener(type)) {
return;
}
const callList = this.getCallList(type);
if (callList) {
const index = callList.indexOf(handler);
if (index >= 0) {
callList.splice(index, 1);
}
}
}
removeAllListener(type) {
this._eventMap.delete(type);
}
clear() {
this._eventMap.clear();
}
hasListener(type) {
return this._eventMap.has(type);
}
getCallList(type) {
return this._eventMap.get(type);
}
}
var _default = exports.default = AsyncEventDispatcher;