Skip to content
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

Merged
merged 3 commits into from Dec 11, 2023

Conversation

amy-at-kickstarter
Copy link
Contributor

📲 What

Add two custom operators for API patterns that (I expect) will be fairly common in Combine.

🤔 Why

As part of the SwiftUI migration work, we want to establish some repeatable patterns for the future. When I ported ReportProjectFormViewModel to be completely in Combine, I left two somewhat confusing operations - a way to map fetch results, and a way to handle API failures without cancelling the pipeline. I wanted to make a cleaner interface for these two patterns, since I assume they'll pop up again.

@@ -1,7 +1,6 @@
import Library
import SwiftUI

// TODO(MBL-1039) - Refactor this so that saveTriggered takes a closure, not a binding
Copy link
Contributor Author

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.

/// }
/// }

public func handleFailureAndAllowRetry(_ onFailure: @escaping (Self.Failure) -> Void)
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 flatMap) are likely to be beastly to write.


return ErrorEnvelope.couldNotParseJSON
.mapFetchResults { (data: GraphAPI.FetchUserEmailQuery.Data) -> UserEnvelope<GraphUserEmail>? in
UserEnvelope<GraphUserEmail>.userEnvelope(from: data)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner!

@@ -57,24 +57,16 @@ public final class ReportProjectFormViewModel: ReportProjectFormViewModelType,
self.saveTriggeredSubject
.compactMap { [weak self] _ in
self?.createFlaggingInput()
.handleFailureAndAllowRetry { _ in
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behold, more readable!

-> AnyPublisher<NewOutputType, ErrorEnvelope> {
return self.tryMap { (data: Output) -> NewOutputType in
guard let envelope = convertData(data) else {
throw ErrorEnvelope.couldNotParseJSON
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@amy-at-kickstarter amy-at-kickstarter marked this pull request as ready for review December 5, 2023 16:11
@amy-at-kickstarter amy-at-kickstarter force-pushed the feat/adyer/mbl-1016/graphql-combine-patterns branch from 10fb839 to ec68b06 Compare December 5, 2023 16:11
@amy-at-kickstarter amy-at-kickstarter self-assigned this Dec 5, 2023
Copy link

codecov bot commented Dec 5, 2023

Codecov Report

Attention: 25 lines in your changes are missing coverage. Please review.

Comparison is base (bd02759) 83.72% compared to head (bd23876) 83.72%.

Files Patch % Lines
KsApi/Publisher+Service.swift 0.00% 15 Missing ⚠️
...ibrary/ViewModels/ReportProjectFormViewModel.swift 0.00% 7 Missing and 1 partial ⚠️
KsApi/Service.swift 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1900      +/-   ##
==========================================
- Coverage   83.72%   83.72%   -0.01%     
==========================================
  Files        1226     1227       +1     
  Lines      111788   111790       +2     
  Branches    29723    29727       +4     
==========================================
  Hits        93596    93596              
- Misses      17175    17176       +1     
- Partials     1017     1018       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@ifosli ifosli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

@amy-at-kickstarter amy-at-kickstarter force-pushed the feat/adyer/mbl-1016/graphql-combine-patterns branch from ec68b06 to bd23876 Compare December 11, 2023 14:26
@amy-at-kickstarter amy-at-kickstarter merged commit 91c1afe into main Dec 11, 2023
7 checks passed
@amy-at-kickstarter amy-at-kickstarter deleted the feat/adyer/mbl-1016/graphql-combine-patterns branch December 11, 2023 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants