Skip to content

Commit

Permalink
[ApolloPagination] Add completion callbacks to fetch and refetch in G…
Browse files Browse the repository at this point in the history
…raphQLQueryPager for API Consistency (#292)
  • Loading branch information
Iron-Ham authored Mar 25, 2024
1 parent d18c105 commit 7ecdd56
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,27 @@ public class GraphQLQueryPager<Model>: Publisher {
}

/// Discards pagination state and fetches the first page from scratch.
/// - Parameter cachePolicy: The apollo cache policy to trigger the first fetch with. Defaults to `fetchIgnoringCacheData`.
public func refetch(cachePolicy: CachePolicy = .fetchIgnoringCacheData) {
pager.refetch(cachePolicy: cachePolicy)
/// - Parameters:
/// - cachePolicy: The apollo cache policy to trigger the first fetch with. Defaults to `fetchIgnoringCacheData`.
/// - callbackQueue: The `DispatchQueue` that the `completion` fires on. Defaults to `main`.
/// - completion: A completion block that will always trigger after the execution of this operation.
public func refetch(
cachePolicy: CachePolicy = .fetchIgnoringCacheData,
callbackQueue: DispatchQueue = .main,
completion: (() -> Void)? = nil
) {
pager.refetch(cachePolicy: cachePolicy, callbackQueue: callbackQueue, completion: completion)
}

/// Fetches the first page.
public func fetch() {
pager.fetch()
/// - Parameters:
/// - callbackQueue: The `DispatchQueue` that the `completion` fires on. Defaults to `main`.
/// - completion: A completion block that will always trigger after the execution of this operation.
public func fetch(
callbackQueue: DispatchQueue = .main,
completion: (() -> Void)? = nil
) {
pager.fetch(callbackQueue: callbackQueue, completion: completion)
}

/// Resets pagination state and cancels in-flight updates from the pager.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ public protocol PagerType {
callbackQueue: DispatchQueue,
completion: ((PaginationError?) -> Void)?
)
func refetch(cachePolicy: CachePolicy)
func fetch()
func refetch(
cachePolicy: CachePolicy,
callbackQueue: DispatchQueue,
completion: (() -> Void)?
)
func fetch(
callbackQueue: DispatchQueue,
completion: (() -> Void)?
)
}

/// Handles pagination in the queue by managing multiple query watchers.
Expand Down Expand Up @@ -151,9 +158,17 @@ class GraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQuery: G
}

/// Discards pagination state and fetches the first page from scratch.
/// - Parameter cachePolicy: The apollo cache policy to trigger the first fetch with. Defaults to `fetchIgnoringCacheData`.
func refetch(cachePolicy: CachePolicy = .fetchIgnoringCacheData) {
Task {
/// - Parameters:
/// - cachePolicy: The apollo cache policy to trigger the first fetch with. Defaults to `fetchIgnoringCacheData`.
/// - callbackQueue: The `DispatchQueue` that the `completion` fires on. Defaults to `main`.
/// - completion: A completion block that will always trigger after the execution of this operation.
func refetch(
cachePolicy: CachePolicy = .fetchIgnoringCacheData,
callbackQueue: DispatchQueue = .main,
completion: (() -> Void)? = nil
) {
execute(callbackQueue: callbackQueue, completion: { _ in completion?() }) { [weak self] in
guard let self else { return }
for completion in await self.completionManager.completions {
completion.execute(error: PaginationError.cancellation)
}
Expand All @@ -162,9 +177,15 @@ class GraphQLQueryPagerCoordinator<InitialQuery: GraphQLQuery, PaginatedQuery: G
}

/// Fetches the first page.
func fetch() {
Task {
await pager.fetch()
/// - Parameters:
/// - callbackQueue: The `DispatchQueue` that the `completion` fires on. Defaults to `main`.
/// - completion: A completion block that will always trigger after the execution of this operation.
func fetch(
callbackQueue: DispatchQueue = .main,
completion: (() -> Void)? = nil
) {
execute(callbackQueue: callbackQueue, completion: { _ in completion?() }) { [weak self] in
await self?.pager.fetch()
}
}

Expand Down

0 comments on commit 7ecdd56

Please sign in to comment.