diff options
-rw-r--r-- | ext/fg/js/frontend-api-sender.js | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/ext/fg/js/frontend-api-sender.js b/ext/fg/js/frontend-api-sender.js index af998a8f..b7c2c57c 100644 --- a/ext/fg/js/frontend-api-sender.js +++ b/ext/fg/js/frontend-api-sender.js @@ -22,7 +22,7 @@ class FrontendApiSender { this.senderId = FrontendApiSender.generateId(16); this.ackTimeout = 3000; // 3 seconds this.responseTimeout = 10000; // 10 seconds - this.callbacks = {}; + this.callbacks = new Map(); this.disconnected = false; this.nextId = 0; @@ -43,7 +43,7 @@ class FrontendApiSender { return new Promise((resolve, reject) => { const info = {id, resolve, reject, ack: false, timer: null}; - this.callbacks[id] = info; + this.callbacks.set(id, info); info.timer = setTimeout(() => this.onError(id, 'Timeout (ack)'), this.ackTimeout); this.port.postMessage({id, action, params, target, senderId: this.senderId}); @@ -71,19 +71,18 @@ class FrontendApiSender { onDisconnect() { this.disconnected = true; - const ids = Object.keys(this.callbacks); - for (const id of ids) { + for (const id of this.callbacks.keys()) { this.onError(id, 'Disconnected'); } } onAck(id) { - if (!hasOwn(this.callbacks, id)) { + const info = this.callbacks.get(id); + if (typeof info === 'undefined') { console.warn(`ID ${id} not found for ack`); return; } - const info = this.callbacks[id]; if (info.ack) { console.warn(`Request ${id} already ack'd`); return; @@ -95,18 +94,18 @@ class FrontendApiSender { } onResult(id, data) { - if (!hasOwn(this.callbacks, id)) { + const info = this.callbacks.get(id); + if (typeof info === 'undefined') { console.warn(`ID ${id} not found`); return; } - const info = this.callbacks[id]; if (!info.ack) { console.warn(`Request ${id} not ack'd`); return; } - delete this.callbacks[id]; + this.callbacks.delete(id); clearTimeout(info.timer); info.timer = null; @@ -118,9 +117,9 @@ class FrontendApiSender { } onError(id, reason) { - if (!hasOwn(this.callbacks, id)) { return; } - const info = this.callbacks[id]; - delete this.callbacks[id]; + const info = this.callbacks.get(id); + if (typeof info === 'undefined') { return; } + this.callbacks.delete(id); info.timer = null; info.reject(new Error(reason)); } |