diff options
Diffstat (limited to 'ext/mixed')
-rw-r--r-- | ext/mixed/js/comm.js | 25 | ||||
-rw-r--r-- | ext/mixed/js/yomichan.js | 56 |
2 files changed, 49 insertions, 32 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) { diff --git a/ext/mixed/js/yomichan.js b/ext/mixed/js/yomichan.js index d921a5a2..7fffbaa6 100644 --- a/ext/mixed/js/yomichan.js +++ b/ext/mixed/js/yomichan.js @@ -54,10 +54,10 @@ const yomichan = (() => { this._isBackendPreparedPromiseResolve = resolve; this._messageHandlers = new Map([ - ['backendPrepared', this._onMessageBackendPrepared.bind(this)], - ['getUrl', this._onMessageGetUrl.bind(this)], - ['optionsUpdated', this._onMessageOptionsUpdated.bind(this)], - ['zoomChanged', this._onMessageZoomChanged.bind(this)] + ['backendPrepared', {async: false, handler: this._onMessageBackendPrepared.bind(this)}], + ['getUrl', {async: false, handler: this._onMessageGetUrl.bind(this)}], + ['optionsUpdated', {async: false, handler: this._onMessageOptionsUpdated.bind(this)}], + ['zoomChanged', {async: false, handler: this._onMessageZoomChanged.bind(this)}] ]); } @@ -210,6 +210,43 @@ const yomichan = (() => { } } + getMessageResponseResult(response) { + let error = chrome.runtime.lastError; + if (error) { + throw new Error(error.message); + } + if (!isObject(response)) { + throw new Error('Tab did not respond'); + } + error = response.error; + if (error) { + throw jsonToError(error); + } + return response.result; + } + + invokeMessageHandler({handler, async}, params, callback, ...extraArgs) { + try { + let promiseOrResult = handler(params, ...extraArgs); + if (async === 'dynamic') { + ({async, result: promiseOrResult} = promiseOrResult); + } + if (async) { + promiseOrResult.then( + (result) => { callback({result}); }, + (error) => { callback({error: errorToJson(error)}); } + ); + return true; + } else { + callback({result: promiseOrResult}); + return false; + } + } catch (error) { + callback({error: errorToJson(error)}); + return false; + } + } + // Private _onExtensionUnloaded(error) { @@ -222,16 +259,13 @@ const yomichan = (() => { } _getLogContext() { - return {url: this._getUrl()}; + return this._getUrl(); } _onMessage({action, params}, sender, callback) { - const handler = this._messageHandlers.get(action); - if (typeof handler !== 'function') { return false; } - - const result = handler(params, sender); - callback(result); - return false; + const messageHandler = this._messageHandlers.get(action); + if (typeof messageHandler === 'undefined') { return false; } + return this.invokeMessageHandler(messageHandler, params, callback, sender); } _onMessageBackendPrepared() { |