|
| 1 | +// |
| 2 | +// Completable.swift |
| 3 | +// RxSwift |
| 4 | +// |
| 5 | +// Created by sergdort on 19/08/2017. |
| 6 | +// Copyright © 2017 Krunoslav Zaher. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +/// Sequence containing 0 elements |
| 10 | +public enum CompletableTrait { } |
| 11 | +/// Represents a push style sequence containing 0 elements. |
| 12 | +public typealias Completable = PrimitiveSequence<CompletableTrait, Swift.Never> |
| 13 | + |
| 14 | +public enum CompletableEvent { |
| 15 | + /// Sequence terminated with an error. (underlying observable sequence emits: `.error(Error)`) |
| 16 | + case error(Swift.Error) |
| 17 | + |
| 18 | + /// Sequence completed successfully. |
| 19 | + case completed |
| 20 | +} |
| 21 | + |
| 22 | +public extension PrimitiveSequenceType where TraitType == CompletableTrait, ElementType == Swift.Never { |
| 23 | + public typealias CompletableObserver = (CompletableEvent) -> () |
| 24 | + |
| 25 | + /** |
| 26 | + Creates an observable sequence from a specified subscribe method implementation. |
| 27 | + |
| 28 | + - seealso: [create operator on reactivex.io](http://reactivex.io/documentation/operators/create.html) |
| 29 | + |
| 30 | + - parameter subscribe: Implementation of the resulting observable sequence's `subscribe` method. |
| 31 | + - returns: The observable sequence with the specified implementation for the `subscribe` method. |
| 32 | + */ |
| 33 | + public static func create(subscribe: @escaping (@escaping CompletableObserver) -> Disposable) -> PrimitiveSequence<TraitType, ElementType> { |
| 34 | + let source = Observable<ElementType>.create { observer in |
| 35 | + return subscribe { event in |
| 36 | + switch event { |
| 37 | + case .error(let error): |
| 38 | + observer.on(.error(error)) |
| 39 | + case .completed: |
| 40 | + observer.on(.completed) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return PrimitiveSequence(raw: source) |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + Subscribes `observer` to receive events for this sequence. |
| 50 | + |
| 51 | + - returns: Subscription for `observer` that can be used to cancel production of sequence elements and free resources. |
| 52 | + */ |
| 53 | + public func subscribe(_ observer: @escaping (CompletableEvent) -> ()) -> Disposable { |
| 54 | + var stopped = false |
| 55 | + return self.primitiveSequence.asObservable().subscribe { event in |
| 56 | + if stopped { return } |
| 57 | + stopped = true |
| 58 | + |
| 59 | + switch event { |
| 60 | + case .next: |
| 61 | + rxFatalError("Completables can't emit values") |
| 62 | + case .error(let error): |
| 63 | + observer(.error(error)) |
| 64 | + case .completed: |
| 65 | + observer(.completed) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + Subscribes a completion handler and an error handler for this sequence. |
| 72 | + |
| 73 | + - parameter onCompleted: Action to invoke upon graceful termination of the observable sequence. |
| 74 | + - parameter onError: Action to invoke upon errored termination of the observable sequence. |
| 75 | + - returns: Subscription object used to unsubscribe from the observable sequence. |
| 76 | + */ |
| 77 | + public func subscribe(onCompleted: (() -> Void)? = nil, onError: ((Swift.Error) -> Void)? = nil) -> Disposable { |
| 78 | + return self.primitiveSequence.subscribe { event in |
| 79 | + switch event { |
| 80 | + case .error(let error): |
| 81 | + onError?(error) |
| 82 | + case .completed: |
| 83 | + onCompleted?() |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +public extension PrimitiveSequenceType where TraitType == CompletableTrait, ElementType == Swift.Never { |
| 90 | + /** |
| 91 | + Returns an observable sequence that terminates with an `error`. |
| 92 | + |
| 93 | + - seealso: [throw operator on reactivex.io](http://reactivex.io/documentation/operators/empty-never-throw.html) |
| 94 | + |
| 95 | + - returns: The observable sequence that terminates with specified error. |
| 96 | + */ |
| 97 | + public static func error(_ error: Swift.Error) -> Completable { |
| 98 | + return PrimitiveSequence(raw: Observable.error(error)) |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + Returns a non-terminating observable sequence, which can be used to denote an infinite duration. |
| 103 | + |
| 104 | + - seealso: [never operator on reactivex.io](http://reactivex.io/documentation/operators/empty-never-throw.html) |
| 105 | + |
| 106 | + - returns: An observable sequence whose observers will never get called. |
| 107 | + */ |
| 108 | + public static func never() -> Completable { |
| 109 | + return PrimitiveSequence(raw: Observable.never()) |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + Returns an empty observable sequence, using the specified scheduler to send out the single `Completed` message. |
| 114 | + |
| 115 | + - seealso: [empty operator on reactivex.io](http://reactivex.io/documentation/operators/empty-never-throw.html) |
| 116 | + |
| 117 | + - returns: An observable sequence with no elements. |
| 118 | + */ |
| 119 | + public static func empty() -> Completable { |
| 120 | + return Completable(raw: Observable.empty()) |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +public extension PrimitiveSequenceType where TraitType == CompletableTrait, ElementType == Swift.Never { |
| 126 | + /** |
| 127 | + Invokes an action for each event in the observable sequence, and propagates all observer messages through the result sequence. |
| 128 | + |
| 129 | + - seealso: [do operator on reactivex.io](http://reactivex.io/documentation/operators/do.html) |
| 130 | + |
| 131 | + - parameter onNext: Action to invoke for each element in the observable sequence. |
| 132 | + - parameter onError: Action to invoke upon errored termination of the observable sequence. |
| 133 | + - parameter onCompleted: Action to invoke upon graceful termination of the observable sequence. |
| 134 | + - parameter onSubscribe: Action to invoke before subscribing to source observable sequence. |
| 135 | + - parameter onSubscribed: Action to invoke after subscribing to source observable sequence. |
| 136 | + - parameter onDispose: Action to invoke after subscription to source observable has been disposed for any reason. It can be either because sequence terminates for some reason or observer subscription being disposed. |
| 137 | + - returns: The source sequence with the side-effecting behavior applied. |
| 138 | + */ |
| 139 | + public func `do`(onError: ((Swift.Error) throws -> Void)? = nil, |
| 140 | + onCompleted: (() throws -> Void)? = nil, |
| 141 | + onSubscribe: (() -> ())? = nil, |
| 142 | + onSubscribed: (() -> ())? = nil, |
| 143 | + onDispose: (() -> ())? = nil) |
| 144 | + -> Completable { |
| 145 | + return Completable(raw: primitiveSequence.source.do( |
| 146 | + onError: onError, |
| 147 | + onCompleted: onCompleted, |
| 148 | + onSubscribe: onSubscribe, |
| 149 | + onSubscribed: onSubscribed, |
| 150 | + onDispose: onDispose) |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + /** |
| 157 | + Concatenates the second observable sequence to `self` upon successful termination of `self`. |
| 158 | + |
| 159 | + - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html) |
| 160 | + |
| 161 | + - parameter second: Second observable sequence. |
| 162 | + - returns: An observable sequence that contains the elements of `self`, followed by those of the second sequence. |
| 163 | + */ |
| 164 | + public func concat(_ second: Completable) -> Completable { |
| 165 | + return Completable.concat(primitiveSequence, second) |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully. |
| 170 | + |
| 171 | + - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html) |
| 172 | + |
| 173 | + - returns: An observable sequence that contains the elements of each given sequence, in sequential order. |
| 174 | + */ |
| 175 | + public static func concat<S: Sequence>(_ sequence: S) -> Completable |
| 176 | + where S.Iterator.Element == Completable { |
| 177 | + let source = Observable.concat(sequence.lazy.map { $0.asObservable() }) |
| 178 | + return Completable(raw: source) |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully. |
| 183 | + |
| 184 | + - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html) |
| 185 | + |
| 186 | + - returns: An observable sequence that contains the elements of each given sequence, in sequential order. |
| 187 | + */ |
| 188 | + public static func concat<C: Collection>(_ collection: C) -> Completable |
| 189 | + where C.Iterator.Element == Completable { |
| 190 | + let source = Observable.concat(collection.map { $0.asObservable() }) |
| 191 | + return Completable(raw: source) |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + Concatenates all observable sequences in the given sequence, as long as the previous observable sequence terminated successfully. |
| 196 | + |
| 197 | + - seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html) |
| 198 | + |
| 199 | + - returns: An observable sequence that contains the elements of each given sequence, in sequential order. |
| 200 | + */ |
| 201 | + public static func concat(_ sources: Completable ...) -> Completable { |
| 202 | + let source = Observable.concat(sources.map { $0.asObservable() }) |
| 203 | + return Completable(raw: source) |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + Merges elements from all observable sequences from collection into a single observable sequence. |
| 208 | + |
| 209 | + - seealso: [merge operator on reactivex.io](http://reactivex.io/documentation/operators/merge.html) |
| 210 | + |
| 211 | + - parameter sources: Collection of observable sequences to merge. |
| 212 | + - returns: The observable sequence that merges the elements of the observable sequences. |
| 213 | + */ |
| 214 | + public static func merge<C: Collection>(_ sources: C) -> Completable |
| 215 | + where C.Iterator.Element == Completable { |
| 216 | + let source = Observable.merge(sources.map { $0.asObservable() }) |
| 217 | + return Completable(raw: source) |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + Merges elements from all observable sequences from array into a single observable sequence. |
| 222 | + |
| 223 | + - seealso: [merge operator on reactivex.io](http://reactivex.io/documentation/operators/merge.html) |
| 224 | + |
| 225 | + - parameter sources: Array of observable sequences to merge. |
| 226 | + - returns: The observable sequence that merges the elements of the observable sequences. |
| 227 | + */ |
| 228 | + public static func merge(_ sources: [Completable]) -> Completable { |
| 229 | + let source = Observable.merge(sources.map { $0.asObservable() }) |
| 230 | + return Completable(raw: source) |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + Merges elements from all observable sequences into a single observable sequence. |
| 235 | + |
| 236 | + - seealso: [merge operator on reactivex.io](http://reactivex.io/documentation/operators/merge.html) |
| 237 | + |
| 238 | + - parameter sources: Collection of observable sequences to merge. |
| 239 | + - returns: The observable sequence that merges the elements of the observable sequences. |
| 240 | + */ |
| 241 | + public static func merge(_ sources: Completable...) -> Completable { |
| 242 | + let source = Observable.merge(sources.map { $0.asObservable() }) |
| 243 | + return Completable(raw: source) |
| 244 | + } |
| 245 | +} |
0 commit comments