StartWith
Prepends a sequence of values to an observable sequence.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
extension ObservableType {
/**
Prepends a sequence of values to an observable sequence.
- seealso: [startWith operator on reactivex.io](http://reactivex.io/documentation/operators/startwith.html)
- parameter elements: Elements to prepend to the specified sequence.
- returns: The source sequence prepended with the specified values.
*/
public func startWith(_ elements: Element ...)
-> Observable<Element> {
return StartWith(source: self.asObservable(), elements: elements)
}
}
final private class StartWith: Producer
1
2
3
4
5
6
7
override func run<Observer: ObserverType>(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where Observer.Element == Element {
for e in self.elements {
observer.on(.next(e))
}
return (sink: Disposables.create(), subscription: self.source.subscribe(observer))
}
1
2
3
4
5
6
7
8
let disposeBag = DisposeBag()
Observable.of("🐶", "🐱", "🐭", "🐹")
.startWith("1️⃣")
.startWith("2️⃣")
.startWith("3️⃣", "🅰️", "🅱️")
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)