fix(toast): resolve singleton race condition on rapid calls#998
Merged
Pilotager merged 4 commits intomallfoundry:mainfrom Mar 6, 2026
Merged
Conversation
Add a synchronous pendingToastSelectorSet to track toasts being mounted before useEffect completes registration. This closes the async window where rapid successive openToast calls all see an empty toastSelectorSet and incorrectly create multiple Toast instances. Also fix the allowMultiple condition logic which was equivalent to !hasExistingToast regardless of the _isMultipleAllowed flag. Fixes mallfoundry#997
Contributor
Author
|
我已经把pr patch到本地, 这是测试结果 iShot_2026-03-04_10.37.27.mp4 |
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.
问题
关联 Issue:#997
Toast 默认为单例模式,但快速连续调用时会出现多个 Toast 叠加,单例机制失效。
根因
存在两个 Bug:
Bug 1:条件逻辑错误
Bug 2:异步注册导致竞态窗口(Race Condition)
Toast 组件在
useEffect中将自身注册到toastSelectorSet,这是异步操作(渲染完成后才执行)。快速点击时,第二次openToast调用发生在第一个组件的useEffect执行之前,此时toastSelectorSet仍为空,hasExistingToast = false,导致再次创建新实例叠加。修复
修复条件逻辑:改为
if (_isMultipleAllowed || !hasExistingToast),正确区分单例/多例行为。增加同步
pendingToastSelectorSet:在调用mountPortal之前同步记录正在挂载的 Toast,堵住useEffect异步注册的时序窗口。Toast 完全卸载后(onTransitionExited)再从该 Set 中清除。