-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MBL-1016: Add custom operators for common API patterns (#1900)
* Remove defunct // TODO comment * MBL-1016: Add custom operator for mapping fetch results with Combine * MBL-1016: Add custom operator for handling API failures with Combine
- Loading branch information
1 parent
bd02759
commit 91c1afe
Showing
5 changed files
with
63 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Combine | ||
import Foundation | ||
|
||
extension Publisher { | ||
/// A convenience method for mapping the results of your fetch to another data type. Any unknown errors are returned in the error as `ErrorEnvelope.couldNotParseJSON`. | ||
func mapFetchResults<NewOutputType>(_ convertData: @escaping ((Output) -> NewOutputType?)) | ||
-> AnyPublisher<NewOutputType, ErrorEnvelope> { | ||
return self.tryMap { (data: Output) -> NewOutputType in | ||
guard let envelope = convertData(data) else { | ||
throw ErrorEnvelope.couldNotParseJSON | ||
} | ||
|
||
return envelope | ||
} | ||
.mapError { rawError in | ||
|
||
if let error = rawError as? ErrorEnvelope { | ||
return error | ||
} | ||
|
||
return ErrorEnvelope.couldNotParseJSON | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
/// A convenience method for gracefully catching API failures. | ||
/// If you handle your API failure in receiveCompletion:, that will actually cancel the entire pipeline, which means the failed request can't be retried. | ||
/// This is a wrapper around the .catch operator, which just makes it a bit easier to read. | ||
/// | ||
/// An example: | ||
/// ``` | ||
/// self.somethingHappened | ||
/// .flatMap() { _ in | ||
/// self.doAnAPIRequest | ||
/// .handleFailureAndAllowRetry() { e in | ||
/// showTheError(e) | ||
/// } | ||
/// } | ||
|
||
public func handleFailureAndAllowRetry(_ onFailure: @escaping (Self.Failure) -> Void) | ||
-> AnyPublisher<Self.Output, Never> { | ||
return self.catch { e in | ||
onFailure(e) | ||
return Empty<Self.Output, Never>() | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters