@@ -59,8 +59,8 @@ public struct AtomScope<Content: View>: View {
5959 /// - id: An identifier represents this scope used for matching with scoped atoms.
6060 /// - content: The descendant view content that provides scoped context for atoms.
6161 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 )
6464 self . content = content ( )
6565 }
6666
@@ -82,9 +82,9 @@ public struct AtomScope<Content: View>: View {
8282 /// The content and behavior of the view.
8383 public var body : some View {
8484 switch inheritance {
85- case . environment( let id ) :
85+ case . environment( let scopeID ) :
8686 WithEnvironment (
87- id : id ,
87+ scopeID : scopeID ,
8888 observers: observers,
8989 overrideContainer: overrideContainer,
9090 content: content
@@ -110,7 +110,13 @@ public struct AtomScope<Content: View>: View {
110110 ///
111111 /// - Returns: The self instance.
112112 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) ) }
114120 }
115121
116122 /// Override the atoms used in this scope with the given value.
@@ -128,7 +134,13 @@ public struct AtomScope<Content: View>: View {
128134 ///
129135 /// - Returns: The self instance.
130136 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) }
132144 }
133145
134146 /// Override the atoms used in this scope with the given value.
@@ -148,41 +160,43 @@ public struct AtomScope<Content: View>: View {
148160 ///
149161 /// - Returns: The self instance.
150162 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) }
152170 }
153171}
154172
155173private extension AtomScope {
156174 enum Inheritance {
157- case environment( id : ScopeID )
175+ case environment( scopeID : ScopeID )
158176 case context( store: StoreContext )
159177 }
160178
161179 struct WithEnvironment : View {
162- let id : ScopeID
180+ let scopeID : ScopeID
163181 let observers : [ Observer ]
164182 let overrideContainer : OverrideContainer
165183 let content : Content
166184
167185 @State
168- private var state = ScopeState ( )
186+ private var scopeToken = ScopeKey . Token ( )
169187 @Environment ( \. store)
170188 private var environmentStore
171189
172190 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+ )
179199 )
180-
181- state. unregister = {
182- store? . unregister ( scopeKey: scopeKey)
183- }
184-
185- return content. environment ( \. store, store)
186200 }
187201 }
188202
0 commit comments