aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorsiikamiika <siikamiika@users.noreply.github.com>2020-04-04 16:59:17 +0300
committersiikamiika <siikamiika@users.noreply.github.com>2020-04-05 19:57:37 +0300
commit123e725049e5c6b5c3b7b53ee37aaf93e7bb0c61 (patch)
treeada636d3c61befadb21d8d62a93a204c468ae637 /ext
parent067c26982e4b374fbb51fa49d9930ddf0c8e00fd (diff)
use duck typing for event listener
Diffstat (limited to 'ext')
-rw-r--r--ext/mixed/js/core.js49
1 files changed, 26 insertions, 23 deletions
diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js
index 4314926c..7fb3c32e 100644
--- a/ext/mixed/js/core.js
+++ b/ext/mixed/js/core.js
@@ -325,34 +325,37 @@ const yomichan = (() => {
reject = _reject;
});
- if (eventHandler === chrome.runtime.onMessage) {
- const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
- let timeoutId = null;
- if (timeout !== null) {
- timeoutId = window.setTimeout(() => {
- timeoutId = null;
- eventHandler.removeListener(runtimeMessageCallback);
- reject(new Error(`Listener timed out in ${timeout} ms`));
- }, timeout);
- }
+ if (
+ typeof eventHandler.addListener === 'undefined' ||
+ typeof eventHandler.removeListener === 'undefined'
+ ) {
+ throw new Error('Event handler type not supported');
+ }
- const cleanupResolve = (value) => {
- if (timeoutId !== null) {
- window.clearTimeout(timeoutId);
- timeoutId = null;
- }
+ const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
+ let timeoutId = null;
+ if (timeout !== null) {
+ timeoutId = window.setTimeout(() => {
+ timeoutId = null;
eventHandler.removeListener(runtimeMessageCallback);
- sendResponse();
- resolve(value);
- };
+ reject(new Error(`Listener timed out in ${timeout} ms`));
+ }, timeout);
+ }
- userCallback({action, params}, {resolve: cleanupResolve, sender});
+ const cleanupResolve = (value) => {
+ if (timeoutId !== null) {
+ window.clearTimeout(timeoutId);
+ timeoutId = null;
+ }
+ eventHandler.removeListener(runtimeMessageCallback);
+ sendResponse();
+ resolve(value);
};
- eventHandler.addListener(runtimeMessageCallback);
- } else {
- throw new Error('Event handler type not supported');
- }
+ userCallback({action, params}, {resolve: cleanupResolve, sender});
+ };
+
+ eventHandler.addListener(runtimeMessageCallback);
return listenerPromise;
}