@@ -98,8 +98,8 @@ internal struct StoreContext {
98
98
let value = cache? . value ?? initialize ( of: atom, for: key, override: override)
99
99
100
100
// Add an `Edge` from the upstream to downstream.
101
- store. graph . dependencies [ transactionState. key, default: [ ] ] . insert ( key)
102
- store. graph . children [ key, default: [ ] ] . insert ( transactionState. key)
101
+ store. dependencies [ transactionState. key, default: [ ] ] . insert ( key)
102
+ store. children [ key, default: [ ] ] . insert ( transactionState. key)
103
103
104
104
return value
105
105
}
@@ -113,10 +113,10 @@ internal struct StoreContext {
113
113
let ( key, override) = lookupAtomKeyAndOverride ( of: atom)
114
114
let cache = lookupCache ( of: atom, for: key)
115
115
let value = cache? . value ?? initialize ( of: atom, for: key, override: override)
116
- let isNewSubscription = store. state . subscribed [ subscriber. key, default: [ ] ] . insert ( key) . inserted
116
+ let isNewSubscription = store. subscribes [ subscriber. key, default: [ ] ] . insert ( key) . inserted
117
117
118
118
if isNewSubscription {
119
- store. state . subscriptions [ key, default: [ : ] ] [ subscriber. key] = subscription
119
+ store. subscriptions [ key, default: [ : ] ] [ subscriber. key] = subscription
120
120
subscriber. unsubscribe = {
121
121
unsubscribeAll ( for: subscriber. key)
122
122
}
@@ -224,22 +224,22 @@ internal struct StoreContext {
224
224
func unwatch( _ atom: some Atom , subscriber: Subscriber ) {
225
225
let ( key, _) = lookupAtomKeyAndOverride ( of: atom)
226
226
227
- store. state . subscribed [ subscriber. key] ? . remove ( key)
227
+ store. subscribes [ subscriber. key] ? . remove ( key)
228
228
unsubscribe ( [ key] , for: subscriber. key)
229
229
}
230
230
231
231
@usableFromInline
232
232
func registerScope( state: ScopeState ) {
233
233
let key = state. token. key
234
234
235
- withUnsafeMutablePointer ( to: & store. state . scopes [ key] ) { scope in
235
+ withUnsafeMutablePointer ( to: & store. scopes [ key] ) { scope in
236
236
if scope. pointee == nil {
237
237
scope. pointee = Scope ( )
238
238
}
239
239
}
240
240
241
241
state. unregister = {
242
- let scope = store. state . scopes. removeValue ( forKey: key)
242
+ let scope = store. scopes. removeValue ( forKey: key)
243
243
244
244
if let scope {
245
245
for key in scope. atoms {
@@ -252,9 +252,10 @@ internal struct StoreContext {
252
252
@usableFromInline
253
253
func snapshot( ) -> Snapshot {
254
254
Snapshot (
255
- graph: store. graph,
256
- caches: store. state. caches,
257
- subscriptions: store. state. subscriptions
255
+ dependencies: store. dependencies,
256
+ children: store. children,
257
+ caches: store. caches,
258
+ subscriptions: store. subscriptions
258
259
)
259
260
}
260
261
@@ -264,13 +265,13 @@ internal struct StoreContext {
264
265
var disusedDependencies = [ AtomKey : Set < AtomKey > ] ( )
265
266
266
267
for key in keys {
267
- let oldDependencies = store. graph . dependencies [ key]
268
- let newDependencies = snapshot. graph . dependencies [ key]
268
+ let oldDependencies = store. dependencies [ key]
269
+ let newDependencies = snapshot. dependencies [ key]
269
270
270
271
// Update atom values and the graph.
271
- store. state . caches [ key] = snapshot. caches [ key]
272
- store. graph . dependencies [ key] = newDependencies
273
- store. graph . children [ key] = snapshot. graph . children [ key]
272
+ store. caches [ key] = snapshot. caches [ key]
273
+ store. dependencies [ key] = newDependencies
274
+ store. children [ key] = snapshot. children [ key]
274
275
disusedDependencies [ key] = oldDependencies? . subtracting ( newDependencies ?? [ ] )
275
276
}
276
277
@@ -281,13 +282,13 @@ internal struct StoreContext {
281
282
// Release dependencies that are no longer dependent.
282
283
if let dependencies = disusedDependencies [ key] {
283
284
for dependency in dependencies {
284
- store. graph . children [ dependency] ? . remove ( key)
285
+ store. children [ dependency] ? . remove ( key)
285
286
checkAndRelease ( for: dependency)
286
287
}
287
288
}
288
289
289
290
// Notify updates only for the subscriptions of restored atoms.
290
- if let subscriptions = store. state . subscriptions [ key] {
291
+ if let subscriptions = store. subscriptions [ key] {
291
292
for subscription in subscriptions. values {
292
293
subscription. update ( )
293
294
}
@@ -310,10 +311,10 @@ private extension StoreContext {
310
311
state. effect. initializing ( context: currentContext)
311
312
312
313
let value = getValue ( of: atom, for: key, override: override)
313
- store. state . caches [ key] = AtomCache ( atom: atom, value: value, scopeValues: currentScopeValues)
314
+ store. caches [ key] = AtomCache ( atom: atom, value: value, scopeValues: currentScopeValues)
314
315
315
316
if let scopeKey = key. scopeKey {
316
- store. state . scopes [ scopeKey] ? . atoms. insert ( key)
317
+ store. scopes [ scopeKey] ? . atoms. insert ( key)
317
318
}
318
319
319
320
state. effect. initialized ( context: currentContext)
@@ -326,7 +327,7 @@ private extension StoreContext {
326
327
cache: AtomCache < Node > ,
327
328
newValue: Node . Produced
328
329
) {
329
- store. state . caches [ key] = cache. updated ( value: newValue)
330
+ store. caches [ key] = cache. updated ( value: newValue)
330
331
331
332
// Check whether if the dependent atoms should be updated transitively.
332
333
guard atom. producer. shouldUpdate ( cache. value, newValue) else {
@@ -354,7 +355,7 @@ private extension StoreContext {
354
355
// Overridden atoms don't get updated transitively.
355
356
let newValue = localContext. getValue ( of: cache. atom, for: key, override: nil )
356
357
357
- store. state . caches [ key] = cache. updated ( value: newValue)
358
+ store. caches [ key] = cache. updated ( value: newValue)
358
359
359
360
// Check whether if the dependent atoms should be updated transitively.
360
361
guard cache. atom. producer. shouldUpdate ( cache. value, newValue) else {
@@ -403,8 +404,8 @@ private extension StoreContext {
403
404
continue
404
405
}
405
406
406
- let cache = store. state . caches [ key]
407
- let dependencyCache = store. state . caches [ edge. from]
407
+ let cache = store. caches [ key]
408
+ let dependencyCache = store. caches [ edge. from]
408
409
409
410
if let cache, let dependencyCache {
410
411
performUpdate ( dependency: dependencyCache. atom) {
@@ -417,8 +418,8 @@ private extension StoreContext {
417
418
continue
418
419
}
419
420
420
- let subscription = store. state . subscriptions [ edge. from] ? [ key]
421
- let dependencyCache = store. state . caches [ edge. from]
421
+ let subscription = store. subscriptions [ edge. from] ? [ key]
422
+ let dependencyCache = store. caches [ edge. from]
422
423
423
424
if let subscription, let dependencyCache {
424
425
performUpdate ( dependency: dependencyCache. atom, body: subscription. update)
@@ -431,20 +432,20 @@ private extension StoreContext {
431
432
}
432
433
433
434
func release( for key: AtomKey ) {
434
- let dependencies = store. graph . dependencies. removeValue ( forKey: key)
435
- let state = store. state . states. removeValue ( forKey: key)
436
- let cache = store. state . caches. removeValue ( forKey: key)
435
+ let dependencies = store. dependencies. removeValue ( forKey: key)
436
+ let state = store. states. removeValue ( forKey: key)
437
+ let cache = store. caches. removeValue ( forKey: key)
437
438
438
- store. graph . children. removeValue ( forKey: key)
439
- store. state . subscriptions. removeValue ( forKey: key)
439
+ store. children. removeValue ( forKey: key)
440
+ store. subscriptions. removeValue ( forKey: key)
440
441
441
442
if let scopeKey = key. scopeKey {
442
- store. state . scopes [ scopeKey] ? . atoms. remove ( key)
443
+ store. scopes [ scopeKey] ? . atoms. remove ( key)
443
444
}
444
445
445
446
if let dependencies {
446
447
for dependency in dependencies {
447
- store. graph . children [ dependency] ? . remove ( key)
448
+ store. children [ dependency] ? . remove ( key)
448
449
checkAndRelease ( for: dependency)
449
450
}
450
451
}
@@ -465,7 +466,7 @@ private extension StoreContext {
465
466
// 2. It has no downstream atoms.
466
467
// 3. It has no subscriptions from views.
467
468
lazy var shouldKeepAlive = {
468
- guard let cache = store. state . caches [ key] , cache. shouldKeepAlive else {
469
+ guard let cache = store. caches [ key] , cache. shouldKeepAlive else {
469
470
return false
470
471
}
471
472
@@ -474,10 +475,10 @@ private extension StoreContext {
474
475
}
475
476
476
477
// It should keep alive untile the scope is unregistered.
477
- return store. state . scopes [ scopeKey] ? . atoms. contains ( key) ?? false
478
+ return store. scopes [ scopeKey] ? . atoms. contains ( key) ?? false
478
479
} ( )
479
- lazy var isChildrenEmpty = store. graph . children [ key] ? . isEmpty ?? true
480
- lazy var isSubscriptionEmpty = store. state . subscriptions [ key] ? . isEmpty ?? true
480
+ lazy var isChildrenEmpty = store. children [ key] ? . isEmpty ?? true
481
+ lazy var isSubscriptionEmpty = store. subscriptions [ key] ? . isEmpty ?? true
481
482
482
483
guard !shouldKeepAlive && isChildrenEmpty && isSubscriptionEmpty else {
483
484
return
@@ -488,28 +489,28 @@ private extension StoreContext {
488
489
489
490
func detachDependencies( for key: AtomKey ) -> Set < AtomKey > {
490
491
// Remove current dependencies.
491
- let dependencies = store. graph . dependencies. removeValue ( forKey: key) ?? [ ]
492
+ let dependencies = store. dependencies. removeValue ( forKey: key) ?? [ ]
492
493
493
494
// Detatch the atom from its children.
494
495
for dependency in dependencies {
495
- store. graph . children [ dependency] ? . remove ( key)
496
+ store. children [ dependency] ? . remove ( key)
496
497
}
497
498
498
499
return dependencies
499
500
}
500
501
501
502
func attachDependencies( _ dependencies: Set < AtomKey > , for key: AtomKey ) {
502
503
// Set dependencies.
503
- store. graph . dependencies [ key] = dependencies
504
+ store. dependencies [ key] = dependencies
504
505
505
506
// Attach the atom to its children.
506
507
for dependency in dependencies {
507
- store. graph . children [ dependency] ? . insert ( key)
508
+ store. children [ dependency] ? . insert ( key)
508
509
}
509
510
}
510
511
511
512
func unsubscribeAll( for subscriberKey: SubscriberKey ) {
512
- let keys = store. state . subscribed . removeValue ( forKey: subscriberKey)
513
+ let keys = store. subscribes . removeValue ( forKey: subscriberKey)
513
514
514
515
if let keys {
515
516
unsubscribe ( keys, for: subscriberKey)
@@ -518,7 +519,7 @@ private extension StoreContext {
518
519
519
520
func unsubscribe( _ keys: some Sequence < AtomKey > , for subscriberKey: SubscriberKey ) {
520
521
for key in keys {
521
- store. state . subscriptions [ key] ? . removeValue ( forKey: subscriberKey)
522
+ store. subscriptions [ key] ? . removeValue ( forKey: subscriberKey)
522
523
checkAndRelease ( for: key)
523
524
}
524
525
@@ -533,7 +534,7 @@ private extension StoreContext {
533
534
let oldDependencies = detachDependencies ( for: key)
534
535
535
536
return {
536
- let dependencies = store. graph . dependencies [ key] ?? [ ]
537
+ let dependencies = store. dependencies [ key] ?? [ ]
537
538
let disusedDependencies = oldDependencies. subtracting ( dependencies)
538
539
539
540
// Release disused dependencies if no longer used.
@@ -584,12 +585,12 @@ private extension StoreContext {
584
585
let currentContext = AtomCurrentContext ( store: self )
585
586
let effect = atom. effect ( context: currentContext)
586
587
let state = AtomState ( effect: effect)
587
- store. state . states [ key] = state
588
+ store. states [ key] = state
588
589
return state
589
590
}
590
591
591
592
func lookupState< Node: Atom > ( of atom: Node , for key: AtomKey ) -> AtomState < Node . Effect > ? {
592
- guard let baseState = store. state . states [ key] else {
593
+ guard let baseState = store. states [ key] else {
593
594
return nil
594
595
}
595
596
@@ -616,7 +617,7 @@ private extension StoreContext {
616
617
}
617
618
618
619
func lookupCache< Node: Atom > ( of atom: Node , for key: AtomKey ) -> AtomCache < Node > ? {
619
- guard let baseCache = store. state . caches [ key] else {
620
+ guard let baseCache = store. caches [ key] else {
620
621
return nil
621
622
}
622
623
0 commit comments