Documentation & cleanup

This commit is contained in:
Gwendal Roué committed 2022-03-17 18:44:30 +01:00
1 parent 4eefb9afbe
commit 287ceb8b07
6 files changed
+32 -22

No files matched your search

+4
View File
@@ -297,6 +297,8 @@ public struct Configuration {
(self.label ?? defaultLabel) + (purpose.map { "." + $0 } ?? "")
}
/// Creates a DispatchQueue which has the quality of service and target
/// queue of write accesses.
func makeWriterDispatchQueue(label: String) -> DispatchQueue {
if let targetQueue = writeTargetQueue ?? targetQueue {
return DispatchQueue(label: label, target: targetQueue)
@@ -305,6 +307,8 @@ public struct Configuration {
}
}
/// Creates a DispatchQueue which has the quality of service and target
/// queue of read accesses.
func makeReaderDispatchQueue(label: String) -> DispatchQueue {
if let targetQueue = targetQueue {
return DispatchQueue(label: label, target: targetQueue)
+9 -13
View File
@@ -767,21 +767,17 @@ extension DatabasePool: DatabaseReader {
observation: observation,
scheduling: scheduler,
onChange: onChange)
}
if observation.requiresWriteAccess {
let observer = _addWriteOnly(
} else if observation.requiresWriteAccess {
return _addWriteOnly(
observation: observation,
scheduling: scheduler,
onChange: onChange)
} else {
return _addConcurrent(
observation: observation,
scheduling: scheduler,
onChange: onChange)
return AnyDatabaseCancellable(cancel: observer.cancel)
}
let observer = _addConcurrent(
observation: observation,
scheduling: scheduler,
onChange: onChange)
return AnyDatabaseCancellable(cancel: observer.cancel)
}
/// A concurrent observation fetches the initial value without waiting for
@@ -790,7 +786,7 @@ extension DatabasePool: DatabaseReader {
observation: ValueObservation<Reducer>,
scheduling scheduler: ValueObservationScheduler,
onChange: @escaping (Reducer.Value) -> Void)
-> ValueObserver<Reducer> // For testability
-> DatabaseCancellable
{
assert(!configuration.readonly, "Use _addReadOnly(observation:) instead")
assert(!observation.requiresWriteAccess, "Use _addWriteOnly(observation:) instead")
@@ -898,7 +894,7 @@ extension DatabasePool: DatabaseReader {
}
#endif
return observer
return AnyDatabaseCancellable(cancel: observer.cancel)
}
#if SQLITE_ENABLE_SNAPSHOT
+5 -6
View File
@@ -385,12 +385,11 @@ extension DatabaseQueue {
observation: observation,
scheduling: scheduler,
onChange: onChange)
} else {
return _addWriteOnly(
observation: observation,
scheduling: scheduler,
onChange: onChange)
}
let observer = _addWriteOnly(
observation: observation,
scheduling: scheduler,
onChange: onChange)
return AnyDatabaseCancellable(cancel: observer.cancel)
}
}
+3 -1
View File
@@ -566,6 +566,8 @@ extension DatabaseReader {
{
if scheduler.immediateInitialValue() {
do {
// Perform a reentrant read, in case the observation would be
// started from a database access.
let value = try unsafeReentrantRead { db in
try db.isolated(readOnly: true) {
try observation.fetchValue(db)
@@ -575,7 +577,7 @@ extension DatabaseReader {
} catch {
observation.events.didFail?(error)
}
return AnyDatabaseCancellable(cancel: { })
return AnyDatabaseCancellable(cancel: { /* nothing to cancel */ })
} else {
var isCancelled = false
_weakAsyncRead { dbResult in
+4 -2
View File
@@ -371,7 +371,7 @@ extension DatabaseWriter {
observation: ValueObservation<Reducer>,
scheduling scheduler: ValueObservationScheduler,
onChange: @escaping (Reducer.Value) -> Void)
-> ValueObserver<Reducer> // For testability
-> DatabaseCancellable
{
assert(!configuration.readonly, "Use _addReadOnly(observation:) instead")
@@ -387,6 +387,8 @@ extension DatabaseWriter {
if scheduler.immediateInitialValue() {
do {
// Perform a reentrant read from the writer connection, in case
// the observation would be started from a database access.
let initialValue: Reducer.Value = try unsafeReentrantWrite { db in
let initialValue = try observer.fetchInitialValue(db)
db.add(transactionObserver: observer, extent: .observerLifetime)
@@ -411,7 +413,7 @@ extension DatabaseWriter {
}
}
return observer
return AnyDatabaseCancellable(cancel: observer.cancel)
}
}
@@ -14,6 +14,9 @@ public class ValueObservationScheduler {
impl.schedule(action)
}
/// Returns whether the initial value should be immediately notified.
///
/// If true, then this method was called on the main thread.
func immediateInitialValue() -> Bool {
impl.immediateInitialValue()
}
@@ -71,6 +74,10 @@ public class ValueObservationScheduler {
private protocol ValueObservationSchedulerImpl {
func schedule(_ action: @escaping () -> Void)
/// Returns whether the initial value should be immediately notified.
///
/// If true, then this method was called on the main thread.
func immediateInitialValue() -> Bool
}