Skip to content

Commit 37f74c7

Browse files
committed
Use State over StateObject where possible
1 parent 81a4d06 commit 37f74c7

File tree

3 files changed

+53
-70
lines changed

3 files changed

+53
-70
lines changed

Sources/Atoms/AtomRoot.swift

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import SwiftUI
6161
/// ```
6262
///
6363
public struct AtomRoot<Content: View>: View {
64-
private var storeHost: StoreHost
64+
private var storage: Storage
6565
private var overrides = [OverrideKey: any OverrideProtocol]()
6666
private var observers = [Observer]()
6767
private let content: Content
@@ -70,7 +70,7 @@ public struct AtomRoot<Content: View>: View {
7070
///
7171
/// - Parameter content: The descendant view content that provides context for atoms.
7272
public init(@ViewBuilder content: () -> Content) {
73-
self.storeHost = .tree
73+
self.storage = .managed
7474
self.content = content()
7575
}
7676

@@ -84,26 +84,26 @@ public struct AtomRoot<Content: View>: View {
8484
storesIn store: AtomStore,
8585
@ViewBuilder content: () -> Content
8686
) {
87-
self.storeHost = .unmanaged(store: store)
87+
self.storage = .unmanaged(store: store)
8888
self.content = content()
8989
}
9090

9191
/// The content and behavior of the view.
9292
public var body: some View {
93-
switch storeHost {
94-
case .tree:
95-
TreeManaged(
96-
content: content,
93+
switch storage {
94+
case .managed:
95+
Managed(
9796
overrides: overrides,
98-
observers: observers
97+
observers: observers,
98+
content: content
9999
)
100100

101101
case .unmanaged(let store):
102102
Unmanaged(
103-
content: content,
104103
store: store,
105104
overrides: overrides,
106-
observers: observers
105+
observers: observers,
106+
content: content
107107
)
108108
}
109109
}
@@ -150,31 +150,27 @@ public struct AtomRoot<Content: View>: View {
150150
}
151151

152152
private extension AtomRoot {
153-
enum StoreHost {
154-
case tree
153+
enum Storage {
154+
case managed
155155
case unmanaged(store: AtomStore)
156156
}
157157

158-
struct TreeManaged: View {
159-
@MainActor
160-
final class State: ObservableObject {
161-
let store = AtomStore()
162-
let token = ScopeKey.Token()
163-
}
164-
165-
let content: Content
158+
struct Managed: View {
166159
let overrides: [OverrideKey: any OverrideProtocol]
167160
let observers: [Observer]
161+
let content: Content
168162

169-
@StateObject
170-
private var state = State()
163+
@State
164+
private var store = AtomStore()
165+
@State
166+
private var token = ScopeKey.Token()
171167

172168
var body: some View {
173169
content.environment(
174170
\.store,
175171
StoreContext(
176-
store: state.store,
177-
scopeKey: ScopeKey(token: state.token),
172+
store: store,
173+
scopeKey: ScopeKey(token: token),
178174
inheritedScopeKeys: [:],
179175
observers: observers,
180176
scopedObservers: [],
@@ -186,25 +182,20 @@ private extension AtomRoot {
186182
}
187183

188184
struct Unmanaged: View {
189-
@MainActor
190-
final class State: ObservableObject {
191-
let token = ScopeKey.Token()
192-
}
193-
194-
let content: Content
195185
let store: AtomStore
196186
let overrides: [OverrideKey: any OverrideProtocol]
197187
let observers: [Observer]
188+
let content: Content
198189

199-
@StateObject
200-
private var state = State()
190+
@State
191+
private var token = ScopeKey.Token()
201192

202193
var body: some View {
203194
content.environment(
204195
\.store,
205196
StoreContext(
206197
store: store,
207-
scopeKey: ScopeKey(token: state.token),
198+
scopeKey: ScopeKey(token: token),
208199
inheritedScopeKeys: [:],
209200
observers: observers,
210201
scopedObservers: [],

Sources/Atoms/AtomScope.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,19 @@ public struct AtomScope<Content: View>: View {
8787
public var body: some View {
8888
switch inheritance {
8989
case .environment(let id):
90-
InheritedEnvironment(
90+
WithEnvironment(
9191
id: id,
92-
content: content,
9392
overrides: overrides,
94-
observers: observers
93+
observers: observers,
94+
content: content
9595
)
9696

9797
case .context(let store):
98-
InheritedContext(
99-
content: content,
98+
WithContext(
10099
store: store,
101100
overrides: overrides,
102-
observers: observers
101+
observers: observers,
102+
content: content
103103
)
104104
}
105105
}
@@ -158,27 +158,22 @@ private extension AtomScope {
158158
case context(store: StoreContext)
159159
}
160160

161-
struct InheritedEnvironment: View {
162-
@MainActor
163-
final class State: ObservableObject {
164-
let token = ScopeKey.Token()
165-
}
166-
161+
struct WithEnvironment: View {
167162
let id: ScopeID
168-
let content: Content
169163
let overrides: [OverrideKey: any OverrideProtocol]
170164
let observers: [Observer]
165+
let content: Content
171166

172-
@StateObject
173-
private var state = State()
167+
@State
168+
private var token = ScopeKey.Token()
174169
@Environment(\.store)
175170
private var environmentStore
176171

177172
var body: some View {
178173
content.environment(
179174
\.store,
180175
environmentStore?.scoped(
181-
scopeKey: ScopeKey(token: state.token),
176+
scopeKey: ScopeKey(token: token),
182177
scopeID: id,
183178
observers: observers,
184179
overrides: overrides
@@ -187,11 +182,11 @@ private extension AtomScope {
187182
}
188183
}
189184

190-
struct InheritedContext: View {
191-
let content: Content
185+
struct WithContext: View {
192186
let store: StoreContext
193187
let overrides: [OverrideKey: any OverrideProtocol]
194188
let observers: [Observer]
189+
let content: Content
195190

196191
var body: some View {
197192
content.environment(

Sources/Atoms/PropertyWrapper/ViewContext.swift

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ import SwiftUI
3232
///
3333
@propertyWrapper
3434
public struct ViewContext: DynamicProperty {
35-
@StateObject
36-
private var state = State()
37-
35+
@State
36+
private var signal = false
37+
@State
38+
private var subscriberState = SubscriberState()
3839
@Environment(\.store)
3940
private var _store
4041

@@ -52,29 +53,25 @@ public struct ViewContext: DynamicProperty {
5253
/// This property provides primary access to the view context. However you don't
5354
/// access ``wrappedValue`` directly.
5455
/// Instead, you use the property variable created with the `@ViewContext` attribute.
55-
#if compiler(>=6) || hasFeature(DisableOutwardActorInference)
56-
@MainActor
57-
#endif
56+
57+
@MainActor
5858
public var wrappedValue: AtomViewContext {
59-
AtomViewContext(
59+
let signal = _signal
60+
61+
// Initializes State and starts observing for updates.
62+
_ = signal.wrappedValue
63+
64+
return AtomViewContext(
6065
store: store,
61-
subscriber: Subscriber(state.subscriberState),
62-
subscription: Subscription(
63-
location: location,
64-
update: { [weak state] in
65-
state?.objectWillChange.send()
66-
}
67-
)
66+
subscriber: Subscriber(subscriberState),
67+
subscription: Subscription(location: location) {
68+
signal.wrappedValue.toggle()
69+
}
6870
)
6971
}
7072
}
7173

7274
private extension ViewContext {
73-
@MainActor
74-
final class State: ObservableObject {
75-
let subscriberState = SubscriberState()
76-
}
77-
7875
@MainActor
7976
var store: StoreContext {
8077
guard let _store else {

0 commit comments

Comments
 (0)