From 32569c167fc5e6078947e99e868967f692b6f489 Mon Sep 17 00:00:00 2001 From: dangthaison91 Date: Wed, 21 Feb 2018 15:34:14 +0700 Subject: [PATCH 1/2] Remove ImmutableMappable code duplication --- .../RxSwift/ObservableType+ObjectMapper.swift | 23 -------------- Source/RxSwift/Single+ObjectMapper.swift | 31 ++----------------- 2 files changed, 2 insertions(+), 52 deletions(-) diff --git a/Source/RxSwift/ObservableType+ObjectMapper.swift b/Source/RxSwift/ObservableType+ObjectMapper.swift index 61a16375..00d8900f 100644 --- a/Source/RxSwift/ObservableType+ObjectMapper.swift +++ b/Source/RxSwift/ObservableType+ObjectMapper.swift @@ -31,26 +31,3 @@ public extension ObservableType where E == Response { } } } - - -// MARK: - ImmutableMappable -public extension ObservableType where E == Response { - - /// Maps data received from the signal into an object - /// which implements the ImmutableMappable protocol and returns the result back - /// If the conversion fails, the signal errors. - public func mapObject(_ type: T.Type, context: MapContext? = nil) -> Observable { - return flatMap { response -> Observable in - return Observable.just(try response.mapObject(T.self, context: context)) - } - } - - /// Maps data received from the signal into an array of objects - /// which implement the ImmutableMappable protocol and returns the result back - /// If the conversion fails, the signal errors. - public func mapArray(_ type: T.Type, context: MapContext? = nil) -> Observable<[T]> { - return flatMap { response -> Observable<[T]> in - return Observable.just(try response.mapArray(T.self, context: context)) - } - } -} diff --git a/Source/RxSwift/Single+ObjectMapper.swift b/Source/RxSwift/Single+ObjectMapper.swift index fae867cd..b2920136 100755 --- a/Source/RxSwift/Single+ObjectMapper.swift +++ b/Source/RxSwift/Single+ObjectMapper.swift @@ -17,40 +17,13 @@ public extension PrimitiveSequence where TraitType == SingleTrait, ElementType = /// which implements the Mappable protocol and returns the result back /// If the conversion fails, the signal errors. public func mapObject(_ type: T.Type, context: MapContext? = nil) -> Single { - return flatMap { response -> Single in - return Single.just(try response.mapObject(type, context: context)) - } + return map { try $0.mapObject(type, context: context) } } /// Maps data received from the signal into an array of objects /// which implement the Mappable protocol and returns the result back /// If the conversion fails, the signal errors. public func mapArray(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> { - return flatMap { response -> Single<[T]> in - return Single.just(try response.mapArray(type, context: context)) - } - } -} - - -// MARK: - ImmutableMappable -public extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response { - - /// Maps data received from the signal into an object - /// which implements the ImmutableMappable protocol and returns the result back - /// If the conversion fails, the signal errors. - public func mapObject(_ type: T.Type, context: MapContext? = nil) -> Single { - return flatMap { response -> Single in - return Single.just(try response.mapObject(type, context: context)) - } - } - - /// Maps data received from the signal into an array of objects - /// which implement the ImmutableMappable protocol and returns the result back - /// If the conversion fails, the signal errors. - public func mapArray(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> { - return flatMap { response -> Single<[T]> in - return Single.just(try response.mapArray(type, context: context)) - } + return map { try $0.mapArray(type, context: context) } } } From 05a5ad6f7586aab0a8dc00c0e86317ce15bb40ab Mon Sep 17 00:00:00 2001 From: dangthaison91 Date: Wed, 21 Feb 2018 15:56:09 +0700 Subject: [PATCH 2/2] Revert previous implementation --- Source/RxSwift/Single+ObjectMapper.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Source/RxSwift/Single+ObjectMapper.swift b/Source/RxSwift/Single+ObjectMapper.swift index b2920136..09a6f4ec 100755 --- a/Source/RxSwift/Single+ObjectMapper.swift +++ b/Source/RxSwift/Single+ObjectMapper.swift @@ -17,13 +17,17 @@ public extension PrimitiveSequence where TraitType == SingleTrait, ElementType = /// which implements the Mappable protocol and returns the result back /// If the conversion fails, the signal errors. public func mapObject(_ type: T.Type, context: MapContext? = nil) -> Single { - return map { try $0.mapObject(type, context: context) } + return flatMap { response -> Single in + return Single.just(try response.mapObject(type, context: context)) + } } /// Maps data received from the signal into an array of objects /// which implement the Mappable protocol and returns the result back /// If the conversion fails, the signal errors. public func mapArray(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> { - return map { try $0.mapArray(type, context: context) } + return flatMap { response -> Single<[T]> in + return Single.just(try response.mapArray(type, context: context)) + } } }