|
| 1 | +import Combine |
1 | 2 | import Foundation
|
2 | 3 |
|
3 | 4 | /// An atom type that instantiates an observable object.
|
@@ -70,24 +71,85 @@ public extension ObservableObjectAtom {
|
70 | 71 | AtomProducer { context in
|
71 | 72 | context.transaction(object)
|
72 | 73 | } manageValue: { object, context in
|
73 |
| - var task: Task<Void, Never>? |
74 | 74 | let cancellable = object
|
75 | 75 | .objectWillChange
|
76 |
| - .sink { [weak object] _ in |
77 |
| - // Wait until the object's property is set, because `objectWillChange` |
78 |
| - // emits an event before the property is updated. |
79 |
| - task?.cancel() |
80 |
| - task = Task { @MainActor in |
81 |
| - if let object, !Task.isCancelled, !context.isTerminated { |
82 |
| - context.update(with: object) |
83 |
| - } |
| 76 | + .map { @Sendable _ in } |
| 77 | + .sinkLatest { [weak object] _ in |
| 78 | + // A custom subscriber is used here, encompassing the following |
| 79 | + // three behaviours. |
| 80 | + // |
| 81 | + // 1. It ensures that updates are performed on the main actor because `ObservableObject` |
| 82 | + // is not constrained to be isolated to the main actor. |
| 83 | + // 2. It always performs updates asynchronously to ensure the object to be updated as |
| 84 | + // `objectWillChange` emits events before the update. |
| 85 | + // 3. It adopts the latest event and cancels the previous update when successive events |
| 86 | + // arrive. |
| 87 | + if let object, !context.isTerminated { |
| 88 | + context.update(with: object) |
84 | 89 | }
|
85 | 90 | }
|
86 | 91 |
|
87 | 92 | context.onTermination = {
|
88 |
| - task?.cancel() |
89 | 93 | cancellable.cancel()
|
90 | 94 | }
|
91 | 95 | }
|
92 | 96 | }
|
93 | 97 | }
|
| 98 | + |
| 99 | +private extension Publisher where Output: Sendable, Failure == Never { |
| 100 | + func sinkLatest(receiveValue: @MainActor @escaping (Output) -> Void) -> AnyCancellable { |
| 101 | + let subscriber = Subscribers.SinkLatestOnMainActor(receiveValue: receiveValue) |
| 102 | + receive(subscriber: subscriber) |
| 103 | + return AnyCancellable(subscriber) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +private extension Subscribers { |
| 108 | + final class SinkLatestOnMainActor<Input: Sendable>: Combine.Subscriber, Cancellable { |
| 109 | + private var receiveValue: (@MainActor (Input) -> Void)? |
| 110 | + private var currentTask: Task<Void, Never>? |
| 111 | + private var lock = os_unfair_lock_s() |
| 112 | + |
| 113 | + init(receiveValue: @MainActor @escaping (Input) -> Void) { |
| 114 | + self.receiveValue = receiveValue |
| 115 | + } |
| 116 | + |
| 117 | + func receive(subscription: any Combine.Subscription) { |
| 118 | + subscription.request(.unlimited) |
| 119 | + } |
| 120 | + |
| 121 | + func receive(_ input: Input) -> Demand { |
| 122 | + withLock { |
| 123 | + guard let receiveValue else { |
| 124 | + return .none |
| 125 | + } |
| 126 | + |
| 127 | + currentTask?.cancel() |
| 128 | + currentTask = Task { @MainActor in |
| 129 | + guard !Task.isCancelled else { |
| 130 | + return |
| 131 | + } |
| 132 | + receiveValue(input) |
| 133 | + } |
| 134 | + |
| 135 | + return .unlimited |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + func receive(completion: Completion<Never>) {} |
| 140 | + |
| 141 | + func cancel() { |
| 142 | + withLock { |
| 143 | + currentTask?.cancel() |
| 144 | + currentTask = nil |
| 145 | + receiveValue = nil |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + func withLock<R>(_ body: () -> R) -> R { |
| 150 | + os_unfair_lock_lock(&lock) |
| 151 | + defer { os_unfair_lock_unlock(&lock) } |
| 152 | + return body() |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments