Skip to content
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

Force store state change onto main actor #34

Open
gordonbrander opened this issue May 16, 2023 · 0 comments · May be fixed by #35
Open

Force store state change onto main actor #34

gordonbrander opened this issue May 16, 2023 · 0 comments · May be fixed by #35
Labels
enhancement New feature or request

Comments

@gordonbrander
Copy link
Collaborator

gordonbrander commented May 16, 2023

View state in SwiftUI must only be changed on the main thread (nee main actor). We currently force effects onto the main thread through .receive(on: DispatchQueue.main). However, we do not force send on to the main thread, relying on callers to do the right thing, and SwiftUI to complain if you don't.

It might be nice to force send onto the main thread to make changing state off main thread impossible. We might do this through decorating Store with @MainActor or perhaps through decorating send with @MainActor.

Code Sketch

public protocol StoreProtocol {
    associatedtype Model: ModelProtocol

    @MainActor var state: Model { get }

    @MainActor func send(_ action: Model.Action) -> Void
}

@MainActor
public final class Store<Model>: ObservableObject, StoreProtocol
where Model: ModelProtocol
{
    // ...
}

Tested in-app on 2023-07-07. No major problems, but a few Converting function value of type '@MainActor (NotebookAction) -> ()' to '(NotebookAction) -> Void' loses global actor 'MainActor'. We may want to make send nonisolated.

@gordonbrander gordonbrander added the enhancement New feature or request label May 16, 2023
gordonbrander added a commit that referenced this issue Jul 7, 2023
@gordonbrander gordonbrander linked a pull request Jul 7, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant