SharedValueObservation async sequence

This commit is contained in:
Gwendal Roué committed 2021-10-24 17:47:08 +02:00
1 parent 2542ad7de8
commit 450a7c2d71
2 files changed
+40

No files matched your search

@@ -294,3 +294,21 @@ public final class SharedValueObservation<Element> {
return try execute()
}
}
#if swift(>=5.5) && canImport(_Concurrency)
extension SharedValueObservation {
// MARK: - Asynchronous Observation
/// The database observation, as an asynchronous sequence of
/// database changes.
///
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public func values(bufferingPolicy: AsyncValueObservation<Element>.BufferingPolicy = .unbounded)
-> AsyncValueObservation<Element>
{
AsyncValueObservation(bufferingPolicy: bufferingPolicy) { onError, onChange in
self.start(onError: onError, onChange: onChange)
}
}
}
#endif
@@ -601,6 +601,28 @@ class SharedValueObservationTests: GRDBTestCase {
}
}
#endif
#if swift(>=5.5) && canImport(_Concurrency)
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
func testAsyncAwait() async throws {
let dbQueue = try makeDatabaseQueue()
try await dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
let values = ValueObservation
.tracking(Table("player").fetchCount)
.shared(in: dbQueue)
.values()
for try await value in values {
XCTAssertEqual(value, 0)
break
}
}
#endif
}
private class Log: TextOutputStream {