Skip to content

Commit

Permalink
Bring the inits
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-Ham committed Jul 24, 2024
1 parent 61992fc commit 4a80d6c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
public var canLoadNext: Bool { get async { await pager.canLoadNext } }
public var canLoadPrevious: Bool { get async { await pager.canLoadPrevious } }

init<Pager: AsyncGraphQLQueryPagerCoordinator<InitialQuery, PaginatedQuery>, InitialQuery, PaginatedQuery>(
pager: Pager,
transform: @escaping (PaginationOutput<InitialQuery, PaginatedQuery>) throws -> Model
) {
self.pager = pager
Task {
let cancellable = await pager.subscribe { [weak self] result in
guard let self else { return }
let returnValue: Output

switch result {
case let .success(output):
do {
let transformedModels = try transform(output)
returnValue = .success(transformedModels)
} catch {
returnValue = .failure(error)
}
case let .failure(error):
returnValue = .failure(error)
}

_subject.send(returnValue)
}
_ = $cancellables.mutate { $0.insert(cancellable) }
}
}

init<Pager: AsyncGraphQLQueryPagerCoordinator<InitialQuery, PaginatedQuery>, InitialQuery, PaginatedQuery>(
pager: Pager
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
Expand Down Expand Up @@ -55,9 +83,29 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
extractPageInfo: extractPageInfo,
pageResolver: pageResolver
)
self.init(
pager: pager
self.init(pager: pager)
}

public convenience init<
P: PaginationInfo,
InitialQuery: GraphQLQuery,
PaginatedQuery: GraphQLQuery
>(
client: any ApolloClientProtocol,
initialQuery: InitialQuery,
watcherDispatchQueue: DispatchQueue = .main,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
pageResolver: ((P, PaginationDirection) -> PaginatedQuery?)?,
transform: @escaping (PaginationOutput<InitialQuery, PaginatedQuery>) throws -> Model
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
let pager = AsyncGraphQLQueryPagerCoordinator(
client: client,
initialQuery: initialQuery,
watcherDispatchQueue: watcherDispatchQueue,
extractPageInfo: extractPageInfo,
pageResolver: pageResolver
)
self.init(pager: pager, transform: transform)
}

/// Subscribe to the results of the pager, with the management of the subscriber being stored internally to the `AnyGraphQLQueryPager`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ public class GraphQLQueryPager<Model>: Publisher {
public var canLoadNext: Bool { pager.canLoadNext }
public var canLoadPrevious: Bool { pager.canLoadPrevious }

init<Pager: GraphQLQueryPagerCoordinator<InitialQuery, PaginatedQuery>, InitialQuery, PaginatedQuery>(
pager: Pager,
transform: @escaping (PaginationOutput<InitialQuery, PaginatedQuery>) throws -> Model
) {
self.pager = pager
pager.subscribe { [weak self] result in
guard let self else { return }
let returnValue: Output

switch result {
case let .success(output):
do {
let transformedModels = try transform(output)
returnValue = .success(transformedModels)
} catch {
returnValue = .failure(error)
}
case let .failure(error):
returnValue = .failure(error)
}

_subject.send(returnValue)
}
}

init<Pager: GraphQLQueryPagerCoordinator<InitialQuery, PaginatedQuery>, InitialQuery, PaginatedQuery>(
pager: Pager
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
Expand Down Expand Up @@ -46,6 +71,28 @@ public class GraphQLQueryPager<Model>: Publisher {
self.init(pager: pager)
}

public convenience init<
P: PaginationInfo,
InitialQuery: GraphQLQuery,
PaginatedQuery: GraphQLQuery
>(
client: any ApolloClientProtocol,
watcherDispatchQueue: DispatchQueue = .main,
initialQuery: InitialQuery,
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
pageResolver: ((P, PaginationDirection) -> PaginatedQuery?)?,
transform: @escaping (PaginationOutput<InitialQuery, PaginatedQuery>) throws -> Model
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
let pager = GraphQLQueryPagerCoordinator(
client: client,
initialQuery: initialQuery,
watcherDispatchQueue: watcherDispatchQueue,
extractPageInfo: extractPageInfo,
pageResolver: pageResolver
)
self.init(pager: pager, transform: transform)
}

deinit {
pager.reset()
}
Expand Down

0 comments on commit 4a80d6c

Please sign in to comment.