Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Dec 11, 2024
1 parent eaefc68 commit a53a295
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
10 changes: 9 additions & 1 deletion Sources/Verge/Sendable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
final class UnsafeSendableClass<T>: @unchecked Sendable {
var value: T

init(value: T) {
init(_ value: T) {
self.value = value
}
}

struct UnsafeSendableWeak<T: AnyObject>: @unchecked Sendable {
weak var value: T?

init(_ value: T) {
self.value = value
}
}
Expand Down
15 changes: 7 additions & 8 deletions Sources/Verge/SwiftUI/StoreReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,13 @@ public enum StoreReaderComponents<StateType: Equatable> {

}

@MainActor
public final class Node: ObservableObject {

nonisolated public var objectWillChange: ObservableObjectPublisher {
_publisher
}
public let objectWillChange: ObservableObjectPublisher = .init()

/// nil means not loaded first yet
private var detectors: StateProxy.Detectors?

private nonisolated let _publisher: ObservableObjectPublisher = .init()
private var cancellable: StoreStateSubscription?
private let retainValues: [AnyObject]

Expand All @@ -228,9 +224,11 @@ public enum StoreReaderComponents<StateType: Equatable> {

self.currentValue = store.state

cancellable = store.sinkState(queue: .mainIsolated()) { [weak self] state in
let weakSelf = UnsafeSendableWeak(self)

cancellable = store.sinkState(queue: .mainIsolated()) { state in

guard let self else { return }
guard let self = weakSelf.value else { return }

/// retain the latest one
self.currentValue = state
Expand All @@ -254,7 +252,7 @@ public enum StoreReaderComponents<StateType: Equatable> {
if shouldUpdate {
DispatchQueue.main.async {
// For: Publishing changes from within view updates is not allowed, this will cause undefined behavior.
self._publisher.send()
self.objectWillChange.send()
}
}
}
Expand All @@ -276,6 +274,7 @@ public enum StoreReaderComponents<StateType: Equatable> {
#endif
}

@MainActor
func makeContent<Content: View>(@ViewBuilder _ make: @MainActor (inout StateProxy) -> Content)
-> Content
{
Expand Down

0 comments on commit a53a295

Please sign in to comment.