fix: Modal closeAll 异步触发 onClose,修复与 V1 行为不一致的 Breaking Change#2220
Merged
Conversation
PR #1908 (commit 76cba77) 在 closeAll 中新增了同步调用 onClose 的逻辑, 这引发了两个问题: 1. 无限递归 —— 用户在 onClose 中再次调用 closeAll() 会导致栈溢出, 后续 PR #2219 (commit eb355ef) 通过 closingAll 重入守卫修复了此问题。 2. Breaking Change —— 与 V1 行为不一致。 典型场景:用户在 Modal.show 的 content 中通过 onClick 调用 closeAll() + resolve(true),同时在 onClose 中调用 closeAll() + resolve(false)。 V1: closeAll 不触发 onClose → resolve(true) 先执行 → 结果为 true。 V2(同步): closeAll 同步触发 onClose → resolve(false) 抢先执行 → 结果为 false。 修复方案:将 onClose 改为 setTimeout 异步触发。 - 保留 closeAll 触发 onClose 的 feature(通知业务层做清理)。 - closeAll 同步完成所有 close() 调用后,调用者的后续代码先执行, onClose 在下一个事件循环才触发,不会抢占调用者的 resolve。 - close() 同步将 visible 置为 false,onClose 异步执行时即使再调 closeAll(),filter 结果为空数组,天然不会递归,无需重入守卫。
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.
问题背景
PR #1908 (commit 76cba77) 在
closeAll中新增了同步调用onClose的逻辑,这引发了两个问题:问题 1:无限递归(已由 #2219 修复)
用户在
onClose中再次调用closeAll()会导致栈溢出(RangeError: Maximum call stack size exceeded)。#2219 (commit eb355ef) 通过closingAll重入守卫修复了此问题。问题 2:Breaking Change — 与 V1 行为不一致(本 PR 修复)
典型场景:用户在
Modal.show的 content 中通过onClick调用closeAll() + resolve(true),同时在onClose中调用closeAll() + resolve(false)。true✅false❌这对 V1 升级 V2 的用户是一个 Breaking Change。
修复方案
将
onClose改为setTimeout异步触发:为什么这个方案能同时解决两个问题
closeAll仍然触发onClose,业务层的清理逻辑不丢失。closeAll同步完成所有close()调用后,调用者的后续代码(如resolve(true))先执行,onClose在下一个事件循环才触发,不会抢占调用者的 resolve。close()同步将visible置为false,onClose异步执行时即使再调closeAll(),filter 结果为空数组,不会递归。因此移除了closingAll守卫。关联 PR