杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
//  Infallible+Create.swift
//  RxSwift
//
//  Created by Shai Mishali on 27/08/2020.
//  Copyright © 2020 Krunoslav Zaher. All rights reserved.
//
 
import Foundation
 
public enum InfallibleEvent<Element> {
    /// Next element is produced.
    case next(Element)
 
    /// Sequence completed successfully.
    case completed
}
 
extension Infallible {
    public typealias InfallibleObserver = (InfallibleEvent<Element>) -> Void
 
    /**
     Creates an observable sequence from a specified subscribe method implementation.
 
     - seealso: [create operator on reactivex.io](http://reactivex.io/documentation/operators/create.html)
 
     - parameter subscribe: Implementation of the resulting observable sequence's `subscribe` method.
     - returns: The observable sequence with the specified implementation for the `subscribe` method.
     */
    public static func create(subscribe: @escaping (@escaping InfallibleObserver) -> Disposable) -> Infallible<Element> {
        let source = Observable<Element>.create { observer in
            subscribe { event in
                switch event {
                case .next(let element):
                    observer.onNext(element)
                case .completed:
                    observer.onCompleted()
                }
            }
        }
 
        return Infallible(source)
    }
}
 
extension InfallibleEvent: EventConvertible {
    public var event: Event<Element> {
        switch self {
        case let .next(element):
            return .next(element)
        case .completed:
            return .completed
        }
    }
}