|
14 | 14 | /// we can pass in a different `Query` with the same input |
15 | 15 | /// and output as a mock. |
16 | 16 | public protocol Query<Input, Output>: Sendable { |
| 17 | + /// The type the query takes as an input |
17 | 18 | associatedtype Input: Sendable |
| 19 | + /// The type the query returns as an output |
18 | 20 | associatedtype Output: Sendable |
19 | 21 |
|
| 22 | + /// Executes the query |
| 23 | + /// |
| 24 | + /// - Parameter input: The query's input |
| 25 | + /// - Returns: The query's output |
20 | 26 | func execute(with input: Input) async throws -> Output |
21 | 27 |
|
| 28 | + |
| 29 | + /// Observes the query's value over time. When the database |
| 30 | + /// changes new values will automatically be refreshed. |
| 31 | + /// |
| 32 | + /// The `QueryObservation` is an `AsyncSequence` and can |
| 33 | + /// be observed with a for loop. |
| 34 | + /// |
| 35 | + /// ```swift |
| 36 | + /// for try await value in query.observe() { |
| 37 | + /// print(value) |
| 38 | + /// } |
| 39 | + /// ``` |
| 40 | + /// |
| 41 | + /// - Parameter input: The query's input |
| 42 | + /// - Returns: The observation. |
22 | 43 | func observe(with input: Input) -> any QueryObservation<Output> |
23 | 44 | } |
24 | 45 |
|
25 | 46 | public extension Query where Input == () { |
| 47 | + /// Executes the query |
26 | 48 | func execute() async throws -> Output { |
27 | 49 | return try await execute(with: ()) |
28 | 50 | } |
29 | 51 |
|
| 52 | + /// Observes the query's value over time. When the database |
| 53 | + /// changes new values will automatically be refreshed. |
| 54 | + /// |
| 55 | + /// The `QueryObservation` is an `AsyncSequence` and can |
| 56 | + /// be observed with a for loop. |
| 57 | + /// |
| 58 | + /// ```swift |
| 59 | + /// for try await value in query.observe() { |
| 60 | + /// print(value) |
| 61 | + /// } |
| 62 | + /// ``` |
| 63 | + /// |
| 64 | + /// - Returns: The observation. |
30 | 65 | func observe() -> any QueryObservation<Output> { |
31 | 66 | return observe(with: ()) |
32 | 67 | } |
|
0 commit comments