@@ -24,15 +24,15 @@ internal struct StoreContext {
2424
2525 @usableFromInline
2626 func set< Node: StateAtom > ( _ value: Node . Value , for atom: Node ) {
27- // Do nothing if the atom is not yet to be watched .
27+ // Do nothing if the atom is not yet to be registered .
2828 guard let oldValue = getCachedState ( for: atom) ? . value else {
2929 return
3030 }
3131
32- // Note that this is a special handling for `willSet/didSet` because the dependencies of the `StateAtom`
33- // can be invalidated if we call `prepareTransaction` here and there's no timing to restore them.
34- // The dependencies added in `willSet/didSet` will not be released until the value is invalidated and
35- // is going to be a bug, so `AtomTransactionContenxt` will no longer be passed.
32+ // Note that this is special handling for `willSet/didSet` because the dependencies could be invalidated
33+ // by `prepareTransaction` here and there's no timing to restore them.
34+ // The dependencies added by `willSet/didSet` will not be released until the value is invalidated and
35+ // is going to be a bug, so `AtomTransactionContenxt` will no longer be passed soon .
3636 // https://github.com/ra1028/swiftui-atom-properties/issues/18
3737 let key = AtomKey ( atom)
3838 let transaction = Transaction ( key: key) {
@@ -51,7 +51,7 @@ internal struct StoreContext {
5151
5252 @usableFromInline
5353 func watch< Node: Atom > ( _ atom: Node , in transaction: Transaction ) -> Node . Loader . Value {
54- // Return a new value if the transaction is already terminated.
54+ // Return a new value immediately if the transaction is already terminated.
5555 guard !transaction. isTerminated else {
5656 return getNewValue ( for: atom)
5757 }
@@ -83,15 +83,15 @@ internal struct StoreContext {
8383 return
8484 }
8585
86- // Unsubscribe and then release the atom if it's doesn't have any subscriptions or children .
86+ // Unsubscribe and release if it's no longer used .
8787 store. state. subscriptions [ key] ? . removeValue ( forKey: subscriptionKey)
8888 checkRelease ( for: key)
8989 }
9090
9191 // Create and register a state if it doesn't exist yet.
9292 registerIfAbsent ( atom: atom)
9393
94- // Register the subscription to both the store and the container, enabling notifying updates and unsubscription .
94+ // Register the subscription to both the store and the container.
9595 container. subscriptions [ key] = subscription
9696 store. state. subscriptions [ key, default: [ : ] ] . updateValue ( subscription, forKey: subscriptionKey)
9797
@@ -110,14 +110,13 @@ internal struct StoreContext {
110110 value = await atom. _loader. refresh ( context: context)
111111 }
112112
113- // Update the current value with the refresh value.
113+ // Update the current value with the fresh value.
114114 update ( atom: atom, with: value)
115115 return value
116116 }
117117
118118 @usableFromInline
119119 func reset< Node: Atom > ( _ atom: Node ) {
120- // Renew the value and then notify to the downstream.
121120 let value = getNewValue ( for: atom)
122121 update ( atom: atom, with: value)
123122 }
@@ -161,10 +160,11 @@ private extension StoreContext {
161160 let dependencies = store. graph. dependencies [ key] ?? [ ]
162161 let obsoletedDependencies = oldDependencies. subtracting ( dependencies)
163162
164- // Check if the dependencies that are no longer watched can be released .
163+ // Check if the dependencies that are no longer used and release them if possible .
165164 checkReleaseDependencies ( obsoletedDependencies, for: key)
166165 }
167166
167+ // Register the transaction state so it can be terminated from anywhere.
168168 store. state. transactions [ key] = transaction
169169
170170 return AtomLoaderContext ( store: self , transaction: transaction) { value, updatesChildrenOnNextRunLoop in
@@ -176,7 +176,7 @@ private extension StoreContext {
176176 let store = getStore ( )
177177 var state = getCachedState ( for: atom)
178178
179- // Return the cached value is exists otherwise, get a new value and then cache it.
179+ // Return the cached value if exists, otherwise, get a new value and then cache it.
180180 if let value = state? . value {
181181 return value
182182 }
@@ -217,16 +217,18 @@ private extension StoreContext {
217217 guard let state = baseState as? ConcreteAtomState < Node > else {
218218 assertionFailure (
219219 """
220+ [Atoms]
220221 The type of the given atom's value and the cached value did not match.
221222 There might be duplicate keys, make sure that the keys for all atom types are unique.
222223
223- Atom type: \( Node . self)
224- Key type: \( type ( of: atom. key) )
225- Invalid state type: \( type ( of: baseState) )
224+ Atom: \( Node . self)
225+ Key: \( type ( of: atom. key) )
226+ Detected: \( type ( of: baseState) )
227+ Expected: ConcreteAtomState< \( Node . self) >
226228 """
227229 )
228230
229- // Release the invalid registration.
231+ // Release the invalid registration as a fallback .
230232 release ( for: key)
231233 return nil
232234 }
@@ -257,7 +259,7 @@ private extension StoreContext {
257259 }
258260
259261 // At the timing when `ObservableObject/objectWillChange` emits, its properties
260- // have not yet been updated and are still old when the dependent atom reads it.
262+ // have not yet been updated and are still old when dependent atoms read it.
261263 // As a workaround, the update is executed in the next run loop
262264 // so that the downstream atoms can receive the object that's already updated.
263265 if updatesChildrenOnNextRunLoop {
@@ -284,7 +286,7 @@ private extension StoreContext {
284286 state? . value = value
285287 store. state. atomStates [ key] = state
286288
287- // Do not notify update if the new value is equivalent to the old value.
289+ // Do not notify update if the new value and the old value are equivalent .
288290 if let oldValue = oldValue, !atom. _loader. shouldNotifyUpdate ( newValue: value, oldValue: oldValue) {
289291 return
290292 }
@@ -301,7 +303,7 @@ private extension StoreContext {
301303 let dependencies = invalidate ( for: key)
302304 let atomState = store. state. atomStates. removeValue ( forKey: key)
303305
304- // Cleanup downstream edges .
306+ // Cleanup downstreams .
305307 store. graph. children. removeValue ( forKey: key)
306308 store. state. subscriptions. removeValue ( forKey: key)
307309 atomState? . notifyUnassigned ( to: observers)
@@ -333,7 +335,7 @@ private extension StoreContext {
333335 func checkReleaseDependencies( _ dependencies: Set < AtomKey > , for key: AtomKey ) {
334336 let store = getStore ( )
335337
336- // Recursively release dependencies while unlinking the dependent from the dependencies .
338+ // Recursively release dependencies while unlinking the dependent.
337339 for dependency in dependencies {
338340 store. graph. children [ dependency] ? . remove ( key)
339341 checkRelease ( for: dependency)
@@ -343,8 +345,8 @@ private extension StoreContext {
343345 func invalidate( for key: AtomKey ) -> Set < AtomKey > {
344346 let store = getStore ( )
345347
346- // Remove the current transaction and then terminate to prevent current transaction
347- // to watch new values or add terminations.
348+ // Remove the current transaction and then terminate to prevent it to watch new atoms
349+ // or add new terminations.
348350 // Then, temporarily remove dependencies but do not release them recursively here.
349351 store. state. transactions. removeValue ( forKey: key) ? . terminate ( )
350352 return store. graph. dependencies. removeValue ( forKey: key) ?? [ ]
0 commit comments