Skip to content

fix(shared): make Events#once fire exactly once - #19506

Merged
Single-Dancer merged 2 commits into
NervJS:mainfrom
sprayberry-code:fix/events-once-fires-once
Sep 23, 2026
Merged

Single-Dancer merged 2 commits into
NervJS:mainfrom
sprayberry-code:fix/events-once-fires-once

Conversation

@askalf

@askalf askalf commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Make Events#once detach a listener even when its callback throws.
  • Prevent re-entrant triggers from invoking a one-shot callback more than once.
  • Add focused coverage for string and symbol event names, error paths, and existing listener behavior.

Decisions

A per-listener fired guard closes the re-entrancy window without changing general event dispatch. finally preserves exception propagation while ensuring the listener is removed on the throwing path.

Not run: the full monorepo suite; the focused @tarojs/shared Vitest suite was run on both base and change heads.

AI assistance: this bug was found and the fix and tests were drafted with AI tooling in my workflow; the tests and checks above were executed as pasted. I'm responsible for the change and will handle review feedback.

Summary by CodeRabbit

  • Bug Fixes

    • 修复一次性事件监听器在回调内重复触发时可能执行多次的问题。
    • 修复回调抛出异常后监听器未能正常解绑的问题。
    • 改进一次性监听器的解绑行为,确保不会残留监听状态。
  • Tests

    • 增加对重复触发、异常处理、上下文传递、多事件注册及监听器执行顺序的覆盖测试。

@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 3e5fccf1-b6bb-492d-b36d-a3822274753a

📥 Commits

Reviewing files that changed from the base of the PR and between 608377e and 44fb4c2.

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 30a39795-3ab0-4f8b-87ce-35491d18b64a

📥 Commits

Reviewing files that changed from the base of the PR and between 2dc2036 and 608377e.

📒 Files selected for processing (2)
  • packages/shared/src/event-emitter.ts
  • packages/shared/tests/event-emitter.spec.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

Events.once 新增防重入标记,并在回调完成或抛出异常后解绑。新增测试覆盖普通事件、symbol 事件、多事件名、context、异常和监听器清理行为。

Changes

一次监听器行为

Layer / File(s) Summary
once 防重入与异常解绑
packages/shared/src/event-emitter.ts
once 首次执行前设置 fired。重复触发时直接返回。回调执行后或抛出异常后均通过 finally 调用 off。
once 行为测试
packages/shared/tests/event-emitter.spec.ts
新增测试覆盖参数透传、回调异常、重入、symbol 事件、多事件名、context、执行顺序、重复注册和监听器清理。

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题“fix(shared): make Events#once fire exactly once”准确概括了主要变更,即修复 Events#once,确保监听器只执行一次。标题简洁且具体。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

小兔挥耳检查事件线
once 先亮起防重入灯
回调抛错也会完成解绑
多种事件测试排成行
监听器只跳一次舞

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

once 的 wrapper 先执行回调再调用 off 解绑,回调内如果再次 trigger 同一事件,
wrapper 会在解绑前被重入并无限重复执行;回调抛错时 off 也不会执行,监听器
残留在链上,下一次 trigger 仍会命中。改为用 fired 标记保证回调只执行一次,
并把 off 放进 finally 保证异常路径也会解绑。
覆盖 symbol 事件名、多事件名、传入 context、回调内多次 trigger、
先重入再报错、空回调等路径,并断言回调报错后事件不再残留在监听链上
(这一条用于锁定 finally 中的 off,缺少它时仅靠 fired 标记也能通过)。
另把在修复前后都通过的用例标注为 control。
@Single-Dancer
Single-Dancer force-pushed the fix/events-once-fires-once branch from 608377e to 44fb4c2 Compare September 23, 2026 03:34
@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.63%. Comparing base (57a4427) to head (44fb4c2).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #19506   +/-   ##
=======================================
  Coverage   54.63%   54.63%           
=======================================
  Files         464      464           
  Lines       25391    25391           
  Branches     6348     6325   -23     
=======================================
  Hits        13872    13872           
- Misses       9505     9537   +32     
+ Partials     2014     1982   -32     
Flag Coverage Δ
taro-cli 56.96% <ø> (ø)
taro-runtime 57.88% <ø> (ø)
taro-web 52.37% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 15 files with indirect coverage changes

🚀 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.

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