diff options
author | siikamiika <siikamiika@users.noreply.github.com> | 2020-03-22 12:45:58 +0200 |
---|---|---|
committer | siikamiika <siikamiika@users.noreply.github.com> | 2020-04-05 19:57:37 +0300 |
commit | fa65ab351293fa6af882479e0fad6aaca3152569 (patch) | |
tree | 3f30fd50a0ba14607f278dd319c4f64fd19f3b3e /ext/mixed/js | |
parent | d88635cbb28862e0efd79080e5c67865d0410238 (diff) |
refactor getTemporaryListenerResult timeout
Diffstat (limited to 'ext/mixed/js')
-rw-r--r-- | ext/mixed/js/core.js | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index f21bac23..616f48b8 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -312,8 +312,7 @@ const yomichan = (() => { this.trigger('orphaned', {error}); } - getTemporaryListenerResult(eventHandler, userCallback, timeout=30000) { - let resolved = false; + getTemporaryListenerResult(eventHandler, userCallback, timeout=null) { let resolve; let reject; const listenerPromise = new Promise((_resolve, _reject) => { @@ -323,20 +322,24 @@ const yomichan = (() => { if (eventHandler === chrome.runtime.onMessage) { const runtimeMessageCallback = ({action, params}, sender, sendResponse) => { + let timeoutId = null; + if (timeout !== null) { + timeoutId = window.setTimeout(() => { + timeoutId = null; + reject(new Error(`Listener timed out in ${timeout} ms`)); + eventHandler.removeListener(runtimeMessageCallback); + }, timeout); + } + const cleanupResolve = (value) => { - resolved = true; + if (timeoutId !== null) { + window.clearTimeout(timeoutId); + } eventHandler.removeListener(runtimeMessageCallback); sendResponse(); resolve(value); }; - setTimeout(() => { - if (!resolved) { - reject(new Error(`Listener timed out in ${timeout} ms`)); - eventHandler.removeListener(runtimeMessageCallback); - } - }, timeout); - userCallback({action, params}, {resolve: cleanupResolve, sender}); }; |