Aaronius / penpal

A promise-based library for securely communicating with iframes via postMessage.
MIT License
389 stars 56 forks source link

Adding re-handshaking functionality #8

Closed michael-simon closed 7 years ago

michael-simon commented 7 years ago

This modifies the Parent's message listener to listen for 'HANDSHAKE' messages. When it receives one, it attempts to execute the steps of a HANDSHAKE again. This allows for Penpal to be used in cases where where iframe.load is called before penpal is loaded, such as with asynchronous loading (System.js)

As we thought about this, we realized that maybe we should not be waiting on iframe load, but rather module load, to trigger the handshake from the parent. This doesn't get there yet, but this starts to illustrate where that idea would end up. This also doesn't involve changing the function interfaces, so it's a non-intrusive start.

Aaronius commented 7 years ago

Thanks for contributing this. 👍 Before merging it I'm going to test it out on some of our trickier setups.

michael-simon commented 7 years ago

Seems reasonable. The parent/source equality check I added could break in a world where you are trying to send messages to an instantiated child from multiple instantiated parents, but that doesn't actually match the implicit design of the module (although I could see it being a use case in truly difficult apps where you're less 'parent-child' and more 'parent and a bunch of intercommunicating children from different origins and have hacked it in via this method', but if you're gonna do that, it seems to me there should be a different interface for that sort of setup.)

Aaronius commented 7 years ago

Released in 2.6.0 :tada: