Repository navigation
fix(core): 降级保活恢复事件监听时与原生语义对齐(区分 capture、once 触发后不再恢复) - #1120
Merged
Merged
Conversation
- 以 type + handler + capture 识别监听,同一 handler 的 capture/bubble 分别记录与移除 - once 监听触发后同步清理缓存记录,切回后不再重新绑定;触发前被移除时一并移除内部清理监听 - 恢复监听时重新注册 once 的清理监听,多次切换后行为保持一致 Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
#997 修复了降级 + 保活模式下
removeEventListener误删缓存的问题。但缓存识别监听时只看type + handler,和浏览器原生语义(type + handler + capture,以及once触发后自动移除)不一致。切走再切回、恢复监听后,以下场景的行为和原生不同:once监听触发后切回再点击修改
recordEventListeners:以type + handler + capture识别同一个监听,options无论是布尔值还是对象,都统一归一化为 capture 后比较。once监听,额外注册一个内部的{ once, capture }清理监听,事件触发时删除对应的缓存记录;不包装业务回调。清理监听注册在业务监听之前:业务监听被更早的stopImmediatePropagation拦截时(原生不会消耗这次 once),清理也不会执行,缓存记录得以保留。once监听在触发前被removeEventListener移除时,一并移除内部清理监听。recoverEventListeners/recoverDocumentListeners恢复监听时,会重新注册 once 的清理监听,保证多次切换后行为一致。EventListenerRecord类型,替换三处重复的内联类型。验证
__test__/unit/event-listener-cache.test.ts(6 个用例):修复前全部失败,修复后全部通过。{once:1,both:2,captureRemoved:1}→{once:2,both:3,captureRemoved:1}→{once:3,both:4,captureRemoved:1}{once:1,both:2,captureRemoved:1}→{once:1,both:4,captureRemoved:2}→{once:1,both:6,captureRemoved:3}Follow-up of #997