Skip to content

Commit 88411e7

Browse files
committed
Do not notify update if the object is already terminated
1 parent 7906fd8 commit 88411e7

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Sources/Atoms/Core/Loader/AtomLoaderContext.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// The context structure to interact with an atom store.
22
@MainActor
33
public struct AtomLoaderContext<Value, Coordinator> {
4-
internal let store: StoreContext
5-
internal let transaction: Transaction
6-
internal let coordinator: Coordinator
7-
internal let update: @MainActor (Value) -> Void
4+
private let store: StoreContext
5+
private let transaction: Transaction
6+
private let coordinator: Coordinator
7+
private let update: @MainActor (Value) -> Void
88

99
internal init(
1010
store: StoreContext,
@@ -18,6 +18,10 @@ public struct AtomLoaderContext<Value, Coordinator> {
1818
self.update = update
1919
}
2020

21+
internal var isTerminated: Bool {
22+
transaction.isTerminated
23+
}
24+
2125
internal var modifierContext: AtomModifierContext<Value> {
2226
AtomModifierContext(transaction: transaction) { value in
2327
update(with: value)

Sources/Atoms/Core/Loader/ObservableObjectAtomLoader.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public struct ObservableObjectAtomLoader<Node: ObservableObjectAtom>: AtomLoader
2929
// Wait until the object's property is set, because `objectWillChange`
3030
// emits an event before the property is updated.
3131
RunLoop.main.perform(inModes: [.common]) {
32-
context.update(with: value)
32+
if !context.isTerminated {
33+
context.update(with: value)
34+
}
3335
}
3436
}
3537

Sources/Atoms/Core/StoreContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ internal struct StoreContext {
168168
}
169169

170170
// Notify update unless it's cancelled or terminated by other operations.
171-
if !Task.isCancelled && !context.transaction.isTerminated {
171+
if !Task.isCancelled && !context.isTerminated {
172172
update(atom: atom, for: key, newValue: value, cache: cache)
173173
}
174174

0 commit comments

Comments
 (0)