Change AppBridge interval to timeout (#128)

This commit is contained in:
Lukasz Ostrowski 2022-12-02 16:08:39 +01:00 committed by GitHub
parent 063e56f345
commit b39081ed9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 39 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View file

@ -200,7 +200,9 @@ export class AppBridge {
debug("window.parent doesnt exist, will throw"); debug("window.parent doesnt exist, will throw");
reject(new Error("Parent window does not exist.")); reject(new Error("Parent window does not exist."));
} else { return;
}
debug("Calling window.parent.postMessage with %j", action); debug("Calling window.parent.postMessage with %j", action);
window.parent.postMessage( window.parent.postMessage(
@ -211,7 +213,7 @@ export class AppBridge {
"*" "*"
); );
let intervalId: number; let timeoutId: number;
const unsubscribe = this.subscribe(EventType.response, ({ actionId, ok }) => { const unsubscribe = this.subscribe(EventType.response, ({ actionId, ok }) => {
debug( debug(
@ -224,7 +226,7 @@ export class AppBridge {
if (action.payload.actionId === actionId) { if (action.payload.actionId === actionId) {
debug("Received matching action id: %s. Will unsubscribe", actionId); debug("Received matching action id: %s. Will unsubscribe", actionId);
unsubscribe(); unsubscribe();
clearInterval(intervalId); clearTimeout(timeoutId);
if (ok) { if (ok) {
resolve(); resolve();
@ -238,11 +240,10 @@ export class AppBridge {
} }
}); });
intervalId = window.setInterval(() => { timeoutId = window.setTimeout(() => {
unsubscribe(); unsubscribe();
reject(new Error("Action response timed out.")); reject(new Error("Action response timed out."));
}, DISPATCH_RESPONSE_TIMEOUT); }, DISPATCH_RESPONSE_TIMEOUT);
}
}); });
} }