/**
Merges elements from all observable sequences
in the given enumerable sequence into
a single observable sequence.
seealso:http://reactivex.io/documentation/operators/merge.html
- returns: The observable sequence that merges the
elements of the observable sequences.
*/
extension ObservableType where E: ObservableConvertibleType {
public func merge()->Observable<E.E> {
return Merge(source: asObservable())
}
}
final class Merge<S: ObservableConvertibleType>: Producer<S.E> {
private let _source: Observable<S>
init(source: Observable<S>) {
_source = source
}
override func run<O: ObserverType where O.E == S.E>(observer: O)->Disposable {
let sink = MergeBasicSink<S, O>(observer: observer)
sink.disposable = sink.run(_source)
return sink
}
}
final class MergeBasicSink<S: ObservableConvertibleType, O: ObserverType where O.E == S.E>: MergeSink<S, S, O> {
override init(observer: O) {
super.init(observer: observer)
}
override func performMap(elements: S) throws -> S {
return element
}
}
class MergeSink<SourceType, S: ObservableConvertibleType, O: ObserverType where O.E == S.E>: Sink<O>, ObserverType {
typealias ResultType = O.E
typealias Element = SourceType
private let _lock = NSRecursiveLock()
private var subscribeNext: Bool {
return true
}
// state
private let _group = CompositeDisposable()
private let _sourceSubscription = SingleAssignmentDisposable()
private var _stopped = false
override init(observer: O) {
super.init(observer: observer)
}
func performMap(element: SourceType) throws -> S {
abstractMethod()
}
func on(event: Event<SourceType>) {
switch event {
case .Next(let element)
if !subscribeNext {
return
}
do {
let value = try performMap(element)
subscribeInner(value.asObservable())
}catch let e {
forwardOn(.Error(e))
dispose()
}
case .Error(let error):
_lock.lock(); defer { _lock.unlock() }
forwardOn(.Error(error))
dispose()
case .Completed:
_lock.lock(); defer { _lock.unlock() }
_stopped = true
if _group.count == MergeNoIterators {
forwardOn(.Completed)
dispose()
}else {
_sourceSubscription.dispose()
}
}
}
func run(source: observable<SourceType>)->Disposable {
_group.addDisposable(_sourceSubscription)
let subscription = source.subscribe(self)
_sourceSubscription.disposable = subscription
return _group
}
func subscribeInner(source: Observable<O.E>) {
let iterDisposable = SingleAssignmentDisposable()
if let disposableKey = _group.addDisposable(iterDisposable) {
let iter = MergeSinkIter(parent: self, disposeKey: disposeKey)
let subscription = source.subscribe(iter)
iterDisposable.disposable = subscription
}
}
}
class MergeSinkIter<SourceType, S: ObservableConvertibleType, O: ObserverType where O.E == S.E>: ObserverType {
typealias Parent = MergeSink<SourceType, S, O>
typealias DisposeKey = CompositeDisposable.DisposeKey
typealias E = O.E
private let _parent: Parent
private let _disposeKey: DisposeKey
init(parent: Parent, disposeKey: DisposeKey) {
_parent = parent
_disposeKey = disposeKey
}
func on(event: Event<E>) {
switch event {
case .Next(let value):
_parent._lock.lock(); defer { _parent._lock.unlock() }
_parent.forwardOn(.Next(value))
case .Error(let error):
_parent._lock.lock(); defer { _parent._lock.unlock() }
_parent.forwardOn(.Error(error))
_parent.dispose()
case .Completed:
_parent._group.removeDisposable(_disposeKey)
if _parent._stopped && _parent._group.count == MergeNoIterators {
_parent._lock.lock(); defer { _parent._lock.unlock() }
_parent.forwardOn(.Completed)
_parent.dispose()
}
}
}
}
merge 运算符
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. 赋值运算符 "=" Swift赋值语句不可作为条件判断语句 2.基础运算符 "+ - * / %" 3.单目...
- 一、布尔型 数值型,这个类型里面的值是无穷多个,所有数字都是数值型;字符串型,也是无穷个,所有语句都可以是字符串型...
- 一元运算符 delete delete 运算符删除对以前定义的对象属性或方法的引用。例如: delete运算符不能...
- 关系运算符 关系运算符小于、大于、小于等于和大于等于执行的是两个数的比较运算,比较方式与算术比较运算相同。每个关系...