map运算

Map运算如下图所示:

map运算_第1张图片
屏幕快照 2016-03-12 下午9.32.39.png

代码如下:

extension ObservableType {
    public func map(selector: E throws->R)->Observable {
        return self.asObservable().composeMap(selector)
    }

    public func mapWithIndex(selector: (E, Int) throws->R)->Observable {
        return MapWithIndex(source: asObservable(), selector:selector)
    }
}

public class Observable: ObservableType {
    public typealias E = Element

    init() {

    }

    public func subscribe(observer: O)->Disposable {
        abstractMethod()
    }

    public func asObservable()->Observable {
        return self
    }

    deinit {

    }

    internal func composeMap(selector: Element throws->R_->Observable {
        return Map(source: self, selector:selector)
    }
}

class Map: Producer {
    typealias Selector = (SourceType) throws->ResultType

    private let _source: Observable
    private let _selector: Selector

    init(source:Observable, selector: Selector) {
        _source = source
        _selector = selector
    }

    override func composeMap(selector: ResultType throws ->R)->Observable {
        let originalSelector = _selector
        return Map(source: _source, selector: { (s: SourceType) throws->R in
          let r: ResultType = try originalSelector(s)
          return try selector(r)
       })
    }

    override func run(observer: O)->Disposable {
        let sink = MapSink(selector: _selector, observer: observer)
        sink.disposable = _source.subscribe(sink)
        return sink
    }

    deinit {

    }
}

class MapSink: Sink, ObserverType {
    typealias Selector = (SourceType) throws -> ResultType

    typealias ResultType = O.E
    typealias Element = SourceType

    private let _selector: Selector

    init(selector: Selector, observer: O) {
        _selector = selector
        super.init(oberver: oberver)
    }

    func on(event: Event) {
        switch event {
        case .Next(let element):
            do {
                let mappedElement = try _selector(element)
                forwardOn(.Next(mappedElement))
            }catch let e {
                forwardOn(.Error(e))
                dispose()
            }
        case .Error(let error):
            forwardOn(.Error(error))
            dispose()
        case .Completed:
            forwardOn(.Completed)
            dispose()
        }
    }
}

class MapWithIndex : Producer {
    typealias Selector = (SourceType, Int) throws->ResultType

    private let _source: Observable

    private let _selector: Selector

    init(source: Observable, selector: Selector) {
        _source = source
        _selector = selector
    }

    override func run(observer: O)->Disposable {
        let sink = MapWithIndexSink(selector: _selector, observer: observer)
        sink.disposable = _source.subscribe(sink)
        return sink
    }
}

class MapWithIndexSink: Sink, ObserverType {
    typealias Selector = (SourceType, Int) throws->ResultType
    typealias ResultType = O.E
    typealias Element = SourceType
    typealias Parent = MapWithIndex

    private let _selector: Selector
    private var _index = 0

    init(selector: Selector, observer: O) {
        _selector = selector
        super.init(observer: observer)
    }

    func on(event: Event) {
        switch event {
        case .Next(let element):
            do {
                let mappedElement = try _selector(element, try incrementChecked(&_index))
                forwardOn(.Next(mappedElement))
            }
            catch let e {
                forwardOn(.Error(e))
                dispose()
            }
        case .Error(let error):
            forwardOn(.Error(error))
            dispose()
        case .Completed:
            forwardOn(.Completed)
            dispose()
        }
    }
}

你可能感兴趣的:(map运算)