aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-21 14:30:13 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-21 14:30:13 -0500
commit362e317a5d282cd19ec22531e3a70020d542919e (patch)
tree23de79961351575ba4020ff6ee2e7ba6abe2ac39 /ext/fg/js
parenta2175f2c293ef2de41a58acdd9adc202b0302d67 (diff)
Change FrontendApiSender.callbacks to be a map
Diffstat (limited to 'ext/fg/js')
-rw-r--r--ext/fg/js/frontend-api-sender.js23
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));
}