extension Observable {
@warn_unused_result(message="http://git.io/rxs.uo")
public static func zip<O1: ObservableType, O2: ObservableType, O3: ObservableType, O4: ObservableType, O5: ObservableType>
(source1: O1, _ source2: O2, _ source3: O3, _ source4: O4, _ source5: O5, resultSelector: (O1.E, O2.E, O3.E, O4.E, O5.E) throws -> E)
-> Observable<E> {
return Zip5(
source1: source1.asObservable(),
source2: source2.asObservable(),
source3: source3.asObservable(),
source4: source4.asObservable(),
source5: source5.asObservable(),
resultSelector: resultSelector
)
}
}
class ZipSink5_<E1, E2, E3, E4, E5, O: ObserverType> : ZipSink<O> {
typealias R = O.E
typealias Parent = Zip5<E1, E2, E3, E4, E5, R>
let _parent: Parent
var _values1: Queue<E1> = Queue(capacity: 2)
var _values2: Queue<E2> = Queue(capacity: 2)
var _values3: Queue<E3> = Queue(capacity: 2)
var _values4: Queue<E4> = Queue(capacity: 2)
var _values5: Queue<E5> = Queue(capacity: 2)
init(parent: Parent, observer: O) {
_parent = parent
super.init(arity: 5, observer: observer)
}
override func hasElements(index: Int) -> Bool {
switch (index) {
case 0: return _values1.count > 0
case 1: return _values2.count > 0
case 2: return _values3.count > 0
case 3: return _values4.count > 0
case 4: return _values5.count > 0
default:
rxFatalError("Unhandled case (Function)")
}
return false
}
func run() -> Disposable {
let subscription1 = SingleAssignmentDisposable()
let subscription2 = SingleAssignmentDisposable()
let subscription3 = SingleAssignmentDisposable()
let subscription4 = SingleAssignmentDisposable()
let subscription5 = SingleAssignmentDisposable()
let observer1 = ZipObserver(lock: _lock, parent: self, index: 0, setNextValue: { self._values1.enqueue($0) }, this: subscription1)
let observer2 = ZipObserver(lock: _lock, parent: self, index: 1, setNextValue: { self._values2.enqueue($0) }, this: subscription2)
let observer3 = ZipObserver(lock: _lock, parent: self, index: 2, setNextValue: { self._values3.enqueue($0) }, this: subscription3)
let observer4 = ZipObserver(lock: _lock, parent: self, index: 3, setNextValue: { self._values4.enqueue($0) }, this: subscription4)
let observer5 = ZipObserver(lock: _lock, parent: self, index: 4, setNextValue: { self._values5.enqueue($0) }, this: subscription5)
subscription1.disposable = _parent.source1.subscribe(observer1)
subscription2.disposable = _parent.source2.subscribe(observer2)
subscription3.disposable = _parent.source3.subscribe(observer3)
subscription4.disposable = _parent.source4.subscribe(observer4)
subscription5.disposable = _parent.source5.subscribe(observer5)
return CompositeDisposable(disposables: [
subscription1,
subscription2,
subscription3,
subscription4,
subscription5
])
}
override func getResult() throws -> R {
return try _parent._resultSelector(_values1.dequeue()!, _values2.dequeue()!, _values3.dequeue()!, _values4.dequeue()!, _values5.dequeue()!)
}
}
class Zip5<E1, E2, E3, E4, E5, R> : Producer<R> {
typealias ResultSelector = (E1, E2, E3, E4, E5) throws -> R
let source1: Observable<E1>
let source2: Observable<E2>
let source3: Observable<E3>
let source4: Observable<E4>
let source5: Observable<E5>
let _resultSelector: ResultSelector
init(source1: Observable<E1>, source2: Observable<E2>, source3: Observable<E3>, source4: Observable<E4>, source5: Observable<E5>, resultSelector: ResultSelector) {
self.source1 = source1
self.source2 = source2
self.source3 = source3
self.source4 = source4
self.source5 = source5
_resultSelector = resultSelector
}
override func run<O: ObserverType where O.E == R>(observer: O) -> Disposable {
let sink = ZipSink5_(parent: self, observer: observer)
sink.disposable = sink.run()
return sink
}
}
zip5运算
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. 赋值运算符 "=" Swift赋值语句不可作为条件判断语句 2.基础运算符 "+ - * / %" 3.单目...
- 一元运算符 delete delete 运算符删除对以前定义的对象属性或方法的引用。例如: delete运算符不能...
- 关系运算符 关系运算符小于、大于、小于等于和大于等于执行的是两个数的比较运算,比较方式与算术比较运算相同。每个关系...