Skip to content

Commit 09e2f92

Browse files
committed
Gardening
1 parent 9220b9a commit 09e2f92

File tree

6 files changed

+50
-38
lines changed

6 files changed

+50
-38
lines changed

Sources/Atoms/Context/AtomRelationContext.swift

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ public struct AtomRelationContext: AtomWatchableContext {
189189
/// - Parameter object: An object that to be retained.
190190
@inlinable
191191
public func keepUntilTermination<Object: AnyObject>(_ object: Object) {
192-
_box.keepUntilTermination(object)
192+
let retainer = ObjectRetainer(object)
193+
addTermination(retainer.release)
193194
}
194195
}
195196

@@ -200,23 +201,10 @@ internal protocol _AnyAtomRelationContextBox {
200201

201202
func watch<Node: Atom>(_ atom: Node, shouldNotifyAfterUpdates: Bool) -> Node.State.Value
202203
func addTermination(_ termination: @MainActor @escaping () -> Void)
203-
func keepUntilTermination<Object: AnyObject>(_ object: Object)
204204
}
205205

206206
@usableFromInline
207207
internal struct _AtomRelationContextBox<Caller: Atom>: _AnyAtomRelationContextBox {
208-
final class Retainer<Object: AnyObject> {
209-
private var object: Object?
210-
211-
init(_ object: Object) {
212-
self.object = object
213-
}
214-
215-
func release() {
216-
object = nil
217-
}
218-
}
219-
220208
let caller: Caller
221209

222210
@usableFromInline
@@ -235,10 +223,19 @@ internal struct _AtomRelationContextBox<Caller: Atom>: _AnyAtomRelationContextBo
235223
func addTermination(_ termination: @MainActor @escaping () -> Void) {
236224
store.addTermination(caller, termination: termination)
237225
}
226+
}
227+
228+
@usableFromInline
229+
internal final class ObjectRetainer<Object: AnyObject> {
230+
private var object: Object?
231+
232+
@usableFromInline
233+
init(_ object: Object) {
234+
self.object = object
235+
}
238236

239237
@usableFromInline
240-
func keepUntilTermination<Object: AnyObject>(_ object: Object) {
241-
let retainer = Retainer(object)
242-
store.addTermination(caller, termination: retainer.release)
238+
func release() {
239+
object = nil
243240
}
244241
}

Sources/Atoms/Context/AtomTestContext.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ public struct AtomTestContext: AtomWatchableContext {
2525
/// Waits until any of atoms watched through this context is updated for up to
2626
/// the specified timeout, and then return a boolean value indicating whether an update is done.
2727
///
28+
/// ```swift
29+
/// func testAsyncUpdate() async {
30+
/// let context = AtomTestContext()
31+
///
32+
/// let initialPhase = context.watch(AsyncCalculationAtom().phase)
33+
/// XCTAssertEqual(initialPhase, .suspending)
34+
///
35+
/// let didUpdate = await context.waitUntilNextUpdate()
36+
/// let currentPhase = context.watch(AsyncCalculationAtom().phase)
37+
///
38+
/// XCTAssertTure(didUpdate)
39+
/// XCTAssertEqual(currentPhase, .success(123))
40+
/// }
41+
/// ```
42+
///
2843
/// - Parameter interval: The maximum timeout interval that this function can wait until
2944
/// the next update. The default timeout interval is `60`.
3045
/// - Returns: A boolean value indicating whether an update is done.

Sources/Atoms/Core/Internal/Store.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private extension Store {
153153
}
154154

155155
// Notify the initial update to observers.
156-
notifyObserversOfUpdate(of: atom, state: state)
156+
notifyChangesToObservers(of: atom, state: state)
157157

158158
return state
159159
}
@@ -180,7 +180,7 @@ private extension Store {
180180

181181
host.onUpdate = { state in
182182
// Notify the update to observers.
183-
notifyObserversOfUpdate(of: atom, state: state)
183+
notifyChangesToObservers(of: atom, state: state)
184184
}
185185

186186
guard let container = container else {
@@ -231,7 +231,7 @@ private extension Store {
231231
return host
232232
}
233233

234-
func notifyObserversOfUpdate<Node: Atom>(
234+
func notifyChangesToObservers<Node: Atom>(
235235
of atom: Node,
236236
state: Node.State
237237
) {

Sources/Atoms/Core/State/AsyncSequenceAtomState.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ public final class AsyncSequenceAtomState<Sequence: AsyncSequence>: RefreshableA
2222
do {
2323
for try await element in box.unboxed {
2424
if !Task.isCancelled {
25-
self.phase = .success(element)
25+
phase = .success(element)
2626
context.notifyUpdate()
2727
}
2828
}
2929
}
3030
catch {
3131
if !Task.isCancelled {
32-
self.phase = .failure(error)
32+
phase = .failure(error)
3333
context.notifyUpdate()
3434
}
3535
}
3636
}
3737
context.addTermination(task.cancel)
3838

39-
let phase = Value.suspending
40-
self.phase = phase
39+
let initialPhase = Value.suspending
40+
phase = initialPhase
4141

42-
return phase
42+
return initialPhase
4343
}
4444

4545
/// Overrides the value with an arbitrary value.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// A state that is actual implementation of `ModifiedAtom`.
22
public final class ModifiedAtomState<Node: Atom, Modifier: AtomModifier>: AtomState where Node.State.Value == Modifier.Value {
3-
private var modified: Modifier.ModifiedValue?
3+
private var modifiedValue: Modifier.ModifiedValue?
44
private let atom: Node
55
private let modifier: Modifier
66

@@ -11,21 +11,21 @@ public final class ModifiedAtomState<Node: Atom, Modifier: AtomModifier>: AtomSt
1111

1212
/// Returns a value with initiating the update process and caches the value for the next access.
1313
public func value(context: Context) -> Modifier.ModifiedValue {
14-
if let modified = modified {
15-
return modified
14+
if let modifiedValue = modifiedValue {
15+
return modifiedValue
1616
}
1717

1818
let value = context.atomContext.watch(atom)
19-
let modified = modifier.value(context: context, with: value) { [weak self] modified in
20-
self?.modified = modified
19+
let modifiedValue = modifier.value(context: context, with: value) { [weak self] modifiedValue in
20+
self?.modifiedValue = modifiedValue
2121
}
22-
self.modified = modified
22+
self.modifiedValue = modifiedValue
2323

24-
return modified
24+
return modifiedValue
2525
}
2626

2727
/// Overrides the value with an arbitrary value.
28-
public func override(with modified: Modifier.ModifiedValue, context: Context) {
29-
self.modified = modified
28+
public func override(with modifiedValue: Modifier.ModifiedValue, context: Context) {
29+
self.modifiedValue = modifiedValue
3030
}
3131
}

Sources/Atoms/Core/State/PublisherAtomState.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ public final class PublisherAtomState<Publisher: Combine.Publisher>: Refreshable
2323
let task = Task {
2424
for await result in box.unboxed {
2525
if !Task.isCancelled {
26-
self.phase = AsyncPhase(result)
26+
phase = AsyncPhase(result)
2727
context.notifyUpdate()
2828
}
2929
}
3030
}
3131
context.addTermination(task.cancel)
3232

33-
let phase = Value.suspending
34-
self.phase = phase
33+
let initialPhase = Value.suspending
34+
phase = initialPhase
3535

36-
return phase
36+
return initialPhase
3737
}
3838

3939
/// Overrides the value with an arbitrary value.

0 commit comments

Comments
 (0)