aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js/comm.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-07-11 15:20:00 -0400
committerGitHub <noreply@github.com>2020-07-11 15:20:00 -0400
commitec42a7e4d61dc30c2839a7ff7be44bec131127a5 (patch)
tree73d0221a3aebab8bf786012d1a61f14483b59df8 /ext/mixed/js/comm.js
parent8389cd8ba27f328123fb73e72b24e69a4a6de2c1 (diff)
Message handler refactor (#660)
* Refactor searchQueryUpdate action * Use standard message handler style * Use name "promiseOrResult" for consistency * Use standard message handler convention for Yomichan message handlers * Use common message handler invoker
Diffstat (limited to 'ext/mixed/js/comm.js')
-rw-r--r--ext/mixed/js/comm.js25
1 files changed, 4 insertions, 21 deletions
diff --git a/ext/mixed/js/comm.js b/ext/mixed/js/comm.js
index 182400e3..1516a98f 100644
--- a/ext/mixed/js/comm.js
+++ b/ext/mixed/js/comm.js
@@ -166,31 +166,14 @@ class CrossFrameAPIPort extends EventDispatcher {
// Invocation
_onInvoke(id, {action, params}) {
+ const callback = (response) => this._sendResponse({type: 'result', id, data: response});
const messageHandler = this._messageHandlers.get(action);
if (typeof messageHandler === 'undefined') {
- this._sendError(id, new Error(`Unknown action: ${action}`));
- return;
+ callback({error: new Error(`Unknown action: ${action}`)});
+ return false;
}
-
- let {handler, async} = messageHandler;
-
this._sendAck(id);
- try {
- let result = handler(params);
- if (async === 'dynamic') {
- ({async, result} = result);
- }
- if (async) {
- result.then(
- (result2) => this._sendResult(id, result2),
- (error2) => this._sendError(id, error2)
- );
- } else {
- this._sendResult(id, result);
- }
- } catch (error) {
- this._sendError(id, error);
- }
+ return yomichan.invokeMessageHandler(messageHandler, params, callback);
}
_sendResponse(data) {