From b39081ed9a7bbd36c89d23ee0cc9ab1049ee6abd Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Fri, 2 Dec 2022 16:08:39 +0100 Subject: [PATCH] Change AppBridge interval to timeout (#128) --- .DS_Store | Bin 0 -> 6148 bytes src/app-bridge/app-bridge.ts | 79 ++++++++++++++++++----------------- 2 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..879bd63f75ab25cdf263f31bfa330555c8265614 GIT binary patch literal 6148 zcmeHKF;2ul47A}SPNLvS%KZXAh)zdA%?G&T5CxHfgy_@pY?xV_6VZZ1MFC^Uo~+li zn@kb!nwf834p(LyGh4%n_R7#2=jn}|WQOQ17t*t&h4`V+~Kiv-t$fST2kOERb3jAvY(6edl^FT!@AO)nrQ33uR3Y^#_ z&VlvUfxzQC=T}4;p6gu#SaRGY&ViVLc~F5t^}J$u(2*}$*Coz@K{u~EW1ZaD^M>Me zcjQYgH}3)!rGOMzDsURZ4g3E!{G0p#lBAIokOF^70blKIcUyc?_SWX(*lP>?4*p@N mt>p-dLjVSB#h0Jz3ZGHeCC-7Bj(nv9br7H~GAZyI3cLg9o+6h3 literal 0 HcmV?d00001 diff --git a/src/app-bridge/app-bridge.ts b/src/app-bridge/app-bridge.ts index 7576bee..25527dd 100644 --- a/src/app-bridge/app-bridge.ts +++ b/src/app-bridge/app-bridge.ts @@ -200,49 +200,50 @@ export class AppBridge { debug("window.parent doesnt exist, will throw"); reject(new Error("Parent window does not exist.")); - } else { - debug("Calling window.parent.postMessage with %j", action); + return; + } - window.parent.postMessage( - { - type: action.type, - payload: action.payload, - }, - "*" + debug("Calling window.parent.postMessage with %j", action); + + window.parent.postMessage( + { + type: action.type, + payload: action.payload, + }, + "*" + ); + + let timeoutId: number; + + const unsubscribe = this.subscribe(EventType.response, ({ actionId, ok }) => { + debug( + "Subscribing to %s with action id: %s and status 'ok' is: %s", + EventType.response, + actionId, + ok ); - let intervalId: number; - - const unsubscribe = this.subscribe(EventType.response, ({ actionId, ok }) => { - debug( - "Subscribing to %s with action id: %s and status 'ok' is: %s", - EventType.response, - actionId, - ok - ); - - if (action.payload.actionId === actionId) { - debug("Received matching action id: %s. Will unsubscribe", actionId); - unsubscribe(); - clearInterval(intervalId); - - if (ok) { - resolve(); - } else { - reject( - new Error( - "Action responded with negative status. This indicates the action method was not used properly." - ) - ); - } - } - }); - - intervalId = window.setInterval(() => { + if (action.payload.actionId === actionId) { + debug("Received matching action id: %s. Will unsubscribe", actionId); unsubscribe(); - reject(new Error("Action response timed out.")); - }, DISPATCH_RESPONSE_TIMEOUT); - } + clearTimeout(timeoutId); + + if (ok) { + resolve(); + } else { + reject( + new Error( + "Action responded with negative status. This indicates the action method was not used properly." + ) + ); + } + } + }); + + timeoutId = window.setTimeout(() => { + unsubscribe(); + reject(new Error("Action response timed out.")); + }, DISPATCH_RESPONSE_TIMEOUT); }); }