Skip to content

Commit 2f35840

Browse files
committed
Refactoring
1 parent b3a6d3b commit 2f35840

27 files changed

+334
-246
lines changed

Sources/Atoms/AtomRoot.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private extension AtomRoot {
187187
var body: some View {
188188
let scopeKey = state.token.key
189189
let store = StoreContext.registerRoot(
190-
store: store,
190+
in: store,
191191
scopeKey: scopeKey,
192192
overrides: overrides,
193193
observers: observers

Sources/Atoms/AtomScope.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public struct AtomScope<Content: View>: View {
104104
/// Note that unlike ``AtomRoot/observe(_:)``, this observes only the state changes caused by atoms
105105
/// used in this scope.
106106
///
107-
/// - Note: If this scope inherits the parent context, the observers are ignored.
107+
/// - Note: It ignores the observers if this scope inherits the parent scope.
108108
///
109109
/// - Parameter onUpdate: A closure to handle a snapshot of recent updates.
110110
///
@@ -120,7 +120,7 @@ public struct AtomScope<Content: View>: View {
120120
///
121121
/// This only overrides atoms used in this scope and never be inherited to a nested scopes.
122122
///
123-
/// - Note: If this scope inherits the parent context, the overrides are ignored.
123+
/// - Note: It ignores the overrides if this scope inherits the parent scope.
124124
///
125125
/// - Parameters:
126126
/// - atom: An atom to be overridden.
@@ -140,7 +140,7 @@ public struct AtomScope<Content: View>: View {
140140
///
141141
/// This only overrides atoms used in this scope and never be inherited to a nested scopes.
142142
///
143-
/// - Note: If this scope inherits the parent context, the overrides are ignored.
143+
/// - Note: It ignores the overrides if this scope inherits the parent scope.
144144
///
145145
/// - Parameters:
146146
/// - atomType: An atom type to be overridden.

Sources/Atoms/Context/AtomCurrentContext.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
public struct AtomCurrentContext: AtomContext {
44
@usableFromInline
55
internal let _store: StoreContext
6-
@usableFromInline
7-
internal let _transactionScopeKey: ScopeKey?
86

9-
internal init(store: StoreContext, transactionScopeKey: ScopeKey?) {
7+
internal init(store: StoreContext) {
108
self._store = store
11-
self._transactionScopeKey = transactionScopeKey
129
}
1310

1411
/// Accesses the value associated with the given atom without watching it.
@@ -27,7 +24,7 @@ public struct AtomCurrentContext: AtomContext {
2724
/// - Returns: The value associated with the given atom.
2825
@inlinable
2926
public func read<Node: Atom>(_ atom: Node) -> Node.Produced {
30-
_store.read(atom, transactionScopeKey: _transactionScopeKey)
27+
_store.read(atom)
3128
}
3229

3330
/// Sets the new value for the given writable atom.
@@ -49,7 +46,7 @@ public struct AtomCurrentContext: AtomContext {
4946
/// - atom: A writable atom to update.
5047
@inlinable
5148
public func set<Node: StateAtom>(_ value: Node.Produced, for atom: Node) {
52-
_store.set(value, for: atom, transactionScopeKey: _transactionScopeKey)
49+
_store.set(value, for: atom)
5350
}
5451

5552
/// Modifies the cached value of the given writable atom.
@@ -73,7 +70,7 @@ public struct AtomCurrentContext: AtomContext {
7370
/// - body: A value modification body.
7471
@inlinable
7572
public func modify<Node: StateAtom>(_ atom: Node, body: (inout Node.Produced) -> Void) {
76-
_store.modify(atom, transactionScopeKey: _transactionScopeKey, body: body)
73+
_store.modify(atom, body: body)
7774
}
7875

7976
/// Refreshes and then returns the value associated with the given refreshable atom.
@@ -97,7 +94,7 @@ public struct AtomCurrentContext: AtomContext {
9794
@_disfavoredOverload
9895
@discardableResult
9996
public func refresh<Node: AsyncAtom>(_ atom: Node) async -> Node.Produced {
100-
await _store.refresh(atom, transactionScopeKey: _transactionScopeKey)
97+
await _store.refresh(atom)
10198
}
10299

103100
/// Refreshes and then returns the value associated with the given refreshable atom.
@@ -119,7 +116,7 @@ public struct AtomCurrentContext: AtomContext {
119116
@inlinable
120117
@discardableResult
121118
public func refresh<Node: Refreshable>(_ atom: Node) async -> Node.Produced {
122-
await _store.refresh(atom, transactionScopeKey: _transactionScopeKey)
119+
await _store.refresh(atom)
123120
}
124121

125122
/// Resets the value associated with the given atom, and then notifies.
@@ -140,7 +137,7 @@ public struct AtomCurrentContext: AtomContext {
140137
@inlinable
141138
@_disfavoredOverload
142139
public func reset<Node: Atom>(_ atom: Node) {
143-
_store.reset(atom, transactionScopeKey: _transactionScopeKey)
140+
_store.reset(atom)
144141
}
145142

146143
/// Calls arbitrary reset function of the given atom.
@@ -160,6 +157,6 @@ public struct AtomCurrentContext: AtomContext {
160157
/// - Parameter atom: An atom to reset.
161158
@inlinable
162159
public func reset<Node: Resettable>(_ atom: Node) {
163-
_store.reset(atom, transactionScopeKey: _transactionScopeKey)
160+
_store.reset(atom)
164161
}
165162
}

Sources/Atoms/Context/AtomTestContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ internal extension AtomTestContext {
438438
@usableFromInline
439439
var _store: StoreContext {
440440
.registerRoot(
441-
store: _state.store,
441+
in: _state.store,
442442
scopeKey: _state.token.key,
443443
overrides: _state.overrides,
444444
observers: []

Sources/Atoms/Context/AtomTransactionContext.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public struct AtomTransactionContext: AtomWatchableContext {
3333
/// - Returns: The value associated with the given atom.
3434
@inlinable
3535
public func read<Node: Atom>(_ atom: Node) -> Node.Produced {
36-
_store.read(atom, transactionScopeKey: _transactionState.scopeKey)
36+
_store.read(atom)
3737
}
3838

3939
/// Sets the new value for the given writable atom.
@@ -56,7 +56,7 @@ public struct AtomTransactionContext: AtomWatchableContext {
5656
/// - atom: A writable atom to update.
5757
@inlinable
5858
public func set<Node: StateAtom>(_ value: Node.Produced, for atom: Node) {
59-
_store.set(value, for: atom, transactionScopeKey: _transactionState.scopeKey)
59+
_store.set(value, for: atom)
6060
}
6161

6262
/// Modifies the cached value of the given writable atom.
@@ -80,7 +80,7 @@ public struct AtomTransactionContext: AtomWatchableContext {
8080
/// - body: A value modification body.
8181
@inlinable
8282
public func modify<Node: StateAtom>(_ atom: Node, body: (inout Node.Produced) -> Void) {
83-
_store.modify(atom, transactionScopeKey: _transactionState.scopeKey, body: body)
83+
_store.modify(atom, body: body)
8484
}
8585

8686
/// Refreshes and then returns the value associated with the given refreshable atom.
@@ -104,7 +104,7 @@ public struct AtomTransactionContext: AtomWatchableContext {
104104
@_disfavoredOverload
105105
@discardableResult
106106
public func refresh<Node: AsyncAtom>(_ atom: Node) async -> Node.Produced {
107-
await _store.refresh(atom, transactionScopeKey: _transactionState.scopeKey)
107+
await _store.refresh(atom)
108108
}
109109

110110
/// Refreshes and then returns the value associated with the given refreshable atom.
@@ -126,7 +126,7 @@ public struct AtomTransactionContext: AtomWatchableContext {
126126
@inlinable
127127
@discardableResult
128128
public func refresh<Node: Refreshable>(_ atom: Node) async -> Node.Produced {
129-
await _store.refresh(atom, transactionScopeKey: _transactionState.scopeKey)
129+
await _store.refresh(atom)
130130
}
131131

132132
/// Resets the value associated with the given atom, and then notifies.
@@ -148,7 +148,7 @@ public struct AtomTransactionContext: AtomWatchableContext {
148148
@inlinable
149149
@_disfavoredOverload
150150
public func reset<Node: Atom>(_ atom: Node) {
151-
_store.reset(atom, transactionScopeKey: _transactionState.scopeKey)
151+
_store.reset(atom)
152152
}
153153

154154
/// Calls arbitrary reset function of the given atom.
@@ -168,7 +168,7 @@ public struct AtomTransactionContext: AtomWatchableContext {
168168
/// - Parameter atom: An atom to reset.
169169
@inlinable
170170
public func reset<Node: Resettable>(_ atom: Node) {
171-
_store.reset(atom, transactionScopeKey: _transactionState.scopeKey)
171+
_store.reset(atom)
172172
}
173173

174174
/// Accesses the value associated with the given atom for reading and initiates watch to

Sources/Atoms/Core/AtomCache.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ internal protocol AtomCacheProtocol {
33

44
var atom: Node { get }
55
var value: Node.Produced { get }
6+
var initScopeKey: ScopeKey? { get }
7+
8+
func updated(value: Node.Produced) -> Self
69
}
710

811
internal struct AtomCache<Node: Atom>: AtomCacheProtocol, CustomStringConvertible {
9-
var atom: Node
10-
var value: Node.Produced
12+
let atom: Node
13+
let value: Node.Produced
14+
let initScopeKey: ScopeKey?
1115

1216
var description: String {
1317
"\(value)"
1418
}
19+
20+
func updated(value: Node.Produced) -> Self {
21+
AtomCache(atom: atom, value: value, initScopeKey: initScopeKey)
22+
}
1523
}

Sources/Atoms/Core/AtomKey.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
internal struct AtomKey: Hashable, Sendable, CustomStringConvertible {
22
private let key: UnsafeUncheckedSendable<AnyHashable>
33
private let type: ObjectIdentifier
4+
private let scopeKey: ScopeKey?
45
private let anyAtomType: Any.Type
56

6-
let scopeKey: ScopeKey?
7-
87
var description: String {
98
let atomLabel = String(describing: anyAtomType)
109

@@ -16,6 +15,10 @@ internal struct AtomKey: Hashable, Sendable, CustomStringConvertible {
1615
}
1716
}
1817

18+
var isScoped: Bool {
19+
scopeKey != nil
20+
}
21+
1922
init<Node: Atom>(_ atom: Node, scopeKey: ScopeKey?) {
2023
self.key = UnsafeUncheckedSendable(atom.key)
2124
self.type = ObjectIdentifier(Node.self)

Sources/Atoms/Core/AtomState.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@ internal protocol AtomStateProtocol: AnyObject {
33
associatedtype Effect: AtomEffect
44

55
var effect: Effect { get }
6-
var initializedScopeKey: ScopeKey? { get }
76
var transactionState: TransactionState? { get set }
87
}
98

109
@MainActor
1110
internal final class AtomState<Effect: AtomEffect>: AtomStateProtocol {
1211
let effect: Effect
13-
let initializedScopeKey: ScopeKey?
1412
var transactionState: TransactionState?
1513

16-
init(effect: Effect, initializedScopeKey: ScopeKey?) {
14+
init(effect: Effect) {
1715
self.effect = effect
18-
self.initializedScopeKey = initializedScopeKey
1916
}
2017
}

0 commit comments

Comments
 (0)