Skip to content

Fix: remove window message listener leak via new destroy() method - #64

Merged
yashschandra merged 1 commit into
mainfrom
INAPP-14608
Aug 5, 2026
Merged

yashschandra merged 1 commit into
mainfrom
INAPP-14608

Conversation

@yashschandra

Copy link
Copy Markdown
Contributor

Summary

  • The Framecast constructor passed a fresh this.handlePostedMessage.bind(this) to both removeEventListener and addEventListener, so the removal never matched and every instance leaked its message listener on the window for the life of the page.
  • The constructor now keeps a single boundHandlePostedMessage on the instance, and a new destroy() method removes the listener, rejects pending call()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.
  • After destroy(), postMessage becomes a no-op instead of posting to a stale window.
  • Ported from Fix: remove window message listener leak via new destroy() method framecast#20, adapted for this package's vitest setup and broadcast-queueing feature (destroy() also calls clearQueue()).

Changes

  • packages/framecast/src/index.ts — store boundHandlePostedMessage once; drop the ineffective constructor removeEventListener; add destroy(); guard postMessage when target is null; make target nullable.
  • packages/framecast/src/index.test.ts — new destroy suite 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 public destroy() API).

Test plan

  • pnpm --filter @ciolabs/framecast test — all 43 tests pass, including the new destroy suite
  • pnpm --filter @ciolabs/framecast build — package builds cleanly (ESM/CJS/DTS)
  • Create/destroy several Framecast instances against the same window and confirm no message listeners remain
  • Call framecast.call(...) then destroy() and confirm the promise rejects once with "Framecast destroyed"

🤖 Generated with Claude Code

… 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-bot

changeset-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3e68e91

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@ciolabs/framecast Minor

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

@yashschandra
yashschandra requested a review from avigoldman August 4, 2026 14:38
@yashschandra
yashschandra merged commit 7ee5c73 into main Aug 5, 2026
3 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants