How to detect if connection with child was lost?
See original GitHub issueHi
I am currently using penpal in a setup at a client but we stumbled upon the following “issue”:
When the original page in the iframe - that has the penpal library - is replaced by a page that does not have the library and we want to do a call to the child, we get this:
[Penpal] Parent: Sending getState() call
So Child has a function getState. Parent asks the child to do the getState function. This all works when the child has the library running, but we don’t get anything when that initial page is changed to another page. It just “hangs” on the “Sending <functionname> call”… No error, no disconnect, nothing.
The code we use is this from the parent to the child. We don’t see any Error from the catch.
connection.current.promise .then((child) => { console.log(" Child doGetSTate ", child); return child .getState() .then((state) => console.log("save state to local storage ")) .catch((error) => console.log("an error occured in getState", error)); }) .catch((error) => console.log(" error occured in doGetState() ", error));
What are we doing wrong? TL;DR: we need to know how to intercept or detect when the connection with the child page is lost, so that we cannot do a call anymore to the child, because it just “hangs”: [Penpal] Parent: Sending getState() call
Thanks!
Mario
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (6 by maintainers)
Thanks for the feedback @yowainwright!
That’s where I was headed. It prompts a few questions:
onConnection
get called on the initial connection and all subsequent connections, or just the subsequent connections?onConnection
. Maybe thechild
object?child
object provided throughconnection.promise
stop functioning after a reconnection? Its API may be different than the newly-connected child API.onConnection
is called on the initial connection and all subsequent connections, does that makeonConnection
redundant toconnection.promise
and should we therefore removeconnection.promise
?I’m thinking it might be more appropriate to have
onDisconnect
(assuming this functionality is feasible) andonReconnect
, whereonReconnect
only gets called on subsequent connections. On a reconnect, a newchild
object would get passed to theonReconnect
callback and thechild
object provided throughconnection.promise
would stop functioning (an error would be thrown if the parent attempts to call its methods).I’m not in love with it though.