@@ -71,7 +71,7 @@ internal struct StoreContext {
71
71
let ( key, _) = lookupAtomKeyAndOverride ( of: atom)
72
72
73
73
if let cache = lookupCache ( of: atom, for: key) {
74
- switchContext ( scope : cache. initializedScope )
74
+ switchContext ( with : cache)
75
75
. update ( atom: atom, for: key, cache: cache, newValue: value)
76
76
}
77
77
}
@@ -82,7 +82,7 @@ internal struct StoreContext {
82
82
83
83
if let cache = lookupCache ( of: atom, for: key) {
84
84
let newValue = mutating ( cache. value, body)
85
- switchContext ( scope : cache. initializedScope )
85
+ switchContext ( with : cache)
86
86
. update ( atom: atom, for: key, cache: cache, newValue: newValue)
87
87
}
88
88
}
@@ -131,7 +131,7 @@ internal struct StoreContext {
131
131
func refresh< Node: AsyncAtom > ( _ atom: Node ) async -> Node . Produced {
132
132
let ( key, override) = lookupAtomKeyAndOverride ( of: atom)
133
133
let cache = lookupCache ( of: atom, for: key)
134
- let localContext = switchContext ( scope : cache? . initializedScope )
134
+ let localContext = cache. map ( switchContext ) ?? self
135
135
let context = localContext. prepareForTransaction ( of: atom, for: key)
136
136
137
137
let value : Node . Produced
@@ -163,7 +163,7 @@ internal struct StoreContext {
163
163
func refresh< Node: Refreshable > ( _ atom: Node ) async -> Node . Produced {
164
164
let ( key, _) = lookupAtomKeyAndOverride ( of: atom)
165
165
let cache = lookupCache ( of: atom, for: key)
166
- let localContext = switchContext ( scope : cache? . initializedScope )
166
+ let localContext = cache. map ( switchContext ) ?? self
167
167
let state = localContext. getState ( of: atom, for: key)
168
168
let currentContext = AtomCurrentContext ( store: localContext)
169
169
@@ -194,7 +194,7 @@ internal struct StoreContext {
194
194
let ( key, override) = lookupAtomKeyAndOverride ( of: atom)
195
195
196
196
if let cache = lookupCache ( of: atom, for: key) {
197
- let localContext = switchContext ( scope : cache. initializedScope )
197
+ let localContext = switchContext ( with : cache)
198
198
let newValue = localContext. getValue ( of: atom, for: key, override: override)
199
199
localContext. update ( atom: atom, for: key, cache: cache, newValue: newValue)
200
200
}
@@ -204,10 +204,12 @@ internal struct StoreContext {
204
204
@usableFromInline
205
205
func reset( _ atom: some Resettable ) {
206
206
let ( key, _) = lookupAtomKeyAndOverride ( of: atom)
207
- let cache = lookupCache ( of: atom, for: key)
208
- let localContext = switchContext ( scope: cache? . initializedScope)
209
- let currentContext = AtomCurrentContext ( store: localContext)
210
- atom. reset ( context: currentContext)
207
+
208
+ if let cache = lookupCache ( of: atom, for: key) {
209
+ let localContext = switchContext ( with: cache)
210
+ let currentContext = AtomCurrentContext ( store: localContext)
211
+ atom. reset ( context: currentContext)
212
+ }
211
213
}
212
214
213
215
@usableFromInline
@@ -322,7 +324,7 @@ private extension StoreContext {
322
324
323
325
func updatePropagation( for key: AtomKey , cache: some AtomCacheProtocol ) {
324
326
// Dependents must be updated with the scope at which they were initialised.
325
- let localContext = switchContext ( scope : cache. initializedScope )
327
+ let localContext = switchContext ( with : cache)
326
328
327
329
// Overridden atoms don't get updated transitively.
328
330
let newValue = localContext. getValue ( of: cache. atom, for: key, override: nil )
@@ -422,7 +424,7 @@ private extension StoreContext {
422
424
423
425
if let state, let cache {
424
426
// It must call release effect with the scope at which they were initialised.
425
- let localContext = switchContext ( scope : cache. initializedScope )
427
+ let localContext = switchContext ( with : cache)
426
428
let currentContext = AtomCurrentContext ( store: localContext)
427
429
state. effect. released ( context: currentContext)
428
430
}
@@ -510,7 +512,7 @@ private extension StoreContext {
510
512
511
513
return AtomProducerContext ( store: self , transactionState: transactionState) { newValue in
512
514
if let cache = lookupCache ( of: atom, for: key) {
513
- switchContext ( scope : cache. initializedScope )
515
+ switchContext ( with : cache)
514
516
. update ( atom: atom, for: key, cache: cache, newValue: newValue)
515
517
}
516
518
}
@@ -647,11 +649,11 @@ private extension StoreContext {
647
649
}
648
650
}
649
651
650
- func switchContext( scope : Scope ? ) -> StoreContext {
652
+ func switchContext( with cache : some AtomCacheProtocol ) -> StoreContext {
651
653
StoreContext (
652
654
store: store,
653
655
rootScope: rootScope,
654
- currentScope: scope
656
+ currentScope: cache . initializedScope
655
657
)
656
658
}
657
659
}
0 commit comments