Skip to content

fix(v2): preserve listener removal during event dispatch#5218

Draft
leaanthony wants to merge 1 commit intomasterfrom
fix/4393-event-listener-removal
Draft

fix(v2): preserve listener removal during event dispatch#5218
leaanthony wants to merge 1 commit intomasterfrom
fix/4393-event-listener-removal

Conversation

@leaanthony
Copy link
Copy Markdown
Member

Summary

  • Fixes a bug where removing an event listener during its own callback (via cancel function or EventsOff) had no effect because notifyListeners overwrote eventListeners[eventName] with a stale snapshot taken before callbacks ran
  • The fix re-reads the current listener list after all callbacks complete and filters out exhausted listeners, ensuring any removals that happened during dispatch are preserved
  • Rebuilt both debug and production JS runtimes

Test plan

  • New vitest tests verify:
    1. A listener removed via its cancel function during its own callback stays removed on subsequent events
    2. All listeners removed via EventsOff during a callback stay removed on subsequent events
  • All 13 existing + 2 new tests pass

Fixes #4393

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 23, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6807c654-b040-438e-98a6-b54cfad688d7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/4393-event-listener-removal

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

When a listener was removed during its own callback (via the cancel
function returned by EventsOn), notifyListeners would restore it because
it replaced eventListeners[eventName] with a snapshot taken before the
callbacks ran.

Fix by operating on the live listener list directly and checking if each
listener still exists before invoking its callback.

Fixes #4393
@leaanthony leaanthony force-pushed the fix/4393-event-listener-removal branch from e74901a to fd8da51 Compare April 24, 2026 13:20
@leaanthony
Copy link
Copy Markdown
Member Author

🤖 PR Triage Review

Accepted

Preserves listener removal during event dispatch (v2 runtime). Fixes event management bug.

Platform: All (v2, runtime)

Next Steps: Dispatching for testing on all platforms.


Reviewed by Wails PR Reviewer Bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Removing an event while handling an event leave the event handler on

1 participant