summaryrefslogtreecommitdiff
path: root/ext/fg/js/frontend-api-sender.js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg/js/frontend-api-sender.js')
-rw-r--r--ext/fg/js/frontend-api-sender.js27
1 files changed, 13 insertions, 14 deletions
diff --git a/ext/fg/js/frontend-api-sender.js b/ext/fg/js/frontend-api-sender.js
index af998a8f..93c2e593 100644
--- a/ext/fg/js/frontend-api-sender.js
+++ b/ext/fg/js/frontend-api-sender.js
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
+ * Copyright (C) 2019-2020 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* This program is free software: you can redistribute it and/or modify
@@ -13,7 +13,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
@@ -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));
}