Skip to content
Permalink
Browse files

Merge branch 'feature/map' of https://github.com/MortyMerr/RxSwift in…

…to MortyMerr-feature/map
  • Loading branch information...
kzaher committed Jul 24, 2019
2 parents 6723f55 + de5a6bc commit 74a960fabe7f5e25d8dddc4ff45ff3a2e5b8fdc7
@@ -22,10 +22,6 @@ import RxSwift
return RxSwift.Resources.numberOfSerialDispatchQueueObservables
}

static var numberOfMapOperators: Int32 {
return RxSwift.Resources.numberOfMapOperators
}

static var total: Int32 {
return RxSwift.Resources.total
}
@@ -29,13 +29,5 @@ public class Observable<Element> : ObservableType {
_ = Resources.decrementTotal()
#endif
}

// this is kind of ugly I know :(
// Swift compiler reports "Not supported yet" when trying to override protocol extensions, so ¯\_(ツ)_/¯
/// Optimizations for map operator
internal func composeMap<Result>(_ transform: @escaping (Element) throws -> Result) -> Observable<Result> {
return _map(source: self, transform: transform)
}
}

@@ -19,7 +19,7 @@ extension ObservableType {
*/
public func map<Result>(_ transform: @escaping (Element) throws -> Result)
-> Observable<Result> {
return self.asObservable().composeMap(transform)
return Map(source: self.asObservable(), transform: transform)
}
}

@@ -57,19 +57,6 @@ final private class MapSink<SourceType, Observer: ObserverType>: Sink<Observer>,
}
}

#if TRACE_RESOURCES
private let _numberOfMapOperators = AtomicInt(0)
extension Resources {
public static var numberOfMapOperators: Int32 {
return load(_numberOfMapOperators)
}
}
#endif

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

final private class Map<SourceType, ResultType>: Producer<ResultType> {
typealias Transform = (SourceType) throws -> ResultType

@@ -80,29 +67,11 @@ final private class Map<SourceType, ResultType>: Producer<ResultType> {
init(source: Observable<SourceType>, transform: @escaping Transform) {
self._source = source
self._transform = transform

#if TRACE_RESOURCES
_ = increment(_numberOfMapOperators)
#endif
}

override func composeMap<Result>(_ selector: @escaping (ResultType) throws -> Result) -> Observable<Result> {
let originalSelector = self._transform
return Map<SourceType, Result>(source: self._source, transform: { (s: SourceType) throws -> Result in
let r: ResultType = try originalSelector(s)
return try selector(r)
})
}

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)
}

#if TRACE_RESOURCES
deinit {
_ = decrement(_numberOfMapOperators)
}
#endif
}
@@ -22,10 +22,6 @@ import RxSwift
return RxSwift.Resources.numberOfSerialDispatchQueueObservables
}

static var numberOfMapOperators: Int32 {
return RxSwift.Resources.numberOfMapOperators
}

static var total: Int32 {
return RxSwift.Resources.total
}
@@ -384,71 +384,4 @@ extension ObservableMapTest {
XCTAssertEqual(res.events, correctMessages)
XCTAssertEqual(xs.subscriptions, correctSubscriptions)
}

#if TRACE_RESOURCES
func testMapCompose_OptimizationIsPerformed() {
let scheduler = TestScheduler(initialClock: 0)

var checked = false
let xs = scheduler.createHotObservable([
.next(150, 1),
.next(210, 0),
])

let res = scheduler.start {
xs
.map { $0 * 10 }
.map { x -> Int in
checked = true
XCTAssertTrue(Resources.numberOfMapOperators == 1)
return x + 1
}
}

let correctMessages = [
Recorded.next(210, 0 * 10 + 1),
]

let correctSubscriptions = [
Subscription(200, 1000)
]

XCTAssertTrue(checked)
XCTAssertEqual(res.events, correctMessages)
XCTAssertEqual(xs.subscriptions, correctSubscriptions)
}

func testMapCompose_OptimizationIsNotPerformed() {
let scheduler = TestScheduler(initialClock: 0)

var checked = false
let xs = scheduler.createHotObservable([
.next(150, 1),
.next(210, 0),
])

let res = scheduler.start {
xs
.map { $0 * 10 }
.filter { _ in true }
.map { x -> Int in
checked = true
XCTAssertTrue(Resources.numberOfMapOperators == 2)
return x + 1
}
}

let correctMessages = [
Recorded.next(210, 0 * 10 + 1),
]

let correctSubscriptions = [
Subscription(200, 1000)
]

XCTAssertTrue(checked)
XCTAssertEqual(res.events, correctMessages)
XCTAssertEqual(xs.subscriptions, correctSubscriptions)
}
#endif
}

0 comments on commit 74a960f

Please sign in to comment.
You can’t perform that action at this time.