@@ -49,8 +49,8 @@ import SwiftUI
4949///
5050public struct AtomScope < Content: View > : View {
5151 private let inheritance : Inheritance
52- private var overrides : [ OverrideKey : any OverrideProtocol ]
53- private var observers : [ Observer ]
52+ private var overrides = [ OverrideKey : any OverrideProtocol ] ( )
53+ private var observers = [ Observer] ( )
5454 private let content : Content
5555
5656 /// Creates a new scope with the specified content.
@@ -61,8 +61,6 @@ public struct AtomScope<Content: View>: View {
6161 public init < ID: Hashable > ( id: ID = DefaultScopeID ( ) , @ViewBuilder content: ( ) -> Content ) {
6262 let id = ScopeID ( id)
6363 self . inheritance = . environment( id: id)
64- self . overrides = [ : ]
65- self . observers = [ ]
6664 self . content = content ( )
6765 }
6866
@@ -78,8 +76,6 @@ public struct AtomScope<Content: View>: View {
7876 ) {
7977 let store = context. _store
8078 self . inheritance = . context( store: store)
81- self . overrides = store. scopedOverrides
82- self . observers = store. scopedObservers
8379 self . content = content ( )
8480 }
8581
@@ -97,8 +93,6 @@ public struct AtomScope<Content: View>: View {
9793 case . context( let store) :
9894 WithContext (
9995 store: store,
100- overrides: overrides,
101- observers: observers,
10296 content: content
10397 )
10498 }
@@ -110,6 +104,8 @@ public struct AtomScope<Content: View>: View {
110104 /// Note that unlike ``AtomRoot/observe(_:)``, this observes only the state changes caused by atoms
111105 /// used in this scope.
112106 ///
107+ /// - Note: It ignores the observers if this scope inherits the parent scope.
108+ ///
113109 /// - Parameter onUpdate: A closure to handle a snapshot of recent updates.
114110 ///
115111 /// - Returns: The self instance.
@@ -124,13 +120,15 @@ public struct AtomScope<Content: View>: View {
124120 ///
125121 /// This only overrides atoms used in this scope and never be inherited to a nested scopes.
126122 ///
123+ /// - Note: It ignores the overrides if this scope inherits the parent scope.
124+ ///
127125 /// - Parameters:
128126 /// - atom: An atom to be overridden.
129127 /// - value: A value to be used instead of the atom's value.
130128 ///
131129 /// - Returns: The self instance.
132130 public func scopedOverride< Node: Atom > ( _ atom: Node , with value: @escaping @MainActor @Sendable ( Node ) -> Node . Produced ) -> Self {
133- mutating ( self ) { $0. overrides [ OverrideKey ( atom) ] = Override ( isScoped : true , getValue: value) }
131+ mutating ( self ) { $0. overrides [ OverrideKey ( atom) ] = Override ( getValue: value) }
134132 }
135133
136134 /// Override the atoms used in this scope with the given value.
@@ -142,13 +140,15 @@ public struct AtomScope<Content: View>: View {
142140 ///
143141 /// This only overrides atoms used in this scope and never be inherited to a nested scopes.
144142 ///
143+ /// - Note: It ignores the overrides if this scope inherits the parent scope.
144+ ///
145145 /// - Parameters:
146146 /// - atomType: An atom type to be overridden.
147147 /// - value: A value to be used instead of the atom's value.
148148 ///
149149 /// - Returns: The self instance.
150150 public func scopedOverride< Node: Atom > ( _ atomType: Node . Type , with value: @escaping @MainActor @Sendable ( Node ) -> Node . Produced ) -> Self {
151- mutating ( self ) { $0. overrides [ OverrideKey ( atomType) ] = Override ( isScoped : true , getValue: value) }
151+ mutating ( self ) { $0. overrides [ OverrideKey ( atomType) ] = Override ( getValue: value) }
152152 }
153153}
154154
@@ -165,37 +165,33 @@ private extension AtomScope {
165165 let content : Content
166166
167167 @State
168- private var token = ScopeKey . Token ( )
168+ private var state = ScopeState ( )
169169 @Environment ( \. store)
170170 private var environmentStore
171171
172172 var body : some View {
173- content. environment (
174- \. store,
175- environmentStore? . scoped (
176- scopeKey: ScopeKey ( token: token) ,
177- scopeID: id,
178- observers: observers,
179- overrides: overrides
180- )
173+ let scopeKey = state. token. key
174+ let store = environmentStore? . registerScope (
175+ scopeID: id,
176+ scopeKey: scopeKey,
177+ overrides: overrides,
178+ observers: observers
181179 )
180+
181+ state. unregister = {
182+ store? . unregister ( scopeKey: scopeKey)
183+ }
184+
185+ return content. environment ( \. store, store)
182186 }
183187 }
184188
185189 struct WithContext : View {
186190 let store : StoreContext
187- let overrides : [ OverrideKey : any OverrideProtocol ]
188- let observers : [ Observer ]
189191 let content : Content
190192
191193 var body : some View {
192- content. environment (
193- \. store,
194- store. inherited (
195- scopedObservers: observers,
196- scopedOverrides: overrides
197- )
198- )
194+ content. environment ( \. store, store)
199195 }
200196 }
201197}
0 commit comments