/**
This method creates a new observable instance
with a variable number of elements.
see also:http://reactivex.io/documentation/operators/from.html
- parameter elements: Elements to generate
- parameter scheduler: Scheduler to send elements on.
If 'nil', elements are sent immediately on subscription
- returns: The observable sequence whose elements are pulled from the given arguments.
*/
public static func of(elements: E ..., scheduler: ImmediateSchedulerType? = nil)->Observable<E> {
return Sequence(elements: elements, scheduler: scheduler)
}
class Sequence<E>: Producer<E> {
private let _elements: [E]
private let _scheduler: ImmediateSchedulerType?
init(elements: [E], scheduler: ImmediateSchedulerType?) {
_elements = elements
_scheduler = scheduler
}
override func subscribe<O: ObserverType where O.E == E>(observer: O)->Disposable {
// optimized version without scheduler
guard _scheduler != nil else {
for element in _elements {
observer.on(.Next(element))
}
observer.on(.Completed)
return NoDisposable.instance
}
let sink = SequenceSink(parent: self, observer: observer)
sink.disposable = sink.run()
return sink
}
}
class SequenceSink<O: ObserverType>: Sink<O> {
typealias Parent = Sequence<O.E>
private let _parent: Parent
init(parent: Parent, observer: O) {
_parent = parent
super.init(observer: observer)
}
func run()->Disposable {
return _parent._scheduler!.scheduleRecursive( (0, _parent._elements)) { (state, recurse) in
if state.0 < state.1.count {
self.forwardOn(.Next(state.1[state.0]))
recurse((state.0 + 1, state.1))
}else {
self.forwardOn(.Completed)
}
}
}
}
of运算
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. 赋值运算符 "=" Swift赋值语句不可作为条件判断语句 2.基础运算符 "+ - * / %" 3.单目...
- 一元运算符 delete delete 运算符删除对以前定义的对象属性或方法的引用。例如: delete运算符不能...
- 汇总区: 三组共9人 应交作业人数8人,准时提交人数7人,迟提交1人。 应点评人数8人,准时点评人数7人,迟点评1...