mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
SharedValueObservation publisher
This commit is contained in:
1 parent
a3a804fa82
commit
c3618e0cf0
3 files changed
+97
-31
No files matched your search
@@ -19,14 +19,9 @@ extension ValueObservation {
|
||||
}
|
||||
|
||||
public final class SharedValueObservation<Element> {
|
||||
typealias StartFunction = (
|
||||
_ onError: @escaping (Error) -> Void,
|
||||
_ onChange: @escaping (Element) -> Void)
|
||||
-> DatabaseCancellable
|
||||
|
||||
private let scheduler: ValueObservationScheduler
|
||||
private let extent: SharedValueObservationExtent
|
||||
private let startObservation: StartFunction
|
||||
private let startObservation: ValueObservationStart<Element>
|
||||
private let lock = NSRecursiveLock() // support synchronous observation events
|
||||
|
||||
// protected by lock
|
||||
@@ -48,7 +43,7 @@ public final class SharedValueObservation<Element> {
|
||||
fileprivate init(
|
||||
scheduling scheduler: ValueObservationScheduler,
|
||||
extent: SharedValueObservationExtent,
|
||||
startObservation: @escaping StartFunction)
|
||||
startObservation: @escaping ValueObservationStart<Element>)
|
||||
{
|
||||
self.scheduler = scheduler
|
||||
self.extent = extent
|
||||
@@ -106,6 +101,15 @@ public final class SharedValueObservation<Element> {
|
||||
}
|
||||
}
|
||||
|
||||
#if canImport(Combine)
|
||||
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
||||
public func publisher() -> DatabasePublishers.Value<Element> {
|
||||
DatabasePublishers.Value { onError, onChange in
|
||||
self.start(onError: onError, onChange: onChange)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private func handleError(_ error: Error) {
|
||||
synchronized {
|
||||
let notifiedClients = clients
|
||||
|
||||
@@ -52,6 +52,11 @@ struct ValueObservationEvents: Refinable {
|
||||
var didCancel: (() -> Void)?
|
||||
}
|
||||
|
||||
typealias ValueObservationStart<T> = (
|
||||
_ onError: @escaping (Error) -> Void,
|
||||
_ onChange: @escaping (T) -> Void)
|
||||
-> DatabaseCancellable
|
||||
|
||||
extension ValueObservation: Refinable {
|
||||
|
||||
// MARK: - Starting Observation
|
||||
@@ -261,39 +266,30 @@ extension ValueObservation {
|
||||
scheduling scheduler: ValueObservationScheduler = .async(onQueue: .main))
|
||||
-> DatabasePublishers.Value<Reducer.Value>
|
||||
{
|
||||
return DatabasePublishers.Value(self, in: reader, scheduling: scheduler)
|
||||
DatabasePublishers.Value { [weak reader] (onError, onChange) in
|
||||
guard let reader = reader else {
|
||||
return AnyDatabaseCancellable(cancel: { })
|
||||
}
|
||||
return self.start(
|
||||
in: reader,
|
||||
scheduling: scheduler,
|
||||
onError: onError,
|
||||
onChange: onChange)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
||||
extension DatabasePublishers {
|
||||
fileprivate typealias Start<T> = (
|
||||
_ onError: @escaping (Error) -> Void,
|
||||
_ onChange: @escaping (T) -> Void) -> DatabaseCancellable
|
||||
|
||||
/// A publisher that tracks changes in the database.
|
||||
///
|
||||
/// See `ValueObservation.publisher(in:scheduling:)`.
|
||||
public struct Value<Output>: Publisher {
|
||||
public typealias Failure = Error
|
||||
private let start: Start<Output>
|
||||
private let start: ValueObservationStart<Output>
|
||||
|
||||
init<Reducer>(
|
||||
_ observation: ValueObservation<Reducer>,
|
||||
in reader: DatabaseReader,
|
||||
scheduling scheduler: ValueObservationScheduler)
|
||||
where Reducer.Value == Output
|
||||
{
|
||||
start = { [weak reader] (onError, onChange) in
|
||||
guard let reader = reader else {
|
||||
return AnyDatabaseCancellable(cancel: { })
|
||||
}
|
||||
return observation.start(
|
||||
in: reader,
|
||||
scheduling: scheduler,
|
||||
onError: onError,
|
||||
onChange: onChange)
|
||||
}
|
||||
init(start: @escaping ValueObservationStart<Output>) {
|
||||
self.start = start
|
||||
}
|
||||
|
||||
public func receive<S>(subscriber: S) where S: Subscriber, Failure == S.Failure, Output == S.Input {
|
||||
@@ -309,7 +305,7 @@ extension DatabasePublishers {
|
||||
{
|
||||
private struct WaitingForDemand {
|
||||
let downstream: Downstream
|
||||
let start: Start<Downstream.Input>
|
||||
let start: ValueObservationStart<Downstream.Input>
|
||||
}
|
||||
|
||||
private struct Observing {
|
||||
@@ -336,7 +332,7 @@ extension DatabasePublishers {
|
||||
private var lock = NSRecursiveLock() // Allow re-entrancy
|
||||
|
||||
init(
|
||||
start: @escaping Start<Downstream.Input>,
|
||||
start: @escaping ValueObservationStart<Downstream.Input>,
|
||||
downstream: Downstream)
|
||||
{
|
||||
state = .waitingForDemand(WaitingForDemand(
|
||||
|
||||
@@ -94,6 +94,39 @@ class SharedValueObservationTests: GRDBTestCase {
|
||||
XCTAssertEqual(log.flush(), ["cancel"])
|
||||
}
|
||||
|
||||
#if canImport(Combine)
|
||||
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
||||
func test_immediate_publisher() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
}
|
||||
|
||||
let publisher = ValueObservation
|
||||
.tracking(Table("player").fetchCount)
|
||||
.shared(
|
||||
in: dbQueue,
|
||||
scheduling: .immediate)
|
||||
.publisher()
|
||||
|
||||
do {
|
||||
let recorder = publisher.record()
|
||||
try XCTAssertEqual(recorder.next().get(), 0)
|
||||
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")}
|
||||
try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 1)
|
||||
}
|
||||
|
||||
do {
|
||||
let recorder = publisher.record()
|
||||
try XCTAssertEqual(recorder.next().get(), 1)
|
||||
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")}
|
||||
try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 2)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func test_immediate_whileObserved() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
@@ -326,7 +359,40 @@ class SharedValueObservationTests: GRDBTestCase {
|
||||
cancellable.cancel()
|
||||
XCTAssertEqual(log.flush(), ["cancel"])
|
||||
}
|
||||
|
||||
#if canImport(Combine)
|
||||
@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
||||
func test_async_publisher() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "player") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
}
|
||||
|
||||
let publisher = ValueObservation
|
||||
.tracking(Table("player").fetchCount)
|
||||
.shared(in: dbQueue) // default async
|
||||
.publisher()
|
||||
|
||||
do {
|
||||
let recorder = publisher.record()
|
||||
try XCTAssert(recorder.availableElements.get().isEmpty)
|
||||
try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 0)
|
||||
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")}
|
||||
try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 1)
|
||||
}
|
||||
|
||||
do {
|
||||
let recorder = publisher.record()
|
||||
try XCTAssert(recorder.availableElements.get().isEmpty)
|
||||
try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 1)
|
||||
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES")}
|
||||
try XCTAssertEqual(wait(for: recorder.next(), timeout: 1), 2)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func test_async_whileObserved() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
|
||||
Reference in new issue
Block a user