Skip to content

Commit e779f2b

Browse files
committed
Refactoring
1 parent 3257d30 commit e779f2b

File tree

8 files changed

+33
-33
lines changed

8 files changed

+33
-33
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ The context also provides a flexible solution for passing dynamic parameters to
909909

910910
### Context
911911

912-
Context is a context structure for using and interacting with atom values from a view or an another atom.
912+
Context is a structure for using and interacting with atom values from views or other atoms.
913913

914914
|API|Use|
915915
|:--|:--|
@@ -992,7 +992,7 @@ struct BooksView: View {
992992

993993
</details>
994994

995-
Context available through the `@ViewContext` property wrapper when using atoms from a view.
995+
A context available through the `@ViewContext` property wrapper when using atoms from a view.
996996

997997
#### [AtomTransactionContext](https://ra1028.github.io/swiftui-atom-properties/documentation/atoms/atomtransactioncontext)
998998

@@ -1023,7 +1023,7 @@ struct CoordinateAtom: ValueAtom, Hashable {
10231023

10241024
</details>
10251025

1026-
Context passed as a parameter to the primary function of an atom type.
1026+
A context passed as a parameter to the primary function of each atom type.
10271027
This context type has a `coordinator` property that preserves an instance from the time an atom is used and initialized until it is unused and cleaned up, so it can be used to cache values or as a lifecycle for an atom.
10281028

10291029
|API|Use|
@@ -1073,7 +1073,7 @@ class FetchMusicsTests: XCTestCase {
10731073

10741074
</details>
10751075

1076-
Context that can simulate any scenarios in which atoms are used from a view or another atom and provides a comprehensive means of testing.
1076+
A context that can simulate any scenarios in which atoms are used from a view or another atom and provides a comprehensive means of testing.
10771077

10781078
|API|Use|
10791079
|:--|:--|

Sources/Atoms/Core/AtomCache.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
@MainActor
2-
internal protocol AtomCache {
2+
internal protocol AtomCacheBase {
33
var shouldKeepAlive: Bool { get }
44

55
func reset(with store: StoreContext)
66
func notifyUnassigned(to observers: [AtomObserver])
77
}
88

99
@MainActor
10-
internal struct ConcreteAtomCache<Node: Atom>: AtomCache {
10+
internal struct AtomCache<Node: Atom>: AtomCacheBase {
1111
var atom: Node
1212
var value: Node.Loader.Value?
1313

Sources/Atoms/Core/AtomState.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
@MainActor
2-
internal protocol AtomState {
2+
internal protocol AtomStateBase {
33
var transaction: Transaction? { get nonmutating set }
44
var subscriptions: [SubscriptionKey: Subscription] { get nonmutating set }
55
}
66

7-
internal final class ConcreteAtomState<Coordinator>: AtomState {
7+
internal final class AtomState<Coordinator>: AtomStateBase {
88
let coordinator: Coordinator
99
var transaction: Transaction?
1010
var subscriptions = [SubscriptionKey: Subscription]()

Sources/Atoms/Core/StoreContext.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ private extension StoreContext {
210210
}
211211
}
212212

213-
func getCache<Node: Atom>(of atom: Node, for key: AtomKey) -> ConcreteAtomCache<Node> {
213+
func getCache<Node: Atom>(of atom: Node, for key: AtomKey) -> AtomCache<Node> {
214214
let store = getStore()
215215

216216
if let cache = peekCache(of: atom, for: key) {
217217
return cache
218218
}
219219
else {
220-
let cache = ConcreteAtomCache(atom: atom)
220+
let cache = AtomCache(atom: atom)
221221
store.state.atomCaches[key] = cache
222222

223223
for observer in observers {
@@ -228,14 +228,14 @@ private extension StoreContext {
228228
}
229229
}
230230

231-
func peekCache<Node: Atom>(of atom: Node, for key: AtomKey) -> ConcreteAtomCache<Node>? {
231+
func peekCache<Node: Atom>(of atom: Node, for key: AtomKey) -> AtomCache<Node>? {
232232
let store = getStore()
233233

234234
guard let baseCache = store.state.atomCaches[key] else {
235235
return nil
236236
}
237237

238-
guard let cache = baseCache as? ConcreteAtomCache<Node> else {
238+
guard let cache = baseCache as? AtomCache<Node> else {
239239
assertionFailure(
240240
"""
241241
[Atoms]
@@ -257,12 +257,12 @@ private extension StoreContext {
257257
return cache
258258
}
259259

260-
func getState<Node: Atom>(of atom: Node, for key: AtomKey) -> ConcreteAtomState<Node.Coordinator> {
260+
func getState<Node: Atom>(of atom: Node, for key: AtomKey) -> AtomState<Node.Coordinator> {
261261
let store = getStore()
262262

263-
func makeState() -> ConcreteAtomState<Node.Coordinator> {
263+
func makeState() -> AtomState<Node.Coordinator> {
264264
let coordinator = atom.makeCoordinator()
265-
let state = ConcreteAtomState(coordinator: coordinator)
265+
let state = AtomState(coordinator: coordinator)
266266
store.state.atomStates[key] = state
267267
return state
268268
}
@@ -271,7 +271,7 @@ private extension StoreContext {
271271
return makeState()
272272
}
273273

274-
guard let state = baseState as? ConcreteAtomState<Node.Coordinator> else {
274+
guard let state = baseState as? AtomState<Node.Coordinator> else {
275275
assertionFailure(
276276
"""
277277
[Atoms]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@MainActor
22
internal struct StoreState {
3-
var atomCaches = [AtomKey: AtomCache]()
4-
var atomStates = [AtomKey: AtomState]()
3+
var atomCaches = [AtomKey: AtomCacheBase]()
4+
var atomStates = [AtomKey: AtomStateBase]()
55

66
nonisolated init() {}
77
}

Tests/AtomsTests/Core/AtomCacheTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ final class AtomCacheTests: XCTestCase {
1111
}
1212
}
1313

14-
let state0 = ConcreteAtomCache(atom: TestValueAtom(value: 0))
15-
let state1 = ConcreteAtomCache(atom: KeepAliveAtom())
14+
let state0 = AtomCache(atom: TestValueAtom(value: 0))
15+
let state1 = AtomCache(atom: KeepAliveAtom())
1616

1717
XCTAssertFalse(state0.shouldKeepAlive)
1818
XCTAssertTrue(state1.shouldKeepAlive)
@@ -24,7 +24,7 @@ final class AtomCacheTests: XCTestCase {
2424
let atom = TestValueAtom(value: 0)
2525
let dependency = TestStateAtom(defaultValue: 0)
2626
let transaction = Transaction(key: AtomKey(atom)) {}
27-
let state = ConcreteAtomCache(atom: atom)
27+
let state = AtomCache(atom: atom)
2828

2929
XCTAssertEqual(context.watch(dependency, in: transaction), 0)
3030

@@ -38,7 +38,7 @@ final class AtomCacheTests: XCTestCase {
3838

3939
func testNotifyUnassigned() {
4040
let atom = TestStateAtom(defaultValue: 0)
41-
let state = ConcreteAtomCache(atom: atom)
41+
let state = AtomCache(atom: atom)
4242
let observer = TestObserver()
4343

4444
XCTAssertTrue(observer.assignedAtomKeys.isEmpty)

Tests/AtomsTests/Core/EnvironmentTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class EnvironmentTests: XCTestCase {
1010
let atom = TestValueAtom(value: 0)
1111
var environment = EnvironmentValues()
1212

13-
store.state.atomCaches = [AtomKey(atom): ConcreteAtomCache(atom: atom, value: 100)]
13+
store.state.atomCaches = [AtomKey(atom): AtomCache(atom: atom, value: 100)]
1414
environment.store = StoreContext(store)
1515

1616
XCTAssertEqual(environment.store.read(atom), 100)

Tests/AtomsTests/Core/StoreContextTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class StoreContextTests: XCTestCase {
1717
XCTAssertEqual(observer.changedAtomKeys, [AtomKey(atom)])
1818
XCTAssertEqual(observer.unassignedAtomKeys, [AtomKey(atom)])
1919

20-
store.state.atomCaches[key] = ConcreteAtomCache(atom: atom, value: 1)
20+
store.state.atomCaches[key] = AtomCache(atom: atom, value: 1)
2121

2222
XCTAssertEqual(context.read(atom), 1)
2323
XCTAssertNil(store.state.atomCaches[key])
@@ -55,7 +55,7 @@ final class StoreContextTests: XCTestCase {
5555
XCTAssertNil(store.state.atomStates[key])
5656
XCTAssertNil(store.state.atomCaches[key])
5757

58-
let atomState = ConcreteAtomState(coordinator: atom.makeCoordinator())
58+
let atomState = AtomState(coordinator: atom.makeCoordinator())
5959
store.state.atomStates[key] = atomState
6060
atomState.subscriptions[subscriptionKey] = Subscription(
6161
notifyUpdate: { updateCount += 1 },
@@ -74,7 +74,7 @@ final class StoreContextTests: XCTestCase {
7474
XCTAssertNil(store.state.atomStates[key]?.transaction)
7575
XCTAssertNil(store.state.atomCaches[key])
7676

77-
store.state.atomCaches[key] = ConcreteAtomCache(atom: atom, value: 2)
77+
store.state.atomCaches[key] = AtomCache(atom: atom, value: 2)
7878
context.set(3, for: atom)
7979

8080
XCTAssertEqual(updateCount, 1)
@@ -85,7 +85,7 @@ final class StoreContextTests: XCTestCase {
8585
XCTAssertTrue(observer.unassignedAtomKeys.isEmpty)
8686
XCTAssertNotNil(store.state.atomStates[key])
8787
XCTAssertNil(store.state.atomStates[key]?.transaction)
88-
XCTAssertEqual((store.state.atomCaches[key] as? ConcreteAtomCache<TestStateAtom<Int>>)?.value, 3)
88+
XCTAssertEqual((store.state.atomCaches[key] as? AtomCache<TestStateAtom<Int>>)?.value, 3)
8989
}
9090

9191
func testWatch() {
@@ -103,7 +103,7 @@ final class StoreContextTests: XCTestCase {
103103
XCTAssertEqual(context.watch(dependency0, in: transaction), 0)
104104
XCTAssertEqual(store.graph.dependencies, [key: [dependency0Key]])
105105
XCTAssertEqual(store.graph.children, [dependency0Key: [key]])
106-
XCTAssertEqual((store.state.atomCaches[dependency0Key] as? ConcreteAtomCache<TestStateAtom<Int>>)?.value, 0)
106+
XCTAssertEqual((store.state.atomCaches[dependency0Key] as? AtomCache<TestStateAtom<Int>>)?.value, 0)
107107
XCTAssertNotNil(store.state.atomStates[dependency0Key])
108108
XCTAssertEqual(observer.assignedAtomKeys, [dependency0Key])
109109
XCTAssertEqual(observer.changedAtomKeys, [dependency0Key])
@@ -151,8 +151,8 @@ final class StoreContextTests: XCTestCase {
151151
XCTAssertEqual(initialValue, 0)
152152
XCTAssertNotNil(container.wrapper.subscriptions[key])
153153
XCTAssertNotNil(store.state.atomStates[key]?.subscriptions[subscriptionKey])
154-
XCTAssertEqual((store.state.atomCaches[key] as? ConcreteAtomCache<TestAtom>)?.value, 0)
155-
XCTAssertEqual((store.state.atomCaches[dependencyKey] as? ConcreteAtomCache<DependencyAtom>)?.value, 0)
154+
XCTAssertEqual((store.state.atomCaches[key] as? AtomCache<TestAtom>)?.value, 0)
155+
XCTAssertEqual((store.state.atomCaches[dependencyKey] as? AtomCache<DependencyAtom>)?.value, 0)
156156
XCTAssertEqual(observer.assignedAtomKeys, [key, dependencyKey])
157157
XCTAssertEqual(observer.changedAtomKeys, [dependencyKey, key])
158158
XCTAssertTrue(observer.unassignedAtomKeys.isEmpty)
@@ -194,7 +194,7 @@ final class StoreContextTests: XCTestCase {
194194
}
195195

196196
let value1 = await context.refresh(atom).value
197-
let cachedValue = await (store.state.atomCaches[key] as? ConcreteAtomCache<TestTaskAtom<Int>>)?.value?.value
197+
let cachedValue = await (store.state.atomCaches[key] as? AtomCache<TestTaskAtom<Int>>)?.value?.value
198198

199199
XCTAssertEqual(value1, 0)
200200
XCTAssertNotNil(store.state.atomCaches[key])
@@ -270,13 +270,13 @@ final class StoreContextTests: XCTestCase {
270270

271271
_ = context.watch(atom, container: container.wrapper) {}
272272

273-
let state = store.state.atomStates[key] as? ConcreteAtomState<TestAtom.Coordinator>
273+
let state = store.state.atomStates[key] as? AtomState<TestAtom.Coordinator>
274274

275275
XCTAssertNotNil(state?.coordinator)
276276

277277
context.reset(atom)
278278

279-
let newState = store.state.atomStates[key] as? ConcreteAtomState<TestAtom.Coordinator>
279+
let newState = store.state.atomStates[key] as? AtomState<TestAtom.Coordinator>
280280

281281
XCTAssertTrue(state?.coordinator === newState?.coordinator)
282282
}

0 commit comments

Comments
 (0)