Skip to content

Commit 86cb836

Browse files
committed
refactor RxMoya syntax
1 parent 65a726a commit 86cb836

File tree

2 files changed

+16
-48
lines changed

2 files changed

+16
-48
lines changed

Sources/RxMoya/Observable+Response.swift

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,39 @@ extension ObservableType where E == Response {
99

1010
/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
1111
public func filter(statusCodes: ClosedRange<Int>) -> Observable<E> {
12-
return flatMap { response -> Observable<E> in
13-
return Observable.just(try response.filter(statusCodes: statusCodes))
14-
}
12+
return flatMap { Observable.just(try $0.filter(statusCodes: statusCodes)) }
1513
}
1614

1715
public func filter(statusCode: Int) -> Observable<E> {
18-
return flatMap { response -> Observable<E> in
19-
return Observable.just(try response.filter(statusCode: statusCode))
20-
}
16+
return flatMap { Observable.just(try $0.filter(statusCode: statusCode)) }
2117
}
2218

2319
public func filterSuccessfulStatusCodes() -> Observable<E> {
24-
return flatMap { response -> Observable<E> in
25-
return Observable.just(try response.filterSuccessfulStatusCodes())
26-
}
20+
return flatMap { Observable.just(try $0.filterSuccessfulStatusCodes()) }
2721
}
2822

2923
public func filterSuccessfulStatusAndRedirectCodes() -> Observable<E> {
30-
return flatMap { response -> Observable<E> in
31-
return Observable.just(try response.filterSuccessfulStatusAndRedirectCodes())
32-
}
24+
return flatMap { Observable.just(try $0.filterSuccessfulStatusAndRedirectCodes()) }
3325
}
3426

3527
/// Maps data received from the signal into an Image. If the conversion fails, the signal errors.
3628
public func mapImage() -> Observable<Image?> {
37-
return flatMap { response -> Observable<Image?> in
38-
return Observable.just(try response.mapImage())
39-
}
29+
return flatMap { Observable.just(try $0.mapImage()) }
4030
}
4131

4232
/// Maps data received from the signal into a JSON object. If the conversion fails, the signal errors.
4333
public func mapJSON(failsOnEmptyData: Bool = true) -> Observable<Any> {
44-
return flatMap { response -> Observable<Any> in
45-
return Observable.just(try response.mapJSON(failsOnEmptyData: failsOnEmptyData))
46-
}
34+
return flatMap { Observable.just(try $0.mapJSON(failsOnEmptyData: failsOnEmptyData)) }
4735
}
4836

4937
/// Maps received data at key path into a String. If the conversion fails, the signal errors.
5038
public func mapString(atKeyPath keyPath: String? = nil) -> Observable<String> {
51-
return flatMap { response -> Observable<String> in
52-
return Observable.just(try response.mapString(atKeyPath: keyPath))
53-
}
39+
return flatMap { Observable.just(try $0.mapString(atKeyPath: keyPath)) }
5440
}
5541

5642
/// Maps received data at key path into a Decodable object. If the conversion fails, the signal errors.
5743
public func map<D: Decodable>(_ type: D.Type, atKeyPath keyPath: String? = nil, using decoder: JSONDecoder = JSONDecoder(), failsOnEmptyData: Bool = true) -> Observable<D> {
58-
return flatMap { response -> Observable<D> in
59-
return Observable.just(try response.map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData))
60-
}
44+
return flatMap { Observable.just(try $0.map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData)) }
6145
}
6246
}
6347

Sources/RxMoya/Single+Response.swift

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,38 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo
99

1010
/// Filters out responses that don't fall within the given range, generating errors when others are encountered.
1111
public func filter(statusCodes: ClosedRange<Int>) -> Single<ElementType> {
12-
return flatMap { response -> Single<ElementType> in
13-
return Single.just(try response.filter(statusCodes: statusCodes))
14-
}
12+
return flatMap { .just(try $0.filter(statusCodes: statusCodes)) }
1513
}
1614

1715
public func filter(statusCode: Int) -> Single<ElementType> {
18-
return flatMap { response -> Single<ElementType> in
19-
return Single.just(try response.filter(statusCode: statusCode))
20-
}
16+
return flatMap { .just(try $0.filter(statusCode: statusCode)) }
2117
}
2218

2319
public func filterSuccessfulStatusCodes() -> Single<ElementType> {
24-
return flatMap { response -> Single<ElementType> in
25-
return Single.just(try response.filterSuccessfulStatusCodes())
26-
}
20+
return flatMap { .just(try $0.filterSuccessfulStatusCodes()) }
2721
}
2822

2923
public func filterSuccessfulStatusAndRedirectCodes() -> Single<ElementType> {
30-
return flatMap { response -> Single<ElementType> in
31-
return Single.just(try response.filterSuccessfulStatusAndRedirectCodes())
32-
}
24+
return flatMap { .just(try $0.filterSuccessfulStatusAndRedirectCodes()) }
3325
}
3426

3527
/// Maps data received from the signal into an Image. If the conversion fails, the signal errors.
3628
public func mapImage() -> Single<Image?> {
37-
return flatMap { response -> Single<Image?> in
38-
return Single.just(try response.mapImage())
39-
}
29+
return flatMap { .just(try $0.mapImage()) }
4030
}
4131

4232
/// Maps data received from the signal into a JSON object. If the conversion fails, the signal errors.
4333
public func mapJSON(failsOnEmptyData: Bool = true) -> Single<Any> {
44-
return flatMap { response -> Single<Any> in
45-
return Single.just(try response.mapJSON(failsOnEmptyData: failsOnEmptyData))
46-
}
34+
return flatMap { .just(try $0.mapJSON(failsOnEmptyData: failsOnEmptyData)) }
4735
}
4836

4937
/// Maps received data at key path into a String. If the conversion fails, the signal errors.
5038
public func mapString(atKeyPath keyPath: String? = nil) -> Single<String> {
51-
return flatMap { response -> Single<String> in
52-
return Single.just(try response.mapString(atKeyPath: keyPath))
53-
}
39+
return flatMap { .just(try $0.mapString(atKeyPath: keyPath)) }
5440
}
5541

5642
/// Maps received data at key path into a Decodable object. If the conversion fails, the signal errors.
5743
public func map<D: Decodable>(_ type: D.Type, atKeyPath keyPath: String? = nil, using decoder: JSONDecoder = JSONDecoder(), failsOnEmptyData: Bool = true) -> Single<D> {
58-
return flatMap { response -> Single<D> in
59-
return Single.just(try response.map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData))
60-
}
44+
return flatMap { .just(try $0.map(type, atKeyPath: keyPath, using: decoder, failsOnEmptyData: failsOnEmptyData)) }
6145
}
6246
}

0 commit comments

Comments
 (0)