@@ -59,8 +59,8 @@ public struct AtomScope<Content: View>: View {
59
59
/// - id: An identifier represents this scope used for matching with scoped atoms.
60
60
/// - content: The descendant view content that provides scoped context for atoms.
61
61
public init < ID: Hashable > ( id: ID = DefaultScopeID ( ) , @ViewBuilder content: ( ) -> Content ) {
62
- let id = ScopeID ( id)
63
- self . inheritance = . environment( id : id )
62
+ let scopeID = ScopeID ( id)
63
+ self . inheritance = . environment( scopeID : scopeID )
64
64
self . content = content ( )
65
65
}
66
66
@@ -82,9 +82,9 @@ public struct AtomScope<Content: View>: View {
82
82
/// The content and behavior of the view.
83
83
public var body : some View {
84
84
switch inheritance {
85
- case . environment( let id ) :
85
+ case . environment( let scopeID ) :
86
86
WithEnvironment (
87
- id : id ,
87
+ scopeID : scopeID ,
88
88
observers: observers,
89
89
overrideContainer: overrideContainer,
90
90
content: content
@@ -110,7 +110,13 @@ public struct AtomScope<Content: View>: View {
110
110
///
111
111
/// - Returns: The self instance.
112
112
public func scopedObserve( _ onUpdate: @MainActor @escaping ( Snapshot ) -> Void ) -> Self {
113
- mutating ( self ) { $0. observers. append ( Observer ( onUpdate: onUpdate) ) }
113
+ if case . context = inheritance {
114
+ assertionFailure (
115
+ " [Atoms] AtomScope now ignores the given scoped observers if it's inheriting an ancestor scope. This will be deprecated soon. "
116
+ )
117
+ return self
118
+ }
119
+ return mutating ( self ) { $0. observers. append ( Observer ( onUpdate: onUpdate) ) }
114
120
}
115
121
116
122
/// Override the atoms used in this scope with the given value.
@@ -128,7 +134,13 @@ public struct AtomScope<Content: View>: View {
128
134
///
129
135
/// - Returns: The self instance.
130
136
public func scopedOverride< Node: Atom > ( _ atom: Node , with value: @MainActor @escaping ( Node ) -> Node . Produced ) -> Self {
131
- mutating ( self ) { $0. overrideContainer. addOverride ( for: atom, with: value) }
137
+ if case . context = inheritance {
138
+ assertionFailure (
139
+ " [Atoms] AtomScope now ignores the given scoped overrides if it's inheriting an ancestor scope. This will be deprecated soon. "
140
+ )
141
+ return self
142
+ }
143
+ return mutating ( self ) { $0. overrideContainer. addOverride ( for: atom, with: value) }
132
144
}
133
145
134
146
/// Override the atoms used in this scope with the given value.
@@ -148,41 +160,43 @@ public struct AtomScope<Content: View>: View {
148
160
///
149
161
/// - Returns: The self instance.
150
162
public func scopedOverride< Node: Atom > ( _ atomType: Node . Type , with value: @MainActor @escaping ( Node ) -> Node . Produced ) -> Self {
151
- mutating ( self ) { $0. overrideContainer. addOverride ( for: atomType, with: value) }
163
+ if case . context = inheritance {
164
+ assertionFailure (
165
+ " [Atoms] AtomScope now ignores the given scoped overrides if it's inheriting an ancestor scope. This will be deprecated soon. "
166
+ )
167
+ return self
168
+ }
169
+ return mutating ( self ) { $0. overrideContainer. addOverride ( for: atomType, with: value) }
152
170
}
153
171
}
154
172
155
173
private extension AtomScope {
156
174
enum Inheritance {
157
- case environment( id : ScopeID )
175
+ case environment( scopeID : ScopeID )
158
176
case context( store: StoreContext )
159
177
}
160
178
161
179
struct WithEnvironment : View {
162
- let id : ScopeID
180
+ let scopeID : ScopeID
163
181
let observers : [ Observer ]
164
182
let overrideContainer : OverrideContainer
165
183
let content : Content
166
184
167
185
@State
168
- private var state = ScopeState ( )
186
+ private var scopeToken = ScopeKey . Token ( )
169
187
@Environment ( \. store)
170
188
private var environmentStore
171
189
172
190
var body : some View {
173
- let scopeKey = state. token. key
174
- let store = environmentStore? . registerScope (
175
- scopeID: id,
176
- scopeKey: scopeKey,
177
- observers: observers,
178
- overrideContainer: overrideContainer
191
+ content. environment (
192
+ \. store,
193
+ environmentStore? . scoped (
194
+ scopeID: scopeID,
195
+ scopeKey: scopeToken. key,
196
+ observers: observers,
197
+ overrideContainer: overrideContainer
198
+ )
179
199
)
180
-
181
- state. unregister = {
182
- store? . unregister ( scopeKey: scopeKey)
183
- }
184
-
185
- return content. environment ( \. store, store)
186
200
}
187
201
}
188
202
0 commit comments