simplify promise
This commit is contained in:
parent
123e725049
commit
867902523f
@ -318,13 +318,6 @@ const yomichan = (() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getTemporaryListenerResult(eventHandler, userCallback, timeout=null) {
|
getTemporaryListenerResult(eventHandler, userCallback, timeout=null) {
|
||||||
let resolve;
|
|
||||||
let reject;
|
|
||||||
const listenerPromise = new Promise((_resolve, _reject) => {
|
|
||||||
resolve = _resolve;
|
|
||||||
reject = _reject;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof eventHandler.addListener === 'undefined' ||
|
typeof eventHandler.addListener === 'undefined' ||
|
||||||
typeof eventHandler.removeListener === 'undefined'
|
typeof eventHandler.removeListener === 'undefined'
|
||||||
@ -332,32 +325,32 @@ const yomichan = (() => {
|
|||||||
throw new Error('Event handler type not supported');
|
throw new Error('Event handler type not supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
|
return new Promise((resolve, reject) => {
|
||||||
let timeoutId = null;
|
const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
|
||||||
if (timeout !== null) {
|
let timeoutId = null;
|
||||||
timeoutId = window.setTimeout(() => {
|
if (timeout !== null) {
|
||||||
timeoutId = null;
|
timeoutId = window.setTimeout(() => {
|
||||||
eventHandler.removeListener(runtimeMessageCallback);
|
timeoutId = null;
|
||||||
reject(new Error(`Listener timed out in ${timeout} ms`));
|
eventHandler.removeListener(runtimeMessageCallback);
|
||||||
}, timeout);
|
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();
|
const cleanupResolve = (value) => {
|
||||||
resolve(value);
|
if (timeoutId !== null) {
|
||||||
|
window.clearTimeout(timeoutId);
|
||||||
|
timeoutId = null;
|
||||||
|
}
|
||||||
|
eventHandler.removeListener(runtimeMessageCallback);
|
||||||
|
sendResponse();
|
||||||
|
resolve(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
userCallback({action, params}, {resolve: cleanupResolve, sender});
|
||||||
};
|
};
|
||||||
|
|
||||||
userCallback({action, params}, {resolve: cleanupResolve, sender});
|
eventHandler.addListener(runtimeMessageCallback);
|
||||||
};
|
});
|
||||||
|
|
||||||
eventHandler.addListener(runtimeMessageCallback);
|
|
||||||
|
|
||||||
return listenerPromise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
Loading…
Reference in New Issue
Block a user