-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MBL-1016: Add custom operators for common API patterns #1900
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could consider a different generic error here, if we wished, but I think this is fine. |
||
} | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had an "interesting" learning experience writing this operator. I spent about two hours trying to wrangle the types (with generics) for this function, and got completely stuck. I ended up changing things around a bit so that the types were simpler, which worked - but I think it's worth noting that any custom operators that handle complex type transformations (say, with a |
||
-> AnyPublisher<Self.Output, Never> { | ||
return self.catch { e in | ||
onFailure(e) | ||
return Empty<Self.Output, Never>() | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -367,24 +367,9 @@ public struct Service: ServiceType { | |
-> AnyPublisher<UserEnvelope<GraphUserEmail>, ErrorEnvelope> { | ||
GraphQL.shared.client | ||
.fetch(query: GraphAPI.FetchUserEmailQuery()) | ||
// TODO: make this a custom extension, we'll want to reuse this pattern | ||
.tryMap { (data: GraphAPI.FetchUserEmailQuery.Data) -> UserEnvelope<GraphUserEmail> in | ||
guard let envelope = UserEnvelope<GraphUserEmail>.userEnvelope(from: data) else { | ||
throw ErrorEnvelope.couldNotParseJSON | ||
} | ||
|
||
return envelope | ||
} | ||
.mapError { rawError in | ||
|
||
if let error = rawError as? ErrorEnvelope { | ||
return error | ||
} | ||
|
||
return ErrorEnvelope.couldNotParseJSON | ||
.mapFetchResults { (data: GraphAPI.FetchUserEmailQuery.Data) -> UserEnvelope<GraphUserEmail>? in | ||
UserEnvelope<GraphUserEmail>.userEnvelope(from: data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Much cleaner! |
||
} | ||
|
||
.eraseToAnyPublisher() | ||
} | ||
|
||
public func fetchGraphUserSelf() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,24 +57,16 @@ public final class ReportProjectFormViewModel: ReportProjectFormViewModelType, | |
self.saveTriggeredSubject | ||
.compactMap { [weak self] _ in | ||
self?.createFlaggingInput() | ||
.handleFailureAndAllowRetry { _ in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Behold, more readable! |
||
self?.bannerMessage = MessageBannerViewViewModel(( | ||
type: .error, | ||
message: Strings.Something_went_wrong_please_try_again() | ||
)) | ||
self?.saveButtonEnabled = true | ||
self?.saveButtonLoading = false | ||
} | ||
} | ||
.flatMap { [weak self] createFlaggingInput in | ||
createFlaggingInput.catch { _ in | ||
// An API error happens. | ||
// We need to catch this up here in flatMap, instead of in sink, | ||
// because we don't want an API failure to cancel this pipeline. | ||
// If the pipeline gets canceled, you can't re-submit after a failure. | ||
|
||
self?.bannerMessage = MessageBannerViewViewModel(( | ||
type: .error, | ||
message: Strings.Something_went_wrong_please_try_again() | ||
)) | ||
self?.saveButtonEnabled = true | ||
self?.saveButtonLoading = false | ||
|
||
return Empty<EmptyResponseEnvelope, Never>() | ||
} | ||
} | ||
.flatMap { $0 } | ||
.sink(receiveValue: { [weak self] _ in | ||
// Submitted successfully | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did this a while ago but forgot the
//TODO
.