use duck typing for event listener
This commit is contained in:
parent
067c26982e
commit
123e725049
@ -325,35 +325,38 @@ 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);
|
||||
}
|
||||
|
||||
const cleanupResolve = (value) => {
|
||||
if (timeoutId !== null) {
|
||||
window.clearTimeout(timeoutId);
|
||||
timeoutId = null;
|
||||
}
|
||||
eventHandler.removeListener(runtimeMessageCallback);
|
||||
sendResponse();
|
||||
resolve(value);
|
||||
};
|
||||
|
||||
userCallback({action, params}, {resolve: cleanupResolve, sender});
|
||||
};
|
||||
|
||||
eventHandler.addListener(runtimeMessageCallback);
|
||||
} else {
|
||||
if (
|
||||
typeof eventHandler.addListener === 'undefined' ||
|
||||
typeof eventHandler.removeListener === 'undefined'
|
||||
) {
|
||||
throw new Error('Event handler type not supported');
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const cleanupResolve = (value) => {
|
||||
if (timeoutId !== null) {
|
||||
window.clearTimeout(timeoutId);
|
||||
timeoutId = null;
|
||||
}
|
||||
eventHandler.removeListener(runtimeMessageCallback);
|
||||
sendResponse();
|
||||
resolve(value);
|
||||
};
|
||||
|
||||
userCallback({action, params}, {resolve: cleanupResolve, sender});
|
||||
};
|
||||
|
||||
eventHandler.addListener(runtimeMessageCallback);
|
||||
|
||||
return listenerPromise;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user