Skip to content

Commit 8cc864d

Browse files
authored
Internal refactoring (#119)
* Refactor internal * Refactoring
1 parent 2e93b28 commit 8cc864d

File tree

2 files changed

+21
-47
lines changed

2 files changed

+21
-47
lines changed

Sources/Atoms/Core/StoreContext.swift

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ internal struct StoreContext {
7171
}
7272
else {
7373
let cache = makeCache(of: atom, for: key, override: override)
74-
notifyUpdateToObservers()
75-
76-
if checkAndRelease(for: key) {
77-
notifyUpdateToObservers()
78-
}
79-
74+
checkAndRelease(for: key)
8075
return cache.value
8176
}
8277
}
@@ -152,8 +147,7 @@ internal struct StoreContext {
152147
let override = lookupOverride(of: atom)
153148
let scopeKey = lookupScopeKey(of: atom, isScopedOverriden: override?.isScoped ?? false)
154149
let key = AtomKey(atom, scopeKey: scopeKey)
155-
let state = lookupState(of: atom, for: key) ?? makeEphemeralState(of: atom)
156-
let context = prepareForTransaction(of: atom, for: key, state: state)
150+
let context = prepareForTransaction(of: atom, for: key)
157151
let value: Node.Loader.Value
158152

159153
if let override {
@@ -164,6 +158,7 @@ internal struct StoreContext {
164158
}
165159

166160
guard let cache = lookupCache(of: atom, for: key) else {
161+
checkAndRelease(for: key)
167162
return value
168163
}
169164

@@ -180,11 +175,12 @@ internal struct StoreContext {
180175
let override = lookupOverride(of: atom)
181176
let scopeKey = lookupScopeKey(of: atom, isScopedOverriden: override?.isScoped ?? false)
182177
let key = AtomKey(atom, scopeKey: scopeKey)
183-
let state = lookupState(of: atom, for: key) ?? makeEphemeralState(of: atom)
178+
let state = getState(of: atom, for: key)
184179
let context = AtomCurrentContext(store: self, coordinator: state.coordinator)
185180
let value = await atom.refresh(context: context)
186181

187182
guard let transaction = state.transaction, let cache = lookupCache(of: atom, for: key) else {
183+
checkAndRelease(for: key)
188184
return value
189185
}
190186

@@ -214,10 +210,11 @@ internal struct StoreContext {
214210
let override = lookupOverride(of: atom)
215211
let scopeKey = lookupScopeKey(of: atom, isScopedOverriden: override?.isScoped ?? false)
216212
let key = AtomKey(atom, scopeKey: scopeKey)
217-
let state = lookupState(of: atom, for: key) ?? makeEphemeralState(of: atom)
218-
let context = AtomCurrentContext(store: self, coordinator: state.coordinator)
219213

220-
atom.reset(context: context)
214+
if let state = lookupState(of: atom, for: key) {
215+
let context = AtomCurrentContext(store: self, coordinator: state.coordinator)
216+
atom.reset(context: context)
217+
}
221218
}
222219

223220
@usableFromInline
@@ -292,9 +289,10 @@ internal struct StoreContext {
292289
private extension StoreContext {
293290
func prepareForTransaction<Node: Atom>(
294291
of atom: Node,
295-
for key: AtomKey,
296-
state: AtomState<Node.Coordinator>
292+
for key: AtomKey
297293
) -> AtomLoaderContext<Node.Loader.Value, Node.Loader.Coordinator> {
294+
let state = getState(of: atom, for: key)
295+
298296
// Terminate the ongoing transaction first.
299297
state.transaction?.terminate()
300298

@@ -386,8 +384,7 @@ private extension StoreContext {
386384
notifyUpdateToObservers()
387385
}
388386

389-
@discardableResult
390-
func checkAndRelease(for key: AtomKey) -> Bool {
387+
func checkAndRelease(for key: AtomKey) {
391388
// The condition under which an atom may be released are as follows:
392389
// 1. It's not marked as `KeepAlive`, is marked as `Scoped`, or is scoped by override.
393390
// 2. It has no downstream atoms.
@@ -398,11 +395,10 @@ private extension StoreContext {
398395
lazy var shouldRelease = !shouldKeepAlive && isChildrenEmpty && isSubscriptionEmpty
399396

400397
guard shouldRelease else {
401-
return false
398+
return
402399
}
403400

404401
release(for: key)
405-
return true
406402
}
407403

408404
func release(for key: AtomKey) {
@@ -427,16 +423,12 @@ private extension StoreContext {
427423
return state
428424
}
429425

430-
let state = makeEphemeralState(of: atom)
426+
let coordinator = atom.makeCoordinator()
427+
let state = AtomState(coordinator: coordinator)
431428
store.state.states[key] = state
432429
return state
433430
}
434431

435-
func makeEphemeralState<Node: Atom>(of atom: Node) -> AtomState<Node.Coordinator> {
436-
let coordinator = atom.makeCoordinator()
437-
return AtomState(coordinator: coordinator)
438-
}
439-
440432
func lookupState<Node: Atom>(of atom: Node, for key: AtomKey) -> AtomState<Node.Coordinator>? {
441433
guard let baseState = store.state.states[key] else {
442434
return nil
@@ -477,8 +469,7 @@ private extension StoreContext {
477469
for key: AtomKey,
478470
override: AtomOverride<Node>?
479471
) -> AtomCache<Node> {
480-
let state = getState(of: atom, for: key)
481-
let context = prepareForTransaction(of: atom, for: key, state: state)
472+
let context = prepareForTransaction(of: atom, for: key)
482473
let value: Node.Loader.Value
483474

484475
if let override {

Tests/AtomsTests/Core/StoreContextTests.swift

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,23 +142,12 @@ final class StoreContextTests: XCTestCase {
142142

143143
XCTAssertEqual(context.read(atom), 0)
144144
XCTAssertNil(store.state.caches[key])
145-
XCTAssertEqual(
146-
snapshots.map { $0.caches.mapValues { $0.value as? Int } },
147-
[
148-
[key: 0],
149-
[:],
150-
]
151-
)
145+
XCTAssertTrue(snapshots.isEmpty)
152146

153147
snapshots.removeAll()
154148
store.graph.children[key] = [AtomKey(TestAtom(value: 1))]
155149
XCTAssertEqual(context.read(atom), 0)
156-
XCTAssertEqual(
157-
snapshots.map { $0.caches.mapValues { $0.value as? Int } },
158-
[
159-
[key: 0]
160-
]
161-
)
150+
XCTAssertTrue(snapshots.isEmpty)
162151

163152
snapshots.removeAll()
164153
store.state.caches[key] = AtomCache(atom: atom, value: 1)
@@ -279,20 +268,14 @@ final class StoreContextTests: XCTestCase {
279268
XCTAssertNotNil(store.state.states[dependency0Key])
280269
XCTAssertTrue(snapshots.flatMap(\.caches).isEmpty)
281270

282-
snapshots.removeAll()
283271
transaction.terminate()
272+
284273
XCTAssertEqual(context.watch(dependency1, in: transaction), 1)
285274
XCTAssertEqual(store.graph.dependencies, [key: [dependency0Key]])
286275
XCTAssertEqual(store.graph.children, [dependency0Key: [key]])
287276
XCTAssertNil(store.state.caches[dependency1Key])
288277
XCTAssertNil(store.state.states[dependency1Key])
289-
XCTAssertEqual(
290-
snapshots.map { $0.caches.mapValues { $0.value as? Int } },
291-
[
292-
[dependency0Key: 0, dependency1Key: 1],
293-
[dependency0Key: 0],
294-
]
295-
)
278+
XCTAssertTrue(snapshots.isEmpty)
296279
}
297280

298281
@MainActor

0 commit comments

Comments
 (0)