From 362e317a5d282cd19ec22531e3a70020d542919e Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 21 Dec 2019 14:30:13 -0500 Subject: Change FrontendApiSender.callbacks to be a map --- ext/fg/js/frontend-api-sender.js | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'ext/fg/js/frontend-api-sender.js') 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)); } -- cgit v1.2.3