Cleanup after an effect before executing a new one #25
+56
−3
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.
Pull Request Type
Issue for this PR
Link: N/A
Description
Fixes issue where effects with dependencies (eg
.preserved(by:)
) would cleanup itself after new effect ran. This is different behavior from React's implementation and could cause unexpected results.Motivation and Context
Running the following code with Hooks v0.6.0, after tapping the button,
useHook
would first run new effect, then cleanup after previous effect. The timeline is:Steps 4 and 5 should run in opposite order.
The issue emerges in
UseEffectHook.State
inwillSet
property observer. The observation triggers old effect's cleanup byoldValue?()
by which time new effect already finished to return new cleanup closure.SwiftUI Code
React Code
Running the sample code in
patch-1
branch, the results are same as React app above.Impact on Existing Code
The change is source-compatible.
Screenshot/Video/Gif