withLatestFrom 运算

extension ObservableType {
    public func withLatestFrom(second: Second0)->Observable {
        return WithLatestFrom(first: asObservable(), second: second.asObservable(), resultSelector: { $ 1} )
    }

    public func withLatestFrom(second: Second0, resultSelector: (E, Second0.E) throws->ResultType) ->Observable {
        return WithLatestFrom(first: asObservable(), second: second.asObservable(), resultSelector: resultSelector)
    }
}

class WithLatestFrom: Producer {
    typealias ResultSelector = (FirstType, SecondType) throws -> ResultType

    private let _first: Observable
    private let _second: Observable
    private let _resultSelector: ResultSelector

    init(first: Observable, second: Observable, resultSelector: ResultSelector) {
        _first = first
        _second = second
        _resultSelector = resultSelector
    }

    override fun run(observer: O)->Disposable {
        let sink = WithLatestFromSink(parent: self, observer: observer)
        sink.disposable = sink.run()
        return sink
    }
}

class WithLatestFromSink: Sink, ObserverType, SynchronizedOnType, LockOwnerType {
    typealias Parent = WithLatestFrom
    typealias E = FirstType

    private let _parent : Parent

    var _lock = NSRecursiveLock()
    private var _latest: SecondType?

    init(parent: Parent, observer: O) {
        _parent = parent
        super.init(observer: observer)
    }

    func run()->Disposable {
        let sndSubscription = SingleAssignmentDisposable()
        let sndo = WithLatestFromSecond(parent: self, disposable: sndSubscription)

        sndSubscription.disposable = _parent._second.subscribe(sndo)
        let fstSubscription = _parent._first.subscribe(self)

        return StableCompositeDisposable.create(fstSubscription, sndSubscription)
    }

    func on(event: Event) {
        synchronizedOn(event)
    }

    func _synchronized_on(event: Event) {
        switch event {
        cae let .Next(value):
            guard let latest = _latest else {return}
            do {
                let res = try _parent._resultSelector(value, latest)
                forwardOn(.Next(res))
            }catch let e {
                forwardOn(.Error(e))
                dispose()
            }
        case .Completed:
            forwardOn(.Completed)
            dispose()
        case let .Error(error):
            forwardOn(.Error(error))
            dispose()
        }
    }
}    

class WithLatestFromSecond: ObserverType, LockOwnerType, SynchronizedOnType {
    typealias Parent = WithLatestFromSink
    typealias E = SecondType

    private let _parent: parent
    private let _disposable: Disposable

    var _lock: NSRecursiveLock {
        get {
            return _parent._lock
        }
    }

    init(parent: parent, disposable: Disposable) {
          _parent = parent
          _disposable = disposable
    }

    func on(event: Event) {
        _synchronized_on(event)
    }

    func _synchronized_on(event: Event) {
        switch event {
        case let .Next(value):
            _parent._latest = value
        case .Completed:
            _disposable.dispose()
        case let .Error(error):
            _parent.forwardOn(.Error(error))
            _parent.dispose()
        }
    }
}

你可能感兴趣的:(withLatestFrom 运算)