Compare commits

..

No commits in common. "6b4709d01182da51bcfec33b7922f871953599cc" and "ef8d3068a8fb262cc5994f6d9eb1aa5e1ec57ee8" have entirely different histories.

2 changed files with 4 additions and 21 deletions

View File

@ -3,15 +3,8 @@ export class EventHandler {
this.subscribers = {}; this.subscribers = {};
} }
addEventListener(type, handler, { once = false } = {}) { addEventListener(type, handler) {
if (!this.subscribers[type]) this.subscribers[type] = []; if (!this.subscribers[type]) this.subscribers[type] = [];
if (once) {
const originalHandler = handler;
handler = (...args) => {
originalHandler(...args);
this.removeEventListener(type, handler);
};
}
this.subscribers[type].push(handler); this.subscribers[type].push(handler);
} }
@ -19,15 +12,4 @@ export class EventHandler {
if (this.subscribers[type]) if (this.subscribers[type])
this.subscribers[type].forEach((handler) => handler(...data)); this.subscribers[type].forEach((handler) => handler(...data));
} }
removeEventListener(type, handler) {
if (!this.subscribers[type]) return;
this.subscribers[type] = this.subscribers[type].filter(
(h) => h !== handler
);
if (this.subscribers[type].length === 0) {
delete this.subscribers[type];
}
}
} }

View File

@ -142,9 +142,10 @@ export class Socket extends EventHandler {
method, method,
args, args,
}; };
const me = this;
return new Promise((resolve) => { return new Promise((resolve) => {
this.addEventListener(call.callId, (data) => resolve(data), { once: true}); me.addEventListener(call.callId, (data) => resolve(data));
this.sendJson(call); me.sendJson(call);
}); });
} }
} }