refactor getTemporaryListenerResult timeout
This commit is contained in:
parent
d88635cbb2
commit
fa65ab3512
@ -50,7 +50,8 @@ class FrameOffsetForwarder {
|
|||||||
if (action === 'frameOffset' && isObject(params) && params.uniqueId === uniqueId) {
|
if (action === 'frameOffset' && isObject(params) && params.uniqueId === uniqueId) {
|
||||||
resolve(params);
|
resolve(params);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
5000
|
||||||
);
|
);
|
||||||
|
|
||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
|
@ -312,8 +312,7 @@ const yomichan = (() => {
|
|||||||
this.trigger('orphaned', {error});
|
this.trigger('orphaned', {error});
|
||||||
}
|
}
|
||||||
|
|
||||||
getTemporaryListenerResult(eventHandler, userCallback, timeout=30000) {
|
getTemporaryListenerResult(eventHandler, userCallback, timeout=null) {
|
||||||
let resolved = false;
|
|
||||||
let resolve;
|
let resolve;
|
||||||
let reject;
|
let reject;
|
||||||
const listenerPromise = new Promise((_resolve, _reject) => {
|
const listenerPromise = new Promise((_resolve, _reject) => {
|
||||||
@ -323,20 +322,24 @@ const yomichan = (() => {
|
|||||||
|
|
||||||
if (eventHandler === chrome.runtime.onMessage) {
|
if (eventHandler === chrome.runtime.onMessage) {
|
||||||
const runtimeMessageCallback = ({action, params}, sender, sendResponse) => {
|
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) => {
|
const cleanupResolve = (value) => {
|
||||||
resolved = true;
|
if (timeoutId !== null) {
|
||||||
|
window.clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
eventHandler.removeListener(runtimeMessageCallback);
|
eventHandler.removeListener(runtimeMessageCallback);
|
||||||
sendResponse();
|
sendResponse();
|
||||||
resolve(value);
|
resolve(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
if (!resolved) {
|
|
||||||
reject(new Error(`Listener timed out in ${timeout} ms`));
|
|
||||||
eventHandler.removeListener(runtimeMessageCallback);
|
|
||||||
}
|
|
||||||
}, timeout);
|
|
||||||
|
|
||||||
userCallback({action, params}, {resolve: cleanupResolve, sender});
|
userCallback({action, params}, {resolve: cleanupResolve, sender});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user