Repository navigation
fix(runtime): 修复移除未注册事件处理器时误删已有监听器 - #19529
Conversation
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes. ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
Walkthrough
Changes事件监听器移除
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~8 minutes Change: Bug fix · Severity of issue fixed: Low Merge Risk: ⚪ Minimal · up to
|
31b2275 to
3299e7e
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #19529 +/- ##
========================================
Coverage 55.35% 55.36%
========================================
Files 467 467
Lines 25650 25652 +2
Branches 6426 6439 +13
========================================
+ Hits 14199 14201 +2
+ Misses 9573 9454 -119
- Partials 1878 1997 +119
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
这个 PR 做了什么?
修复
TaroEventTarget.removeEventListener()在找不到指定事件处理器时误删已有监听器的问题。问题
当事件类型已经注册了监听器,但传入的处理器未注册时,
findIndex()返回-1。当前实现虽然会在开发环境发出警告,随后仍执行handlers.splice(-1, 1),导致最后一个有效监听器被移除。重复移除同一个处理器也会触发此问题,生产环境同样受影响。例如,先注册处理器 A、B:
修复
在现有警告之后增加
index === -1的提前返回,保证未匹配的移除操作保留现有监听器。匹配成功时继续执行原有移除流程,保留处理器包装匹配、事件名小写化以及
once自动移除行为。新增判断为 O(1),整体仍为 O(H),H 为对应事件类型的处理器数量。回归测试
在
packages/taro-runtime/tests/event.spec.ts中增加 3 个用例:前两个用例同时验证监听器数组引用及内容保持一致,且节点模板不会切换。
验证
pnpm --filter @tarojs/runtime test:ci:11 个测试文件通过,137 个用例通过、1 个跳过。NODE_ENV=production pnpm --filter @tarojs/runtime test tests/event.spec.ts -t '移除未注册的 handler|重复移除同一个 handler':3 个回归用例通过。pnpm --filter @tarojs/shared build:通过。pnpm --filter @tarojs/runtime build:通过。pnpm exec tsc --project packages/taro-runtime/tsconfig.json --noEmit:通过。pnpm exec eslint packages/taro-runtime/src/dom/event-target.ts packages/taro-runtime/tests/event.spec.ts:通过。git diff --check:通过。这个 PR 是什么类型?
这个 PR 涉及以下平台
Summary by CodeRabbit