Skip to content

Commit 98627e6

Browse files
gh-action-runnergh-action-runner
authored andcommitted
Squashed 'apollo-ios-pagination/' changes from 6228198e..ec5e1c0b
ec5e1c0b [ApolloPagination] Remove requirement from initializers (#299) git-subtree-dir: apollo-ios-pagination git-subtree-split: ec5e1c0b2a55382024d8621f967e32dd88ab3bc9
1 parent 9f4acd1 commit 98627e6

File tree

2 files changed

+57
-50
lines changed

2 files changed

+57
-50
lines changed

Sources/ApolloPagination/AsyncGraphQLQueryPager.swift

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
99
public typealias Output = Result<(Model, UpdateSource), Error>
1010
let _subject: CurrentValueSubject<Output?, Never> = .init(nil)
1111
var publisher: AnyPublisher<Output, Never> { _subject.compactMap({ $0 }).eraseToAnyPublisher() }
12-
public var cancellables: Set<AnyCancellable> = []
12+
@Atomic public var cancellables: Set<AnyCancellable> = []
1313
public let pager: any AsyncPagerType
1414

1515
public var canLoadNext: Bool { get async { await pager.canLoadNext } }
@@ -18,45 +18,51 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
1818
init<Pager: AsyncGraphQLQueryPagerCoordinator<InitialQuery, PaginatedQuery>, InitialQuery, PaginatedQuery>(
1919
pager: Pager,
2020
transform: @escaping ([PaginatedQuery.Data], InitialQuery.Data, [PaginatedQuery.Data]) throws -> Model
21-
) async {
21+
) {
2222
self.pager = pager
23-
await pager.subscribe { [weak self] result in
24-
guard let self else { return }
25-
let returnValue: Output
23+
Task {
24+
let cancellable = await pager.subscribe { [weak self] result in
25+
guard let self else { return }
26+
let returnValue: Output
2627

27-
switch result {
28-
case let .success((output, source)):
29-
do {
30-
let transformedModels = try transform(output.previousPages, output.initialPage, output.nextPages)
31-
returnValue = .success((transformedModels, source))
32-
} catch {
28+
switch result {
29+
case let .success((output, source)):
30+
do {
31+
let transformedModels = try transform(output.previousPages, output.initialPage, output.nextPages)
32+
returnValue = .success((transformedModels, source))
33+
} catch {
34+
returnValue = .failure(error)
35+
}
36+
case let .failure(error):
3337
returnValue = .failure(error)
3438
}
35-
case let .failure(error):
36-
returnValue = .failure(error)
37-
}
3839

39-
_subject.send(returnValue)
40-
}.store(in: &cancellables)
40+
_subject.send(returnValue)
41+
}
42+
_ = $cancellables.mutate { $0.insert(cancellable) }
43+
}
4144
}
4245

4346
init<Pager: AsyncGraphQLQueryPagerCoordinator<InitialQuery, PaginatedQuery>, InitialQuery, PaginatedQuery>(
4447
pager: Pager
45-
) async where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
48+
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
4649
self.pager = pager
47-
await pager.subscribe { [weak self] result in
48-
guard let self else { return }
49-
let returnValue: Output
50+
Task {
51+
let cancellable = await pager.subscribe { [weak self] result in
52+
guard let self else { return }
53+
let returnValue: Output
5054

51-
switch result {
52-
case let .success((output, source)):
53-
returnValue = .success((output, source))
54-
case let .failure(error):
55-
returnValue = .failure(error)
56-
}
55+
switch result {
56+
case let .success((output, source)):
57+
returnValue = .success((output, source))
58+
case let .failure(error):
59+
returnValue = .failure(error)
60+
}
5761

58-
_subject.send(returnValue)
59-
}.store(in: &cancellables)
62+
_subject.send(returnValue)
63+
}
64+
_ = $cancellables.mutate { $0.insert(cancellable) }
65+
}
6066
}
6167

6268
convenience init<
@@ -68,8 +74,8 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
6874
pager: Pager,
6975
initialTransform: @escaping (InitialQuery.Data) throws -> Model,
7076
pageTransform: @escaping (PaginatedQuery.Data) throws -> Model
71-
) async where Model: RangeReplaceableCollection, Model.Element == Element {
72-
await self.init(
77+
) where Model: RangeReplaceableCollection, Model.Element == Element {
78+
self.init(
7379
pager: pager,
7480
transform: { previousData, initialData, nextData in
7581
let previous = try previousData.flatMap { try pageTransform($0) }
@@ -93,7 +99,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
9399
pageResolver: ((P, PaginationDirection) -> PaginatedQuery?)?,
94100
initialTransform: @escaping (InitialQuery.Data) throws -> Model,
95101
pageTransform: @escaping (PaginatedQuery.Data) throws -> Model
96-
) async where Model: RangeReplaceableCollection, Model.Element == Element {
102+
) where Model: RangeReplaceableCollection, Model.Element == Element {
97103
let pager = AsyncGraphQLQueryPagerCoordinator(
98104
client: client,
99105
initialQuery: initialQuery,
@@ -108,7 +114,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
108114
},
109115
pageResolver: pageResolver
110116
)
111-
await self.init(
117+
self.init(
112118
pager: pager,
113119
initialTransform: initialTransform,
114120
pageTransform: pageTransform
@@ -137,15 +143,15 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
137143
watcherDispatchQueue: DispatchQueue = .main,
138144
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
139145
pageResolver: ((P, PaginationDirection) -> PaginatedQuery?)?
140-
) async where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
146+
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
141147
let pager = AsyncGraphQLQueryPagerCoordinator(
142148
client: client,
143149
initialQuery: initialQuery,
144150
watcherDispatchQueue: watcherDispatchQueue,
145151
extractPageInfo: extractPageInfo,
146152
pageResolver: pageResolver
147153
)
148-
await self.init(
154+
self.init(
149155
pager: pager
150156
)
151157
}
@@ -161,7 +167,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
161167
extractPageInfo: @escaping (PageExtractionData<InitialQuery, PaginatedQuery, Model?>) -> P,
162168
pageResolver: ((P, PaginationDirection) -> PaginatedQuery?)?,
163169
transform: @escaping ([PaginatedQuery.Data], InitialQuery.Data, [PaginatedQuery.Data]) throws -> Model
164-
) async {
170+
) {
165171
let pager = AsyncGraphQLQueryPagerCoordinator(
166172
client: client,
167173
initialQuery: initialQuery,
@@ -176,7 +182,7 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
176182
},
177183
pageResolver: pageResolver
178184
)
179-
await self.init(
185+
self.init(
180186
pager: pager,
181187
transform: transform
182188
)
@@ -191,9 +197,10 @@ public class AsyncGraphQLQueryPager<Model>: Publisher {
191197
/// Subscribe to the results of the pager, with the management of the subscriber being stored internally to the `AnyGraphQLQueryPager`.
192198
/// - Parameter completion: The closure to trigger when new values come in.
193199
public func subscribe(completion: @MainActor @escaping (Output) -> Void) {
194-
publisher.sink { result in
200+
let cancellable = publisher.sink { result in
195201
Task { await completion(result) }
196-
}.store(in: &cancellables)
202+
}
203+
_ = $cancellables.mutate { $0.insert(cancellable) }
197204
}
198205

199206
/// Load the next page, if available.

Sources/ApolloPagination/GraphQLQueryPager+Convenience.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ public extension AsyncGraphQLQueryPager {
159159
initialQuery: InitialQuery,
160160
extractPageInfo: @escaping (InitialQuery.Data) -> P,
161161
pageResolver: @escaping (P, PaginationDirection) -> InitialQuery?
162-
) async where Model == PaginationOutput<InitialQuery, InitialQuery> {
163-
await self.init(
162+
) where Model == PaginationOutput<InitialQuery, InitialQuery> {
163+
self.init(
164164
pager: AsyncGraphQLQueryPagerCoordinator(
165165
client: client,
166166
initialQuery: initialQuery,
@@ -179,8 +179,8 @@ public extension AsyncGraphQLQueryPager {
179179
extractPageInfo: @escaping (InitialQuery.Data) -> P,
180180
pageResolver: @escaping (P, PaginationDirection) -> InitialQuery?,
181181
transform: @escaping ([InitialQuery.Data], InitialQuery.Data, [InitialQuery.Data]) throws -> Model
182-
) async {
183-
await self.init(
182+
) {
183+
self.init(
184184
pager: AsyncGraphQLQueryPagerCoordinator(
185185
client: client,
186186
initialQuery: initialQuery,
@@ -201,8 +201,8 @@ public extension AsyncGraphQLQueryPager {
201201
extractPageInfo: @escaping (InitialQuery.Data) -> P,
202202
pageResolver: @escaping (P, PaginationDirection) -> InitialQuery?,
203203
transform: @escaping (InitialQuery.Data) throws -> Model
204-
) async where Model: RangeReplaceableCollection, T == Model.Element {
205-
await self.init(
204+
) where Model: RangeReplaceableCollection, T == Model.Element {
205+
self.init(
206206
pager: AsyncGraphQLQueryPagerCoordinator(
207207
client: client,
208208
initialQuery: initialQuery,
@@ -224,8 +224,8 @@ public extension AsyncGraphQLQueryPager {
224224
extractInitialPageInfo: @escaping (InitialQuery.Data) -> P,
225225
extractNextPageInfo: @escaping (PaginatedQuery.Data) -> P,
226226
pageResolver: @escaping (P, PaginationDirection) -> PaginatedQuery?
227-
) async where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
228-
await self.init(
227+
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
228+
self.init(
229229
pager: .init(
230230
client: client,
231231
initialQuery: initialQuery,
@@ -249,8 +249,8 @@ public extension AsyncGraphQLQueryPager {
249249
extractNextPageInfo: @escaping (PaginatedQuery.Data) -> P,
250250
pageResolver: @escaping (P, PaginationDirection) -> PaginatedQuery?,
251251
transform: @escaping ([PaginatedQuery.Data], InitialQuery.Data, [PaginatedQuery.Data]) throws -> Model
252-
) async where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
253-
await self.init(
252+
) where Model == PaginationOutput<InitialQuery, PaginatedQuery> {
253+
self.init(
254254
pager: .init(
255255
client: client,
256256
initialQuery: initialQuery,
@@ -276,8 +276,8 @@ public extension AsyncGraphQLQueryPager {
276276
pageResolver: @escaping (P, PaginationDirection) -> PaginatedQuery?,
277277
initialTransform: @escaping (InitialQuery.Data) throws -> Model,
278278
pageTransform: @escaping (PaginatedQuery.Data) throws -> Model
279-
) async where Model: RangeReplaceableCollection, T == Model.Element {
280-
await self.init(
279+
) where Model: RangeReplaceableCollection, T == Model.Element {
280+
self.init(
281281
pager: .init(
282282
client: client,
283283
initialQuery: initialQuery,

0 commit comments

Comments
 (0)