Change AppBridge interval to timeout (#128)
This commit is contained in:
parent
063e56f345
commit
b39081ed9a
2 changed files with 40 additions and 39 deletions
BIN
.DS_Store
vendored
Normal file
BIN
.DS_Store
vendored
Normal file
Binary file not shown.
|
@ -200,49 +200,50 @@ 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);
|
}
|
||||||
|
|
||||||
window.parent.postMessage(
|
debug("Calling window.parent.postMessage with %j", action);
|
||||||
{
|
|
||||||
type: action.type,
|
window.parent.postMessage(
|
||||||
payload: action.payload,
|
{
|
||||||
},
|
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;
|
if (action.payload.actionId === actionId) {
|
||||||
|
debug("Received matching action id: %s. Will unsubscribe", actionId);
|
||||||
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(() => {
|
|
||||||
unsubscribe();
|
unsubscribe();
|
||||||
reject(new Error("Action response timed out."));
|
clearTimeout(timeoutId);
|
||||||
}, DISPATCH_RESPONSE_TIMEOUT);
|
|
||||||
}
|
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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue