From 1f0e0b1ce40b765af744d025a83bb85e0994244d Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Wed, 21 Sep 2022 14:22:47 +0200 Subject: xP: only care about RPC results if requested This prevents "No response" errors from firing for most commands. --- xP/public/xP.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'xP') diff --git a/xP/public/xP.js b/xP/public/xP.js index c8ceed0..f418d50 100644 --- a/xP/public/xP.js +++ b/xP/public/xP.js @@ -4,7 +4,7 @@ import * as Relay from './proto.js' // ---- RPC -------------------------------------------------------------------- -class RelayRpc extends EventTarget { +class RelayRPC extends EventTarget { constructor(url) { super() this.url = url @@ -72,7 +72,7 @@ class RelayRpc extends EventTarget { if (this.promised[e.commandSeq] !== undefined) this.promised[e.commandSeq].reject(e.error) else - console.error("Unawaited error") + console.error(`Unawaited error: ${e.error}`) break case Relay.Event.Response: if (this.promised[e.commandSeq] !== undefined) @@ -109,9 +109,16 @@ class RelayRpc extends EventTarget { seq = this.commandSeq = 0 this.ws.send(JSON.stringify({commandSeq: seq, data: params})) - return new Promise((resolve, reject) => { - this.promised[seq] = {resolve, reject} - }) + + // Automagically detect if we want a result. + let data = undefined + const promise = new Promise( + (resolve, reject) => { data = {resolve, reject} }) + promise.then = (...args) => { + this.promised[seq] = data + return Promise.prototype.then.call(promise, ...args) + } + return promise } } @@ -173,7 +180,7 @@ function updateIcon(highlighted) { // ---- Event processing ------------------------------------------------------- -let rpc = new RelayRpc(proxy) +let rpc = new RelayRPC(proxy) let rpcEventHandlers = new Map() let buffers = new Map() -- cgit v1.2.3-70-g09d2