Skip to content

Commit 60977b8

Browse files
committed
wip
1 parent 5572922 commit 60977b8

4 files changed

Lines changed: 66 additions & 105 deletions

File tree

Sources/Feather/DatabaseQuery.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public struct FetchManyQuery<Input, Output>: Queryable
1515
private let database: any Database
1616
private let statement: @Sendable (Input, borrowing Transaction) throws -> Statement
1717

18-
public typealias DB = any Database
19-
2018
public init(
2119
_ transactionKind: TransactionKind,
2220
database: any Database,

Sources/Feather/Queries.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ public enum Queries {
1515
/// The original query that requires a database
1616
let base: Base
1717
/// The database to execute the query in
18-
let database: Base.DB
18+
let database: any Database
1919

2020
public var transactionKind: TransactionKind {
2121
return base.transactionKind
2222
}
2323

2424
public func execute(
2525
with input: Base.Input,
26-
in _: ()
26+
in _: ErasedDatabase
2727
) async throws -> Base.Output {
2828
return try await base.execute(with: input, in: database)
2929
}
@@ -34,6 +34,15 @@ public enum Queries {
3434
) throws -> Base.Output {
3535
return try base.execute(with: input, tx: tx)
3636
}
37+
38+
public func observe(
39+
with input: Input,
40+
in _: ErasedDatabase,
41+
handle: @Sendable @escaping (Output) -> Void,
42+
cancelled: @Sendable @escaping () -> Void
43+
) -> QueryObservation<Input, Output> {
44+
return base.observe(with: input, in: database, handle: handle, cancelled: cancelled)
45+
}
3746
}
3847

3948
/// Applies a transform to the queries result
@@ -49,7 +58,7 @@ public enum Queries {
4958

5059
public func execute(
5160
with input: Base.Input,
52-
in database: Base.DB
61+
in database: any Database
5362
) async throws -> Output {
5463
return try await transform(base.execute(with: input, in: database))
5564
}
@@ -116,7 +125,7 @@ public enum Queries {
116125
}
117126

118127
public extension Queryable {
119-
func with(database: DB) -> Queries.WithDatabase<Self> {
128+
func with(database: any Database) -> Queries.WithDatabase<Self> {
120129
return Queries.WithDatabase(base: self, database: database)
121130
}
122131

Sources/Feather/QueryObservation.swift

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
// Created by Wes Wickwire on 3/10/25.
66
//
77

8-
public final class QueryObservation<Input, Output, DB>: DatabaseSubscriber, Sendable
9-
where Input: Sendable, Output: Sendable, DB: Sendable
8+
public final class QueryObservation<Input, Output>: DatabaseSubscriber, Sendable
9+
where Input: Sendable, Output: Sendable
1010
{
11-
private let query: any Queryable<Input, Output, DB>
11+
private let query: any Query<Input, Output>
1212
private let input: Input
13-
private let database: DB
13+
private let database: any Database
1414
private let handle: @Sendable (Output) -> Void
1515
private let cancelled: @Sendable () -> Void
1616

1717
init(
18-
query: any Queryable<Input, Output, DB>,
18+
query: any Query<Input, Output>,
1919
input: Input,
20-
database: DB,
20+
database: any Database,
2121
handle: @Sendable @escaping (Output) -> Void,
2222
cancelled: @Sendable @escaping () -> Void
2323
) {
@@ -39,53 +39,16 @@ where Input: Sendable, Output: Sendable, DB: Sendable
3939
}
4040

4141
public func cancel() {
42-
// database.cancel(subscriber: self)
42+
database.cancel(subscriber: self)
4343
}
4444

4545
public func start() async throws {
46-
// try await database.observe(subscriber: self)
46+
try await database.observe(subscriber: self)
4747
try await emitNext()
4848
}
4949

5050
private func emitNext() async throws {
51-
let output = try await query.execute(with: input, in: database)
51+
let output = try await query.execute(with: input, in: (database))
5252
handle(output)
5353
}
5454
}
55-
56-
extension Queryable {
57-
func observe(
58-
with input: Input,
59-
in database: DB,
60-
handle: @Sendable @escaping (Output) -> Void,
61-
cancelled: @Sendable @escaping () -> Void
62-
) -> QueryObservation<Input, Output, DB> {
63-
QueryObservation<Input, Output, DB>(
64-
query: self,
65-
input: input,
66-
database: database,
67-
handle: handle,
68-
cancelled: cancelled
69-
)
70-
}
71-
72-
func stream(
73-
with input: Input,
74-
in database: DB
75-
) -> AsyncThrowingStream<Output, Error> {
76-
return AsyncThrowingStream<Output, Error> { continuation in
77-
let observation = self.observe(
78-
with: input,
79-
in: database
80-
) { output in
81-
continuation.yield(output)
82-
} cancelled: {
83-
// Nothing to do
84-
}
85-
86-
continuation.onTermination = { _ in
87-
observation.cancel()
88-
}
89-
}
90-
}
91-
}

Sources/Feather/Queryable.swift

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
// Created by Wes Wickwire on 11/9/24.
66
//
77

8-
public typealias Query<Input, Output> = Queryable<Input, Output, ()>
8+
public typealias Query<Input, Output> = Queryable<Input, Output>
99

10-
public protocol Queryable<Input, Output, DB>: Sendable {
10+
public protocol Queryable<Input, Output>: Sendable {
1111
associatedtype Input: Sendable
1212
associatedtype Output: Sendable
13-
associatedtype DB: Sendable
1413

1514
/// Whether the query requires a read or write transaction.
1615
var transactionKind: TransactionKind { get }
1716

1817
func execute(
1918
with input: Input,
20-
in database: DB
19+
in database: any Database
2120
) async throws -> Output
2221

2322
func execute(
@@ -26,18 +25,53 @@ public protocol Queryable<Input, Output, DB>: Sendable {
2625
) throws -> Output
2726
}
2827

29-
extension Queryable where DB == any Database {
30-
public func execute(
28+
public extension Queryable {
29+
func execute(
3130
with input: Input,
32-
in database: DB
31+
in database: any Database
3332
) async throws -> Output {
3433
let tx = try await database.begin(transactionKind)
3534
return try execute(with: input, tx: tx)
3635
}
36+
37+
func observe(
38+
with input: Input,
39+
in database: any Database,
40+
handle: @Sendable @escaping (Output) -> Void,
41+
cancelled: @Sendable @escaping () -> Void
42+
) -> QueryObservation<Input, Output> {
43+
return QueryObservation(
44+
query: self.with(database: database),
45+
input: input,
46+
database: database,
47+
handle: handle,
48+
cancelled: cancelled
49+
)
50+
}
51+
52+
func stream(
53+
with input: Input,
54+
in database: any Database
55+
) -> AsyncThrowingStream<Output, Error> {
56+
return AsyncThrowingStream<Output, Error> { continuation in
57+
let observation = self.observe(
58+
with: input,
59+
in: database
60+
) { output in
61+
continuation.yield(output)
62+
} cancelled: {
63+
// Nothing to do
64+
}
65+
66+
continuation.onTermination = { _ in
67+
observation.cancel()
68+
}
69+
}
70+
}
3771
}
3872

3973
extension Queryable where Input == () {
40-
func execute(in database: DB) async throws -> Output {
74+
func execute(in database: any Database) async throws -> Output {
4175
return try await execute(with: (), in: database)
4276
}
4377

@@ -46,53 +80,10 @@ extension Queryable where Input == () {
4680
}
4781

4882
func observe(
49-
in database: DB,
83+
in database: any Database,
5084
handle: @Sendable @escaping (Output) -> Void,
5185
cancelled: @Sendable @escaping () -> Void
52-
) -> QueryObservation<Input, Output, DB> {
86+
) -> QueryObservation<Input, Output> {
5387
return observe(with: (), in: database, handle: handle, cancelled: cancelled)
5488
}
55-
56-
func stream(in database: DB) -> AsyncThrowingStream<Output, Error> {
57-
return stream(with: (), in: database)
58-
}
59-
}
60-
61-
public extension Queryable where Input == (), DB == () {
62-
func execute() async throws -> Output {
63-
return try await execute(with: (), in: ())
64-
}
6589
}
66-
67-
68-
/// An injectable query that can be executed without explicitly
69-
/// sending in the database.
70-
// func execute(
71-
// with input: Input
72-
// ) async throws -> Output
73-
//
74-
// func observe(
75-
// with input: Input,
76-
// handle: @Sendable @escaping (Output) -> Void,
77-
// cancelled: @Sendable @escaping () -> Void
78-
// ) -> QueryObservation<Input, Output>
79-
80-
//
81-
//public extension Query {
82-
// func stream(
83-
// with input: Input
84-
// ) -> AsyncThrowingStream<Output, Error> {
85-
// return AsyncThrowingStream<Output, Error> { continuation in
86-
// let observation = self.observe(with: input) { output in
87-
// continuation.yield(output)
88-
// } cancelled: {
89-
// // Nothing to do
90-
// }
91-
//
92-
// continuation.onTermination = { _ in
93-
// observation.cancel()
94-
// }
95-
// }
96-
// }
97-
//}
98-

0 commit comments

Comments
 (0)