-
-
Notifications
You must be signed in to change notification settings - Fork 17
Fix the issue where AtomEffectBuilder fails to demangle witness for the resulting type of multiple blocks #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+50
−69
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,17 +19,19 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| @MainActor | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| @resultBuilder | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public enum AtomEffectBuilder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static func buildBlock() -> some AtomEffect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| EmptyEffect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static func buildBlock<Effect: AtomEffect>(_ effect: Effect) -> Effect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| effect | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static func buildBlock<each Effect: AtomEffect>(_ effect: repeat each Effect) -> some AtomEffect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if #available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return BlockEffect(repeat each effect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return BlockBCEffect(repeat each effect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // NB: The combination of variadic type and opaque return type in iOS 16 fails to demangle | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // the witness for the return type, which causes a runtime crash. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Once iOS 16 support is dropped, this function will be changed to return an opaque type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static func buildBlock<each Effect: AtomEffect>(_ effect: repeat each Effect) -> BlockEffect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| BlockEffect(repeat each effect) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static func buildIf<Effect: AtomEffect>(_ effect: Effect?) -> some AtomEffect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -54,6 +56,46 @@ public enum AtomEffectBuilder { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| public extension AtomEffectBuilder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Use type pack once it is available in iOS 17 or newer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| // struct BlockEffect<each Effect: AtomEffect>: AtomEffect | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| struct BlockEffect: AtomEffect { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private let _initializing: @MainActor (Context) -> Void | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private let _initialized: @MainActor (Context) -> Void | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private let _updated: @MainActor (Context) -> Void | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| private let _released: @MainActor (Context) -> Void | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| internal init<each Effect: AtomEffect>(_ effect: repeat each Effect) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _initializing = { context in | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| repeat (each effect).initializing(context: context) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _initialized = { context in | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| repeat (each effect).initialized(context: context) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _updated = { context in | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| repeat (each effect).updated(context: context) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _released = { context in | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| repeat (each effect).released(context: context) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+68
to
+79
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| _initializing = { context in | |
| repeat (each effect).initializing(context: context) | |
| } | |
| _initialized = { context in | |
| repeat (each effect).initialized(context: context) | |
| } | |
| _updated = { context in | |
| repeat (each effect).updated(context: context) | |
| } | |
| _released = { context in | |
| repeat (each effect).released(context: context) | |
| } | |
| _initializing = createEffectHandler(effect: repeat each effect, method: { $0.initializing(context:) }) | |
| _initialized = createEffectHandler(effect: repeat each effect, method: { $0.initialized(context:) }) | |
| _updated = createEffectHandler(effect: repeat each effect, method: { $0.updated(context:) }) | |
| _released = createEffectHandler(effect: repeat each effect, method: { $0.released(context:) }) | |
| } | |
| private func createEffectHandler<each Effect: AtomEffect>( | |
| effect: repeat each Effect, | |
| method: @escaping (Effect) -> (Context) -> Void | |
| ) -> @MainActor (Context) -> Void { | |
| return { context in | |
| repeat method(each effect)(context) | |
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider annotating
BlockEffect(and its initializer and methods) with@MainActorto enforce actor isolation and prevent potential threading issues, aligning with the@MainActorcontext ofAtomEffectBuilder.