Skip to content

Commit f75b602

Browse files
committed
Refactor comments
1 parent e626128 commit f75b602

File tree

8 files changed

+29
-44
lines changed

8 files changed

+29
-44
lines changed

Sources/Atoms/Core/AtomKey.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
@usableFromInline
21
internal struct AtomKey: Hashable {
3-
@usableFromInline
42
let typeKey: AtomTypeKey
53

64
private let identifier: AnyHashable
75

8-
@usableFromInline
96
init<Node: Atom>(_ atom: Node) {
107
typeKey = AtomTypeKey(Node.self)
118
identifier = AnyHashable(atom.key)

Sources/Atoms/Core/AtomTypeKey.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
@usableFromInline
21
internal struct AtomTypeKey: Hashable {
32
private let identifier: ObjectIdentifier
43

5-
@usableFromInline
64
init<Node: Atom>(_: Node.Type) {
75
identifier = ObjectIdentifier(Node.self)
86
}

Sources/Atoms/Core/Graph.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
@MainActor
22
internal struct Graph {
3-
/// Upstream atom keys.
43
var dependencies = [AtomKey: Set<AtomKey>]()
5-
6-
/// Downstream atom keys.
74
var children = [AtomKey: Set<AtomKey>]()
85

96
nonisolated init() {}

Sources/Atoms/Core/Overrides.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
@usableFromInline
21
@MainActor
32
internal struct Overrides {
4-
@usableFromInline
5-
internal var _entriesForNode = [AtomKey: Override]()
6-
7-
@usableFromInline
8-
internal var _entriesForType = [AtomTypeKey: Override]()
3+
private var _entriesForNode = [AtomKey: Override]()
4+
private var _entriesForType = [AtomTypeKey: Override]()
95

106
mutating func insert<Node: Atom>(
117
_ atom: Node,
@@ -34,10 +30,11 @@ internal struct Overrides {
3430
guard let override = baseOverride as? ConcreteOverride<Node> else {
3531
assertionFailure(
3632
"""
33+
[Atoms]
3734
Detected an illegal override.
3835
There might be duplicate keys or logic failure.
3936
Detected: \(type(of: self))
40-
Expected: OverrideValue<\(Node.self)>
37+
Expected: ConcreteOverride<\(Node.self)>
4138
"""
4239
)
4340

@@ -48,11 +45,9 @@ internal struct Overrides {
4845
}
4946
}
5047

51-
@usableFromInline
5248
@MainActor
5349
internal protocol Override {}
5450

55-
@usableFromInline
5651
internal struct ConcreteOverride<Node: Atom>: Override {
5752
let value: (Node) -> Node.Loader.Value
5853
}

Sources/Atoms/Core/Store.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@usableFromInline
21
@MainActor
32
internal final class Store {
43
var graph = Graph()

Sources/Atoms/Core/StoreContext.swift

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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) ?? []
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
@MainActor
22
internal struct Subscription {
3-
/// Notify atom update to the view.
43
let notifyUpdate: () -> Void
5-
6-
/// Unsubscribe from the store.
74
let unsubscribe: () -> Void
85
}

Sources/Atoms/Core/SubscriptionContainer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ internal final class SubscriptionContainer {
44
var subscriptions = [AtomKey: Subscription]()
55

66
deinit {
7-
for subscription in subscriptions.values {
7+
for subscription in ContiguousArray(subscriptions.values) {
88
subscription.unsubscribe()
99
}
1010
}

0 commit comments

Comments
 (0)