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 運算
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門暂题,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人究珊,你說我怎么就攤上這事薪者。” “怎么了剿涮?”我有些...
- 文/不壞的土叔 我叫張陵言津,是天一觀的道長。 經(jīng)常有香客問我取试,道長悬槽,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任瞬浓,我火速辦了婚禮初婆,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己磅叛,他們只是感情好屑咳,可當我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著弊琴,像睡著了一般兆龙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上访雪,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼脂倦!你這毒婦竟也來了番宁?” 一聲冷哼從身側響起,我...
- 正文 年R本政府宣布烘浦,位于F島的核電站,受9級特大地震影響杉适,放射性物質發(fā)生泄漏谎倔。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一猿推、第九天 我趴在偏房一處隱蔽的房頂上張望片习。 院中可真熱鬧捌肴,春花似錦、人聲如沸藕咏。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽孽查。三九已至饥悴,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間盲再,已是汗流浹背西设。 一陣腳步聲響...
推薦閱讀更多精彩內容
- 1. 賦值運算符 "=" Swift賦值語句不可作為條件判斷語句 2.基礎運算符 "+ - * / %" 3.單目...
- 一元運算符 delete delete 運算符刪除對以前定義的對象屬性或方法的引用洪规。例如: delete運算符不能...
- 關系運算符 關系運算符小于印屁、大于、小于等于和大于等于執(zhí)行的是兩個數(shù)的比較運算斩例,比較方式與算術比較運算相同雄人。每個關系...
- 本來不想再寫這個話題,可是看到消息實在是氣炸了驻民,昨天多少女同胞都在替謝杏芳不值翻具,大罵垃圾渣男林丹,今天我們的女同胞...