Repository navigation
Expand file tree
/
Copy pathQueryable.swift
More file actions
250 lines (224 loc) · 10.8 KB
/
Copy pathQueryable.swift
File metadata and controls
250 lines (224 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// Copyright © 2023 Dennis Müller and all collaborators
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import Combine
/// A type that can trigger a view presentation from within an `async` function and `await` its completion and potential result value.
///
/// An example use case would be a boolean coming from a confirmation dialog view. First, create a property of the desired data type:
///
/// ```swift
/// @StateObject var deletionConfirmation = Queryable<String, Bool>()
/// ```
///
/// Alternatively, you can put the queryable instance in any class that your view has access to:
///
/// ```swift
/// class SomeObservableObject: ObservableObject {
/// let deletionConfirmation = Queryable<String, Bool>()
/// }
///
/// struct MyView: View {
/// @StateObject private var someObservableObject = SomeObservableObject()
/// }
/// ```
///
/// Then, use one of the `queryable` prefixed presentation modifiers to show the deletion confirmation. For instance, here we use an alert:
///
/// ```swift
/// someView
/// .queryableAlert(
/// controlledBy: deletionConfirmation,
/// title: "Do you want to delete this?") { itemName, query in
/// Button("Cancel", role: .cancel) {
/// query.answer(with: false)
/// }
/// Button("OK") {
/// query.answer(with: true)
/// }
/// } message: { itemName in
/// Text("This cannot be reversed!")
/// }
/// ```
///
/// To actually present the alert and await the boolean result, call ``Queryable/Queryable/query(with:)`` on the ``Queryable/Queryable`` property.
/// This will activate the alert presentation which can then resolve the query in its completion handler.
///
/// ```swift
/// do {
/// let item = // ...
/// let shouldDelete = try await deletionConfirmation.query(with: item.name)
/// } catch {}
/// ```
///
/// When the Task that calls ``Queryable/Queryable/query(with:)`` is cancelled, the suspended query will also cancel and deactivate (i.e. close) the wrapped navigation presentation.
/// In that case, a ``Queryable/QueryCancellationError`` error is thrown.
@MainActor public final class Queryable<Input, Result>: ObservableObject where Input: Sendable, Result: Sendable {
let queryConflictPolicy: QueryConflictPolicy
var storedContinuationState: ContinuationState?
/// Optional item storing the input value for a query and is used to indicate if the query has started, which usually coincides with a presentation being shown.
@Published var itemContainer: ItemContainer?
public init(queryConflictPolicy: QueryConflictPolicy = .cancelNewQuery) {
self.queryConflictPolicy = queryConflictPolicy
}
// MARK: - Public Interface
/// Requests the collection of data by starting a query on the `Result` type, providing an input value.
///
/// This method will suspend for as long as the query is unanswered and not cancelled. When the parent Task is cancelled, this method will immediately cancel the query and throw a ``Queryable/QueryCancellationError`` error.
///
/// Creating multiple queries at the same time will cause a query conflict which is resolved using the ``Queryable/QueryConflictPolicy`` defined in the initializer of ``Queryable/Queryable``. The default policy is ``Queryable/QueryConflictPolicy/cancelPreviousQuery``.
/// - Returns: The result of the query.
public func query(with item: Input) async throws -> Result {
try await query(with: item, id: UUID().uuidString)
}
/// Requests the collection of data by starting a query on the `Result` type, providing an input value.
///
/// This method will suspend for as long as the query is unanswered and not cancelled. When the parent Task is cancelled, this method will immediately cancel the query and throw a ``Queryable/QueryCancellationError`` error.
///
/// Creating multiple queries at the same time will cause a query conflict which is resolved using the ``Queryable/QueryConflictPolicy`` defined in the initializer of ``Queryable/Queryable``. The default policy is ``Queryable/QueryConflictPolicy/cancelPreviousQuery``.
/// - Returns: The result of the query.
public func query() async throws -> Result where Input == Void {
try await query(with: ())
}
/// Cancels any ongoing queries.
public func cancel() {
objectWillChange.send()
itemContainer?.resolver.answer(throwing: QueryCancellationError())
}
/// A flag indicating if a query is active.
public var isQuerying: Bool {
itemContainer != nil
}
/// An `AsyncStream` observing incoming queries and emitting their inputs and resolver to handle manually.
///
/// Only use this, if you need more fine-grained control over the Queryable, i.e. setting view states yourself or adding tests.
/// In most cases, you should prefer to use one of the `.queryable[...]` view modifiers instead.
/// - Warning: With this, there will be no way of knowing when a query has been cancelled external
/// (a sheet that was closed through a gesture, or a system dialog that has overridden any app dialogs)
///
/// - Warning: Do not implement both a manual query observation as well as a `.queryable[...]` view modifier for the same Queryable instance.
/// This will result in unexpected behavior.
var queryObservation: AsyncStream<QueryObservation<Input, Result>> {
AsyncStream(bufferingPolicy: .unbounded) { continuation in
let task = Task {
for await container in $itemContainer.values {
if Task.isCancelled { return }
if let container {
continuation.yield(.init(queryId: container.id, input: container.item, resolver: container.resolver))
}
}
}
continuation.onTermination = { _ in
task.cancel()
}
}
}
// MARK: - Internal Interface
func query(with item: Input, id: String) async throws -> Result {
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
storeContinuation(continuation, withId: id, item: item)
}
} onCancel: {
Task {
await autoCancelContinuation(id: id, reason: .taskCancelled)
}
}
}
func query(id: String) async throws -> Result where Input == Void {
try await query(with: Void(), id: id)
}
func storeContinuation(
_ newContinuation: CheckedContinuation<Result, Swift.Error>,
withId id: String,
item: Input
) {
if let storedContinuationState {
switch queryConflictPolicy {
case .cancelPreviousQuery:
logger.warning("Cancelling previous query of »\(Result.self, privacy: .public)« to allow new query.")
storedContinuationState.continuation.resume(throwing: QueryCancellationError())
self.storedContinuationState = nil
objectWillChange.send()
self.itemContainer = nil
case .cancelNewQuery:
logger.warning("Cancelling new query of »\(Result.self, privacy: .public)« because another query is ongoing.")
newContinuation.resume(throwing: QueryCancellationError())
return
}
}
let resolver = QueryResolver<Result> { result in
self.resumeContinuation(returning: result, queryId: id)
} errorHandler: { error in
self.resumeContinuation(throwing: error, queryId: id)
}
storedContinuationState = .init(queryId: id, continuation: newContinuation)
objectWillChange.send()
itemContainer = .init(queryId: id, item: item, resolver: resolver)
}
func autoCancelContinuation(id: String, reason: AutoCancelReason) {
// If the user cancels a query programmatically and immediately starts the next one, we need to prevent the `QueryInternalError.queryAutoCancel` from the `onDisappear` handler of the canceled query to cancel the new query. That's why the presentations store an id
if storedContinuationState?.queryId == id {
switch reason {
case .presentationEnded:
logger.notice("Cancelling query of »\(Result.self, privacy: .public)« because presentation has terminated.")
case .taskCancelled:
logger.notice("Cancelling query of »\(Result.self, privacy: .public)« because the task was cancelled.")
}
storedContinuationState?.continuation.resume(throwing: QueryCancellationError())
storedContinuationState = nil
objectWillChange.send()
itemContainer = nil
}
}
// MARK: - Private Interface
private func resumeContinuation(returning result: Result, queryId: String) {
guard itemContainer?.id == queryId else { return }
storedContinuationState?.continuation.resume(returning: result)
storedContinuationState = nil
objectWillChange.send()
itemContainer = nil
}
private func resumeContinuation(throwing error: Error, queryId: String) {
guard itemContainer?.id == queryId else { return }
storedContinuationState?.continuation.resume(throwing: error)
storedContinuationState = nil
objectWillChange.send()
itemContainer = nil
}
}
// MARK: - Auxiliary Types
extension Queryable {
struct ItemContainer: Identifiable {
var id: String { queryId }
let queryId: String
var item: Input
var resolver: QueryResolver<Result>
}
struct ContinuationState {
let queryId: String
var continuation: CheckedContinuation<Result, Swift.Error>
}
enum AutoCancelReason {
case presentationEnded
case taskCancelled
}
}