-
Notifications
You must be signed in to change notification settings - Fork 41
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
GraphQLQueryWatcher: publisher
property
#346
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import Combine | ||
import Foundation | ||
#if !COCOAPODS | ||
import ApolloAPI | ||
|
@@ -9,7 +10,7 @@ import ApolloAPI | |
public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, ApolloStoreSubscriber { | ||
weak var client: ApolloClientProtocol? | ||
public let query: Query | ||
let resultHandler: GraphQLResultHandler<Query.Data> | ||
var resultHandler: GraphQLResultHandler<Query.Data>? | ||
|
||
private let callbackQueue: DispatchQueue | ||
|
||
|
@@ -29,26 +30,71 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo | |
@Atomic private var fetching: WeakFetchTaskContainer = .init(nil, nil) | ||
|
||
@Atomic private var dependentKeys: Set<CacheKey>? = nil | ||
private var subscriptions: [WatcherSubscription<AnySubscriber<CachePublisher.Output, Never>>] = [] | ||
|
||
/// Designated initializer | ||
/// Convenience initializer | ||
/// | ||
/// - Parameters: | ||
/// - client: The client protocol to pass in. | ||
/// - query: The query to watch. | ||
/// - context: [optional] A context that is being passed through the request chain. Defaults to `nil`. | ||
/// - callbackQueue: The queue for the result handler. Defaults to the main queue. | ||
/// - resultHandler: The result handler to call with changes. | ||
public init(client: ApolloClientProtocol, | ||
query: Query, | ||
context: RequestContext? = nil, | ||
callbackQueue: DispatchQueue = .main, | ||
resultHandler: @escaping GraphQLResultHandler<Query.Data>) { | ||
public convenience init( | ||
client: ApolloClientProtocol, | ||
query: Query, | ||
context: RequestContext? = nil, | ||
callbackQueue: DispatchQueue = .main, | ||
resultHandler: @escaping GraphQLResultHandler<Query.Data> | ||
) { | ||
self.init( | ||
client: client, | ||
query: query, | ||
context: context, | ||
callbackQueue: callbackQueue, | ||
handler: resultHandler | ||
) | ||
} | ||
|
||
/// Conenience initializer, intended for use with the watcher's `publisher` property. | ||
/// - Parameters: | ||
/// - client: The client protocol to pass in. | ||
/// - query: The query to watch. | ||
/// - context: [optional] A context that is being passed through the request chain. Defaults to `nil`. | ||
/// - callbackQueue: The queue for the result handler. Defaults to the main queue. | ||
public convenience init( | ||
client: ApolloClientProtocol, | ||
query: Query, | ||
context: RequestContext? = nil, | ||
callbackQueue: DispatchQueue = .main | ||
) { | ||
self.init( | ||
client: client, | ||
query: query, | ||
context: context, | ||
callbackQueue: callbackQueue, | ||
handler: nil | ||
) | ||
} | ||
|
||
init( | ||
client: ApolloClientProtocol, | ||
query: Query, | ||
context: RequestContext? = nil, | ||
callbackQueue: DispatchQueue = .main, | ||
handler: GraphQLResultHandler<Query.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. I would think the handler would need to be |
||
) { | ||
self.client = client | ||
self.query = query | ||
self.resultHandler = resultHandler | ||
self.callbackQueue = callbackQueue | ||
self.context = context | ||
|
||
self.resultHandler = { [weak self] result in | ||
guard let self else { return } | ||
for subscription in self.subscriptions { | ||
subscription.trigger(input: result) | ||
} | ||
handler?(result) | ||
} | ||
client.store.subscribe(self) | ||
} | ||
|
||
|
@@ -74,7 +120,7 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo | |
break | ||
} | ||
|
||
self.resultHandler(result) | ||
self.resultHandler?(result) | ||
} | ||
} | ||
} | ||
|
@@ -91,33 +137,33 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo | |
if | ||
let incomingIdentifier = contextIdentifier, | ||
incomingIdentifier == self.contextIdentifier { | ||
// This is from changes to the keys made from the `fetch` method above, | ||
// changes will be returned through that and do not need to be returned | ||
// here as well. | ||
return | ||
// This is from changes to the keys made from the `fetch` method above, | ||
// changes will be returned through that and do not need to be returned | ||
// here as well. | ||
return | ||
} | ||
|
||
guard let dependentKeys = self.dependentKeys else { | ||
// This query has nil dependent keys, so nothing that changed will affect it. | ||
return | ||
} | ||
|
||
if !dependentKeys.isDisjoint(with: changedKeys) { | ||
// First, attempt to reload the query from the cache directly, in order not to interrupt any in-flight server-side fetch. | ||
store.load(self.query) { [weak self] result in | ||
guard let self = self else { return } | ||
|
||
switch result { | ||
case .success(let graphQLResult): | ||
self.callbackQueue.async { [weak self] in | ||
guard let self = self else { | ||
return | ||
} | ||
|
||
self.$dependentKeys.mutate { | ||
$0 = graphQLResult.dependentKeys | ||
} | ||
self.resultHandler(result) | ||
self.resultHandler?(result) | ||
} | ||
case .failure: | ||
if self.fetching.cachePolicy != .returnCacheDataDontFetch { | ||
|
@@ -129,3 +175,44 @@ public final class GraphQLQueryWatcher<Query: GraphQLQuery>: Cancellable, Apollo | |
} | ||
} | ||
} | ||
|
||
extension GraphQLQueryWatcher { | ||
public struct CachePublisher: Publisher { | ||
public typealias Output = Result<GraphQLResult<Query.Data>, Error> | ||
public typealias Failure = Never | ||
|
||
var watcher: GraphQLQueryWatcher | ||
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. Thinking this should be a |
||
|
||
public func receive<S: Subscriber>( | ||
subscriber: S | ||
) where S.Input == Output, S.Failure == Failure { | ||
let subscription = WatcherSubscription<AnySubscriber<Output, Failure>>() | ||
subscription.target = AnySubscriber(subscriber) | ||
subscriber.receive(subscription: subscription) | ||
watcher.subscriptions.append(subscription) | ||
} | ||
} | ||
} | ||
|
||
private extension GraphQLQueryWatcher { | ||
class WatcherSubscription<Target: Subscriber>: Subscription | ||
where Target.Input == CachePublisher.Output { | ||
var target: Target? | ||
// We don't respond to demand, since we emit events according to the underlying cache updates | ||
func request(_ demand: Subscribers.Demand) { } | ||
|
||
func cancel() { | ||
target = nil | ||
} | ||
|
||
func trigger(input: CachePublisher.Output) { | ||
_ = target?.receive(input) | ||
} | ||
} | ||
} | ||
|
||
extension GraphQLQueryWatcher { | ||
public func publisher() -> CachePublisher { | ||
CachePublisher(watcher: self) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Make this
let
please.