Skip to content

Commit 1ccf087

Browse files
authored
Revert to use StateObject instead of State in ViewContext (#162)
1 parent f0f0f37 commit 1ccf087

File tree

1 file changed

+24
-50
lines changed

1 file changed

+24
-50
lines changed

Sources/Atoms/PropertyWrapper/ViewContext.swift

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,46 @@ import SwiftUI
3232
///
3333
@propertyWrapper
3434
public struct ViewContext: DynamicProperty {
35+
private let file: StaticString
36+
private let location: SourceLocation
37+
3538
@Environment(\.store)
3639
private var _store
3740

38-
private let file: StaticString
39-
private let location: SourceLocation
41+
@StateObject
42+
private var state = State()
4043

4144
/// Creates a view context.
4245
public init(file: StaticString = #file, fileID: String = #fileID, line: UInt = #line) {
4346
self.file = file
4447
self.location = SourceLocation(fileID: fileID, line: line)
4548
}
4649

50+
/// The underlying view context to interact with atoms.
51+
///
52+
/// This property provides primary access to the view context. However you don't
53+
/// access ``wrappedValue`` directly.
54+
/// Instead, you use the property variable created with the `@ViewContext` attribute.
4755
#if compiler(>=6) || hasFeature(DisableOutwardActorInference)
48-
@State
49-
private var signal = false
50-
@State
51-
private var subscriberState = SubscriberState()
52-
53-
/// The underlying view context to interact with atoms.
54-
///
55-
/// This property provides primary access to the view context. However you don't
56-
/// access ``wrappedValue`` directly.
57-
/// Instead, you use the property variable created with the `@ViewContext` attribute.
5856
@MainActor
59-
public var wrappedValue: AtomViewContext {
60-
let signal = _signal
61-
62-
// Initializes State and starts observing for updates.
63-
_ = signal.wrappedValue
64-
65-
return AtomViewContext(
66-
store: store,
67-
subscriber: Subscriber(subscriberState),
68-
subscription: Subscription(location: location) {
69-
signal.wrappedValue.toggle()
70-
}
71-
)
72-
}
73-
74-
#else
75-
@MainActor
76-
private final class State: ObservableObject {
77-
let subscriberState = SubscriberState()
78-
}
79-
80-
@StateObject
81-
private var state = State()
82-
83-
/// The underlying view context to interact with atoms.
84-
///
85-
/// This property provides primary access to the view context. However you don't
86-
/// access ``wrappedValue`` directly.
87-
/// Instead, you use the property variable created with the `@ViewContext` attribute.
88-
public var wrappedValue: AtomViewContext {
89-
AtomViewContext(
90-
store: store,
91-
subscriber: Subscriber(state.subscriberState),
92-
subscription: Subscription(location: location) { [weak state] in
93-
state?.objectWillChange.send()
94-
}
95-
)
96-
}
9757
#endif
58+
public var wrappedValue: AtomViewContext {
59+
AtomViewContext(
60+
store: store,
61+
subscriber: Subscriber(state.subscriberState),
62+
subscription: Subscription(location: location) { [weak state] in
63+
state?.objectWillChange.send()
64+
}
65+
)
66+
}
9867
}
9968

10069
private extension ViewContext {
70+
@MainActor
71+
final class State: ObservableObject {
72+
let subscriberState = SubscriberState()
73+
}
74+
10175
@MainActor
10276
var store: StoreContext {
10377
guard let _store else {

0 commit comments

Comments
 (0)