Skip to content

Commit 9ff334c

Browse files
committed
wip
1 parent 1141a53 commit 9ff334c

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

Examples/GRDBDemo/GRDB/GRDBQueryKey.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ struct GRDBQueryKey<Value: Sendable>: SharedReaderKey {
101101
try databaseQueue.read(query.fetch)
102102
}
103103

104+
func load(
105+
initialValue: Value?,
106+
didReceive callback: @escaping @Sendable (Result<Value?, any Error>) -> Void
107+
) {
108+
databaseQueue.asyncRead { result in
109+
callback(result.flatMap { db in Result { try query.fetch(db) } })
110+
}
111+
}
112+
104113
func subscribe(
105114
initialValue: Value?,
106115
didReceive callback: @escaping @Sendable (Result<Value?, any Error>) -> Void

Sources/Sharing/SharedReader.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ public struct SharedReader<Value> {
190190
reference.loadError
191191
}
192192

193+
// $players.isLoading { … } else if $players.loadError { … } else { }
194+
// TODO: public var isLoading: Bool
195+
193196
/// Requests an up-to-date value from an external source.
194197
///
195198
/// When a shared reference is powered by a ``SharedReaderKey``, this method will tell it to

Sources/Sharing/SharedReaderKey.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ extension SharedReader {
145145
}
146146

147147
// TODO: Non-async version of 'init(require:)'?
148+
// TODO: Chopping block for 2.0?
148149

149150
/// Creates a shared reference to a read-only value using a shared key.
150151
///
@@ -154,11 +155,7 @@ extension SharedReader {
154155
/// - Parameter key: A shared key associated with the shared reference. It is responsible for
155156
/// loading and saving the shared reference's value from some external source.
156157
public init(require key: some SharedReaderKey<Value>) async throws {
157-
let value = try await withUnsafeThrowingContinuation { continuation in
158-
key.load(initialValue: nil) { result in
159-
continuation.resume(with: result)
160-
}
161-
}
158+
let value = try await key.load(initialValue: nil)
162159
guard let value else { throw LoadError() }
163160
self.init(rethrowing: value, key)
164161
if let loadError { throw loadError }
@@ -167,11 +164,7 @@ extension SharedReader {
167164
@_disfavoredOverload
168165
@_documentation(visibility: private)
169166
public init(require key: some SharedKey<Value>) async throws {
170-
let value = try await withUnsafeThrowingContinuation { continuation in
171-
key.load(initialValue: nil) { result in
172-
continuation.resume(with: result)
173-
}
174-
}
167+
let value = try await key.load(initialValue: nil)
175168
guard let value else { throw LoadError() }
176169
self.init(rethrowing: value, key)
177170
if let loadError { throw loadError }

0 commit comments

Comments
 (0)