Repository navigation
Fix: remove window message listener leak via new destroy() method - #64
Merged
Merged
Conversation
… method The constructor passed a fresh handlePostedMessage.bind(this) to both removeEventListener and addEventListener, so the removal never matched and every Framecast instance leaked its message listener for the life of the page. Keep a single bound handler on the instance and add a destroy() method that removes the listener, rejects pending call()s with a "Framecast destroyed" error, clears broadcast listeners, discards queued broadcasts, and nulls the target window so a detached iframe can be garbage collected. postMessage becomes a no-op after destroy. Ported from customerio/framecast#20. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 3e68e91 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
avigoldman
approved these changes
Aug 4, 2026
Merged
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.
Summary
Framecastconstructor passed a freshthis.handlePostedMessage.bind(this)to bothremoveEventListenerandaddEventListener, so the removal never matched and every instance leaked itsmessagelistener on the window for the life of the page.boundHandlePostedMessageon the instance, and a newdestroy()method removes the listener, rejects pendingcall()s with a "Framecast destroyed" error (clearing their timeouts), clears broadcast listeners, discards queued broadcasts, and nulls the target window so a detached iframe can be garbage collected.destroy(),postMessagebecomes a no-op instead of posting to a stale window.destroy()also callsclearQueue()).Changes
packages/framecast/src/index.ts— storeboundHandlePostedMessageonce; drop the ineffective constructorremoveEventListener; adddestroy(); guardpostMessagewhentargetis null; maketargetnullable.packages/framecast/src/index.test.ts— newdestroysuite covering listener add/remove, no accumulation across create/destroy cycles, pending-call rejection with timeout cleanup, post-destroy no-op posting, and queued-broadcast discard; replaced the construction test that asserted the old (broken) remove-before-add behavior..changeset/framecast-destroy-listener-leak.md— minor release for@ciolabs/framecast(new publicdestroy()API).Test plan
pnpm --filter @ciolabs/framecast test— all 43 tests pass, including the newdestroysuitepnpm --filter @ciolabs/framecast build— package builds cleanly (ESM/CJS/DTS)messagelisteners remainframecast.call(...)thendestroy()and confirm the promise rejects once with "Framecast destroyed"🤖 Generated with Claude Code