Skip to content

fix(eventEmitter)!: improve event handling and listener management#2277

Merged
andrii-bodnar merged 6 commits intolingui:nextfrom
yslpn:fix/not-add-same-listener-multiple-times
Feb 9, 2026
Merged

fix(eventEmitter)!: improve event handling and listener management#2277
andrii-bodnar merged 6 commits intolingui:nextfrom
yslpn:fix/not-add-same-listener-multiple-times

Conversation

@yslpn
Copy link
Contributor

@yslpn yslpn commented Jul 2, 2025

Description

  1. I expect that the same function will not be added twice. To fix this, I switched from arrays to Set.
  2. Ensures that the event invocation cycle will not be broken if the handlers themselves start changing the list of listeners. For example, adding or removing.

Types of changes

Refactored EventEmitter + add two new tests

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Examples update

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

@vercel
Copy link

vercel bot commented Jul 2, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
js-lingui Ready Ready Preview Feb 9, 2026 9:00am

Request Review

@codecov
Copy link

codecov bot commented Jul 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.75%. Comparing base (dd43fb0) to head (928380e).
⚠️ Report is 300 commits behind head on next.

Additional details and impacted files
@@             Coverage Diff             @@
##             next    #2277       +/-   ##
===========================================
+ Coverage   76.66%   88.75%   +12.08%     
===========================================
  Files          81      117       +36     
  Lines        2083     3281     +1198     
  Branches      532      966      +434     
===========================================
+ Hits         1597     2912     +1315     
+ Misses        375      331       -44     
+ Partials      111       38       -73     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Collaborator

@vonovak vonovak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello and thanks for the PR!

Firstly, may I ask what's the motivation for the PR?

I'd say it can be merged, but for v6 - the change of behavior, while subtle, is a breaking change. Also the removal of removeListener is a breaking change - it should be added back.

Thank you :)

@yslpn
Copy link
Contributor Author

yslpn commented Jul 12, 2025

Hello and thanks for the PR!

Firstly, may I ask what's the motivation for the PR?

I'd say it can be merged, but for v6 - the change of behavior, while subtle, is a breaking change. Also the removal of removeListener is a breaking change - it should be added back.

Thank you :)

Hi, I noticed this behavior when experimenting with my own implementations in React. I think this behavior improves stability, even in the worst case.

I think this change can be merged into version 6. Not urgent.

@andrii-bodnar andrii-bodnar marked this pull request as draft July 17, 2025 10:50
@yslpn yslpn marked this pull request as ready for review December 22, 2025 21:03
@andrii-bodnar andrii-bodnar added this to the v6 milestone Jan 29, 2026
@andrii-bodnar andrii-bodnar removed the v6 label Feb 6, 2026
@andrii-bodnar
Copy link
Contributor

@yslpn could you please rebase this PR onto the next branch?

@vercel
Copy link

vercel bot commented Feb 6, 2026

@yslpn is attempting to deploy a commit to the Crowdin Team on Vercel.

A member of the Team first needs to authorize it.

@yslpn
Copy link
Contributor Author

yslpn commented Feb 6, 2026

@yslpn could you please rebase this PR onto the next branch?

Done.

Could you change the target branch to next? Either I don't have the necessary permissions or I can't find the button :)

@andrii-bodnar andrii-bodnar changed the base branch from main to next February 6, 2026 10:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the core EventEmitter implementation to improve listener management (deduping) and to make listener invocation resilient to modifications during an emit cycle, with accompanying tests to lock in the new behaviors.

Changes:

  • Switch internal listener storage from Array to Set to avoid duplicate listener registrations.
  • Snapshot listeners during emit to prevent iteration from being affected by add/remove operations inside handlers.
  • Add tests covering “remove during emit” behavior and duplicate listener prevention.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/core/src/eventEmitter.ts Refactors listener storage to Set and updates emit iteration to use a snapshot.
packages/core/src/eventEmitter.test.ts Adds tests for emit-cycle mutation safety and listener deduplication behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Collaborator

@vonovak vonovak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR, see my other comment

@yslpn yslpn requested a review from vonovak February 7, 2026 10:57
@andrii-bodnar andrii-bodnar changed the title fix(eventEmitter): improve event handling and listener management fix(eventEmitter)!: improve event handling and listener management Feb 9, 2026
@andrii-bodnar andrii-bodnar merged commit 5794bbf into lingui:next Feb 9, 2026
10 checks passed
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.

5 participants