美文网首页
RxSwift-map源码解析

RxSwift-map源码解析

作者: king_jensen | 来源:发表于2019-08-13 10:27 被阅读0次

    map的使用

    转换闭包应用于可观察序列发出的元素,并返回转换后的元素的新可观察序列。

     let ob = Observable.of(1,2,3,4)
            ob.map { (number) -> Int in
                return number+2
                }
                .subscribe{
                    print("\($0)")
                }
                .disposed(by: disposeBag)
    

    map序列创建

    public func map<Result>(_ transform: @escaping (Element) throws -> Result)
            -> Observable<Result> {
            return self.asObservable().composeMap(transform)
        }
    

    map调用composeMap

     internal func composeMap<Result>(_ transform: @escaping (Element) throws -> Result) -> Observable<Result> {
            return _map(source: self, transform: transform)
        }
    

    composeMap初始化_map序列

    internal func _map<Element, Result>(source: Observable<Element>, transform: @escaping (Element) throws -> Result) -> Observable<Result> {
        return Map(source: source, transform: transform)
    }
    

    实际返回Map序列

    final private class Map<SourceType, ResultType>: Producer<ResultType> {
        typealias Transform = (SourceType) throws -> ResultType
    
        private let _source: Observable<SourceType>
    
        private let _transform: Transform
    
        init(source: Observable<SourceType>, transform: @escaping Transform) {
            self._source = source
            self._transform = transform
    
    #if TRACE_RESOURCES
            _ = increment(_numberOfMapOperators)
    #endif
        }
    

    Map序列中保存源序列_source,以及转换闭包_transform

    map序列订阅

    订阅时,会调用Map.run

     override func run<Observer: ObserverType>(_ 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)
        }
    

    1.创建MapSink

    final private class MapSink<SourceType, Observer: ObserverType>: Sink<Observer>, ObserverType {
        typealias Transform = (SourceType) throws -> ResultType
    
        typealias ResultType = Observer.Element 
        typealias Element = SourceType
    
        private let _transform: Transform
    
        init(transform: @escaping Transform, observer: Observer, cancel: Cancelable) {
            self._transform = transform
            super.init(observer: observer, cancel: cancel)
        }
    
        func on(_ event: Event<SourceType>) {
            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()
            }
        }
    }
    

    保存转换闭包transform,观察者observer,销毁者cancel
    2. self._source.subscribe(observer)MapSink作为观察者,订阅源序列

    信号发送响应

    源序列每一次发送响应时,就会调用MapSink.on

    func on(_ event: Event<SourceType>) {
            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()
            }
        }
    

    self._transform(element),MapSink.on调用_transform转换闭包进行转换,转换后调用Sink.forwardOn(observer)

     final func forwardOn(_ event: Event<Observer.Element>) {
            #if DEBUG
                self._synchronizationTracker.register(synchronizationErrorMessage: .default)
                defer { self._synchronizationTracker.unregister() }
            #endif
            if isFlagSet(self._disposed, 1) {
                return
            }
            self._observer.on(event)
        }
    

    self._observer.on(event)将回调到外界的订阅闭包中.

    总结

    map.png

    相关文章

      网友评论

          本文标题:RxSwift-map源码解析

          本文链接:https://www.haomeiwen.com/subject/btomjctx.html