extension ObservableType {
public func withLatestFrom<Second0: ObservableConvertibleType>(second: Second0)->Observable<Second0.E> {
return WithLatestFrom(first: asObservable(), second: second.asObservable(), resultSelector: { $ 1} )
}
public func withLatestFrom<Second0: ObservableConvertibleType, ResultType>(second: Second0, resultSelector: (E, Second0.E) throws->ResultType) ->Observable<ResultType> {
return WithLatestFrom(first: asObservable(), second: second.asObservable(), resultSelector: resultSelector)
}
}
class WithLatestFrom<FirstType, SecondType, ResultType>: Producer<ResultType> {
typealias ResultSelector = (FirstType, SecondType) throws -> ResultType
private let _first: Observable<FirstType>
private let _second: Observable<SecondType>
private let _resultSelector: ResultSelector
init(first: Observable<FirstType>, second: Observable<SecondType>, resultSelector: ResultSelector) {
_first = first
_second = second
_resultSelector = resultSelector
}
override fun run<O: ObserverType where O.E = ResultType>(observer: O)->Disposable {
let sink = WithLatestFromSink(parent: self, observer: observer)
sink.disposable = sink.run()
return sink
}
}
class WithLatestFromSink<FirstType, SecondType, ResultType, O: ObserverType where O.E == ResultType>: Sink<O>, ObserverType, SynchronizedOnType, LockOwnerType {
typealias Parent = WithLatestFrom<FirstType, SecondType, ResultType>
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<E>) {
synchronizedOn(event)
}
func _synchronized_on(event: Event<E>) {
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<FirstType, SecondType, ResultType, O: ObserverType where O.E == ResultType>: ObserverType, LockOwnerType, SynchronizedOnType {
typealias Parent = WithLatestFromSink<FirstType, SecondType, ResultType, O>
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<E>) {
_synchronized_on(event)
}
func _synchronized_on(event: Event<E>) {
switch event {
case let .Next(value):
_parent._latest = value
case .Completed:
_disposable.dispose()
case let .Error(error):
_parent.forwardOn(.Error(error))
_parent.dispose()
}
}
}
withLatestFrom 运算
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. 赋值运算符 "=" Swift赋值语句不可作为条件判断语句 2.基础运算符 "+ - * / %" 3.单目...
- 一元运算符 delete delete 运算符删除对以前定义的对象属性或方法的引用。例如: delete运算符不能...
- 关系运算符 关系运算符小于、大于、小于等于和大于等于执行的是两个数的比较运算,比较方式与算术比较运算相同。每个关系...