func mapDyz() {
let ob = Observable.of(1,2,3,4)
ob.map { (number) -> Int in
return number+2
}.subscribe { (event) in
print("\(event)")
}.disposed(by: disposeBag)
}
next(3)
next(4)
next(5)
next(6)
completed
步骤1:Filter -> map -> Map.swift
1.1 self.asObservable() -> ObservableSequence
1.2 ObservableSequence 继承Producer
1.3 ObservableSequence 调用 composeMap(:)方法,ObservableSequence调用掉 Observable的composeMap(:)方法
1.4 传入闭包transform
public class Observable : ObservableType {
//省略
internal func composeMap(_ transform: @escaping (Element) throws -> Result) -> Observable {
return _map(source: self, transform: transform)
}
}
步骤2:点击 composeMap( )
2.1 调用 _map
2.2 传入self本身Observable -> ObservableSequence
2.3 传入闭包transform
internal func composeMap(_ transform: @escaping (Element) throws -> Result) -> Observable {
return _map(source: self, transform: transform)
}
步骤3:点击 _map
3.1 调用 Map
internal func _map(source: Observable, transform: @escaping (Element) throws -> Result) -> Observable {
return Map(source: source, transform: transform)
}
步骤4 点击Map
4.1 初始化Map,Map继承Producer
4.2 self._source = source 保存传入的Observable -> ObservableSequence
4.3 self._transform = transform 保存传入的闭包
4.4.1 外界.subscribe( on ),即Map.subscribe( on )调用
4.4.2 来到ObservableType的 subscribe( on )方法
4.4.3 self.asObservable().subscribe(observer),self 是 Map,self.asObservable() 还是 Map
extension ObservableType {
public func subscribe(_ on: @escaping (Event) -> Void)
-> Disposable {
let observer = AnonymousObserver { e in
on(e)
}
return self.asObservable().subscribe(observer)
}
}
4.4.4 来到 Producer的subscribe,最后到Map的run方法
4.5 run方法
4.5.1 新序列的observer是AnonymousObserver,传入的就是AnonymousObserver
4.5.2 原序列的observer是MapSink,即.subscribe()传入的是谁 谁就是observer
4.5.3 self._source.subscribe(sink),ObservableSequence 执行subscribe( )方法,ObservableSequence继承Producer,即 Producer 执行 subscribe( )
final private class Map: Producer {
typealias Transform = (SourceType) throws -> ResultType
private let _source: Observable
private let _transform: Transform
init(source: Observable, transform: @escaping Transform) {
self._source = source
self._transform = transform
#if TRACE_RESOURCES
_ = increment(_numberOfMapOperators)
#endif
}
override func run(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where Observer.Element == ResultType {
let sink = MapSink(transform: self._transform, observer: observer, cancel: cancel)
let subscription = self._source.subscribe(sink)
return (sink: sink, subscription: subscription)
}
}
4.5.4 来到 Producer -> subscribe -> !CurrentThreadScheduler.isScheduleRequired -> self.run 此时的 self是ObservableSequence,即执行ObservableSequence的run() -> ObservableSequenceSink -> run() -> 中间一连串复杂过程省略 -> self.forwardOn -> Sink forwardOn -> self._observer.on(event)即MapSink.on
final private class ObservableSequence: Producer {
fileprivate let _elements: Sequence
fileprivate let _scheduler: ImmediateSchedulerType
init(elements: Sequence, scheduler: ImmediateSchedulerType) {
self._elements = elements
self._scheduler = scheduler
}
override func run(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where Observer.Element == Element {
let sink = ObservableSequenceSink(parent: self, observer: observer, cancel: cancel)
let subscription = sink.run()
return (sink: sink, subscription: subscription)
}
}
final private class ObservableSequenceSink: Sink where Sequence.Element == Observer.Element {
typealias Parent = ObservableSequence
private let _parent: Parent
init(parent: Parent, observer: Observer, cancel: Cancelable) {
self._parent = parent
super.init(observer: observer, cancel: cancel)
}
func run() -> Disposable {
return self._parent._scheduler.scheduleRecursive(self._parent._elements.makeIterator()) { iterator, recurse in
var mutableIterator = iterator
if let next = mutableIterator.next() {
self.forwardOn(.next(next))
recurse(mutableIterator)
}
else {
self.forwardOn(.completed)
self.dispose()
}
}
}
}
步骤5 self._source.subscribe(sink) 执行 on方法,由点击MapSink()进来 ???subscribe(sink)跑到什么地方去了??? 答案见4.4.1之后
element 1,2,3,4
self._transform(element) 执行闭包,map后闭包{ (number) -> Int in return number+2 }
mappedElement 3,4,5,6
func on(_ event: Event) {
switch event {
case .next(let element):
do {
let mappedElement = try self._transform(element)
self.forwardOn(.next(mappedElement))
}
catch let e {
self.forwardOn(.error(e))
self.dispose()
}
case .error(let error):
self.forwardOn(.error(error))
self.dispose()
case .completed:
self.forwardOn(.completed)
self.dispose()
}
}
步骤6 self.forwardOn(.next(mappedElement))
观察者是AnonymousObserver,所以 会来到AnonymousObserver的onCore 执行 回调