diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-02-14 18:18:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-14 18:18:02 -0500 |
commit | 48b59375eb50a3c11ab1cbee659164e6991827ac (patch) | |
tree | 160bfe321492839cf3211b27285490e43a7f130d /ext/js/yomichan.js | |
parent | 9279ced68660610764931da681f22c8b71bf1b6e (diff) |
Cleanup yomichan api (#1394)
* Move invokeMessageHandler to core.js
* Move getMessageResponseResult to backghend.js
* Replace getTemporaryListenerResult
Diffstat (limited to 'ext/js/yomichan.js')
-rw-r--r-- | ext/js/yomichan.js | 75 |
1 files changed, 1 insertions, 74 deletions
diff --git a/ext/js/yomichan.js b/ext/js/yomichan.js index 107694e9..1eae2d4d 100644 --- a/ext/js/yomichan.js +++ b/ext/js/yomichan.js @@ -120,42 +120,6 @@ class Yomichan extends EventDispatcher { } } - getTemporaryListenerResult(eventHandler, userCallback, timeout=null) { - if (!( - typeof eventHandler.addListener === 'function' && - typeof eventHandler.removeListener === 'function' - )) { - throw new Error('Event handler type not supported'); - } - - return new Promise((resolve, reject) => { - const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { - let timeoutId = null; - if (timeout !== null) { - timeoutId = setTimeout(() => { - timeoutId = null; - eventHandler.removeListener(runtimeMessageCallback); - reject(new Error(`Listener timed out in ${timeout} ms`)); - }, timeout); - } - - const cleanupResolve = (value) => { - if (timeoutId !== null) { - clearTimeout(timeoutId); - timeoutId = null; - } - eventHandler.removeListener(runtimeMessageCallback); - sendResponse(); - resolve(value); - }; - - userCallback({action, params}, {resolve: cleanupResolve, sender}); - }; - - eventHandler.addListener(runtimeMessageCallback); - }); - } - sendMessage(...args) { try { return chrome.runtime.sendMessage(...args); @@ -174,43 +138,6 @@ class Yomichan extends EventDispatcher { } } - 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 deserializeError(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: serializeError(error)}); } - ); - return true; - } else { - callback({result: promiseOrResult}); - return false; - } - } catch (error) { - callback({error: serializeError(error)}); - return false; - } - } - triggerExtensionUnloaded() { this._isExtensionUnloaded = true; if (this._isTriggeringExtensionUnloaded) { return; } @@ -235,7 +162,7 @@ class Yomichan extends EventDispatcher { _onMessage({action, params}, sender, callback) { const messageHandler = this._messageHandlers.get(action); if (typeof messageHandler === 'undefined') { return false; } - return this.invokeMessageHandler(messageHandler, params, callback, sender); + return invokeMessageHandler(messageHandler, params, callback, sender); } _onMessageIsReady() { |