|
| 1 | +#if canImport(Combine) |
| 2 | +import Combine |
| 3 | + |
| 4 | +extension SignalProducerConvertible { |
| 5 | + @available(macOS 10.15, *) |
| 6 | + @available(iOS 13.0, *) |
| 7 | + @available(tvOS 13.0, *) |
| 8 | + @available(macCatalyst 13.0, *) |
| 9 | + @available(watchOS 6.0, *) |
| 10 | + public func publisher() -> ProducerPublisher<Value, Error> { |
| 11 | + ProducerPublisher(base: producer) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +@available(macOS 10.15, *) |
| 16 | +@available(iOS 13.0, *) |
| 17 | +@available(tvOS 13.0, *) |
| 18 | +@available(macCatalyst 13.0, *) |
| 19 | +@available(watchOS 6.0, *) |
| 20 | +public struct ProducerPublisher<Output, Failure: Swift.Error>: Publisher { |
| 21 | + public let base: SignalProducer<Output, Failure> |
| 22 | + |
| 23 | + public init(base: SignalProducer<Output, Failure>) { |
| 24 | + self.base = base |
| 25 | + } |
| 26 | + |
| 27 | + public func receive<S>(subscriber: S) where S : Subscriber, Output == S.Input, Failure == S.Failure { |
| 28 | + let subscription = Subscription(subscriber: subscriber, base: base) |
| 29 | + subscription.bootstrap() |
| 30 | + } |
| 31 | + |
| 32 | + final class Subscription<S: Subscriber>: Combine.Subscription where Output == S.Input, Failure == S.Failure { |
| 33 | + let subscriber: S |
| 34 | + let base: SignalProducer<Output, Failure> |
| 35 | + let state: Atomic<State> |
| 36 | + |
| 37 | + init(subscriber: S, base: SignalProducer<Output, Failure>) { |
| 38 | + self.subscriber = subscriber |
| 39 | + self.base = base |
| 40 | + self.state = Atomic(State()) |
| 41 | + } |
| 42 | + |
| 43 | + func bootstrap() { |
| 44 | + subscriber.receive(subscription: self) |
| 45 | + } |
| 46 | + |
| 47 | + func request(_ incoming: Subscribers.Demand) { |
| 48 | + let response: DemandResponse = state.modify { state in |
| 49 | + guard state.hasCancelled == false else { |
| 50 | + return .noAction |
| 51 | + } |
| 52 | + |
| 53 | + guard state.hasStarted else { |
| 54 | + state.hasStarted = true |
| 55 | + state.requested = incoming |
| 56 | + return .startUpstream |
| 57 | + } |
| 58 | + |
| 59 | + state.requested = state.requested + incoming |
| 60 | + let unsatified = state.requested - state.satisfied |
| 61 | + |
| 62 | + if let max = unsatified.max { |
| 63 | + let dequeueCount = Swift.min(state.buffer.count, max) |
| 64 | + state.satisfied += dequeueCount |
| 65 | + |
| 66 | + defer { state.buffer.removeFirst(dequeueCount) } |
| 67 | + return .satisfyDemand(Array(state.buffer.prefix(dequeueCount))) |
| 68 | + } else { |
| 69 | + defer { state.buffer = [] } |
| 70 | + return .satisfyDemand(state.buffer) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + switch response { |
| 75 | + case let .satisfyDemand(output): |
| 76 | + var demand: Subscribers.Demand = .none |
| 77 | + |
| 78 | + for output in output { |
| 79 | + demand += subscriber.receive(output) |
| 80 | + } |
| 81 | + |
| 82 | + if demand != .none { |
| 83 | + request(demand) |
| 84 | + } |
| 85 | + |
| 86 | + case .startUpstream: |
| 87 | + let disposable = base.start { [weak self] event in |
| 88 | + guard let self = self else { return } |
| 89 | + |
| 90 | + switch event { |
| 91 | + case let .value(output): |
| 92 | + let (shouldSendImmediately, isDemandUnlimited): (Bool, Bool) = self.state.modify { state in |
| 93 | + guard state.hasCancelled == false else { return (false, false) } |
| 94 | + |
| 95 | + let unsatified = state.requested - state.satisfied |
| 96 | + |
| 97 | + if let count = unsatified.max, count >= 1 { |
| 98 | + assert(state.buffer.count == 0) |
| 99 | + state.satisfied += 1 |
| 100 | + return (true, false) |
| 101 | + } else if unsatified == .unlimited { |
| 102 | + assert(state.buffer.isEmpty) |
| 103 | + return (true, true) |
| 104 | + } else { |
| 105 | + assert(state.requested == state.satisfied) |
| 106 | + state.buffer.append(output) |
| 107 | + return (false, false) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if shouldSendImmediately { |
| 112 | + let demand = self.subscriber.receive(output) |
| 113 | + |
| 114 | + if isDemandUnlimited == false && demand != .none { |
| 115 | + self.request(demand) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + case .completed, .interrupted: |
| 120 | + self.cancel() |
| 121 | + self.subscriber.receive(completion: .finished) |
| 122 | + |
| 123 | + case let .failed(error): |
| 124 | + self.cancel() |
| 125 | + self.subscriber.receive(completion: .failure(error)) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + let shouldDispose: Bool = state.modify { state in |
| 130 | + guard state.hasCancelled == false else { return true } |
| 131 | + state.producerSubscription = disposable |
| 132 | + return false |
| 133 | + } |
| 134 | + |
| 135 | + if shouldDispose { |
| 136 | + disposable.dispose() |
| 137 | + } |
| 138 | + |
| 139 | + case .noAction: |
| 140 | + break |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + func cancel() { |
| 145 | + let disposable = state.modify { $0.cancel() } |
| 146 | + disposable?.dispose() |
| 147 | + } |
| 148 | + |
| 149 | + struct State { |
| 150 | + var requested: Subscribers.Demand = .none |
| 151 | + var satisfied: Subscribers.Demand = .none |
| 152 | + |
| 153 | + var buffer: [Output] = [] |
| 154 | + |
| 155 | + var producerSubscription: Disposable? |
| 156 | + var hasStarted = false |
| 157 | + var hasCancelled = false |
| 158 | + |
| 159 | + init() { |
| 160 | + producerSubscription = nil |
| 161 | + hasStarted = false |
| 162 | + hasCancelled = false |
| 163 | + } |
| 164 | + |
| 165 | + mutating func cancel() -> Disposable? { |
| 166 | + hasCancelled = true |
| 167 | + defer { producerSubscription = nil } |
| 168 | + return producerSubscription |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + enum DemandResponse { |
| 173 | + case startUpstream |
| 174 | + case satisfyDemand([Output]) |
| 175 | + case noAction |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | +#endif |
0 commit comments