Repository navigation
feat: add Player.destroy() for unmounting players - #111
Merged
Merged
Conversation
A listener-less 'ready' from any iframe on the same origin fired every Player's ready callbacks, even Players whose own iframe wasn't ready. The README warns about this as "inconsistencies if you have multiple players on the page". It also woke Players whose iframe had been removed: remounting an embed with the same src made the old Player pick up the new iframe's ready and, before #94, crash sending its queue to the removed iframe (#93, #75). Only handle 'ready' when it comes from this Player's own iframe, and never from an iframe that is no longer in the page. Other events are unchanged.
Each Player added a window message listener that could never be removed, so a Player whose iframe was gone stayed alive and kept handling messages. destroy() removes that listener and makes later calls no-ops. If the iframe stays on the page, call off() first so it stops sending events.
luwes
force-pushed
the
feat/player-destroy
branch
from
November 3, 2026 05:59
96a2424 to
340510b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
player.destroy()so apps can tear down a player when its iframe goes away, for example when a React or Vue component unmounts. It's additive, and nothing new goes overpostMessage. Two commits:1.
fix: ignore ready from other or removed iframesA listener-less
readyfrom any iframe on the same origin fired every player'sreadycallbacks, even for players whose own iframe wasn't ready. This is the "inconsistencies if you have multiple players on the page" the README warns about. It also woke players whose iframe had been removed: remounting an embed with the samesrcmade the old player pick up the new iframe'sreadyand, before #94, crash withCannot read properties of null (reading 'postMessage')(#93, #75).readyis now handled only when it comes from this player's own iframe, and never from an iframe that is no longer in the page. Other events are unchanged.2.
feat: add Player.destroy() for unmounting playersEach player adds a
windowmessagelistener that could never be removed, so a player whose iframe was gone stayed alive and kept handling messages.destroy()is the minimum needed to stop that:initkeeps a reference to the listener (this._onMessage), andsend()returnsfalseonce destroyed, so later calls never reach the iframe. Once the app drops its reference, the player can be garbage-collected.What it deliberately doesn't do: unregister listeners inside the iframe. Usually the iframe is removed right after, which makes that pointless. If the iframe stays on the page, call
off()first, as the README documents.Tests
src, withoutdestroy()and after it: no page errors, the old player's callbacks don't fire, and the new player works.destroy(): no callbacks fire, the window listener is gone (receiveis never called),send()returnsfalse, and no calls reach the iframe. Calling it twice is safe.destroy()before ready: queued calls are never sent.readydoesn't fire the other player'sready.Removing either line of
destroy()makes a test fail. The full suite passes with 72 tests, and the related tests passed 70 of 70 repeated runs.README documents
destroy()with a React example.Related: #45, a closed report of methods called in
readyhaving no effect after replacing the iframe, as if they went "to the player in the iframe I removed". That fits thereadyfix in commit 1.Closes #93
Closes #75