Skip to content

Commit b620f10

Browse files
committed
Combine
1 parent a0b7de8 commit b620f10

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

Sources/Feather/Query+Combine.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,21 @@ public struct QueryPublisher<Output>: Publisher {
9898
}
9999

100100
public extension Query {
101-
/// Returns a Combine publisher to observes the query
101+
/// Returns a Combine publisher that will fire once
102+
/// and then observe changes as the database changes.
102103
///
103104
/// - Parameter input: The input for the query
104-
/// - Returns: A p
105+
/// - Returns: A combine publisher
105106
func publisher(with input: Input) -> QueryPublisher<Output> {
106107
QueryPublisher(query: self.with(input: input))
107108
}
108109
}
109110

110111
public extension Query where Input == () {
112+
/// Returns a Combine publisher that will fire once
113+
/// and then observe changes as the database changes.
114+
///
115+
/// - Returns: A combine publisher
111116
func publisher() -> QueryPublisher<Output> {
112117
QueryPublisher(query: self)
113118
}

Sources/Feather/Query.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,54 @@
1414
/// we can pass in a different `Query` with the same input
1515
/// and output as a mock.
1616
public protocol Query<Input, Output>: Sendable {
17+
/// The type the query takes as an input
1718
associatedtype Input: Sendable
19+
/// The type the query returns as an output
1820
associatedtype Output: Sendable
1921

22+
/// Executes the query
23+
///
24+
/// - Parameter input: The query's input
25+
/// - Returns: The query's output
2026
func execute(with input: Input) async throws -> Output
2127

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.
2243
func observe(with input: Input) -> any QueryObservation<Output>
2344
}
2445

2546
public extension Query where Input == () {
47+
/// Executes the query
2648
func execute() async throws -> Output {
2749
return try await execute(with: ())
2850
}
2951

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.
3065
func observe() -> any QueryObservation<Output> {
3166
return observe(with: ())
3267
}

Tests/FeatherTests/MapTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ struct MapTests {
1919
}
2020

2121
@Test func mapTransformsOutput_Observation() async throws {
22-
let query = Queries.Just<(), Int>(100)
22+
let query = Queries.Just<(), Int>(100).map(\.description)
23+
2324
var count = 0
24-
for try await output in query.map(\.description).observe() {
25+
for try await output in query.observe() {
2526
count += 1
2627
#expect(output == "100")
2728
}

0 commit comments

Comments
 (0)