StrictConcurrency: refactor ValueReducer and extract its Sendable fetching facet

This commit is contained in:
Gwendal Roué authored and Gwendal Roué committed 2024-07-27 16:37:33 +02:00
1 parent ff2a75489d
commit 5465d056ab
9 files changed
+100 -46

No files matched your search

@@ -76,17 +76,17 @@ final class ValueConcurrentObserver<Reducer: ValueReducer, Scheduler: ValueObser
/// The observed DatabasePool.
let dbPool: DatabasePool
/// A reducer that fetches database values.
private let reducer: Reducer
/// The fetcher that fetches database values.
private let fetcher: Reducer.Fetcher
init(dbPool: DatabasePool, reducer: Reducer) {
init(dbPool: DatabasePool, fetcher: Reducer.Fetcher) {
self.dbPool = dbPool
self.reducer = reducer
self.fetcher = fetcher
}
func fetch(_ db: Database) throws -> Reducer.Fetched {
try db.isolated(readOnly: true) {
try reducer._fetch(db)
try fetcher.fetch(db)
}
}
@@ -94,7 +94,7 @@ final class ValueConcurrentObserver<Reducer: ValueReducer, Scheduler: ValueObser
var region = DatabaseRegion()
let fetchedValue = try db.isolated(readOnly: true) {
try db.recordingSelection(&region) {
try reducer._fetch(db)
try fetcher.fetch(db)
}
}
return try (fetchedValue, region.observableRegion(db))
@@ -170,9 +170,9 @@ final class ValueConcurrentObserver<Reducer: ValueReducer, Scheduler: ValueObser
// State
self.databaseAccess = DatabaseAccess(
dbPool: dbPool,
// ValueReducer semantics guarantees that reducer._fetch
// ValueReducer semantics guarantees that the fetcher
// is independent from the reducer state
reducer: reducer)
fetcher: reducer._makeFetcher())
self.notificationCallbacks = NotificationCallbacks(events: events, onChange: onChange)
self.reducer = reducer
self.reduceQueue = DispatchQueue(
@@ -85,18 +85,18 @@ final class ValueWriteOnlyObserver<
/// If true, database values are fetched from a read-only access.
private let readOnly: Bool
/// A reducer that fetches database values.
private let reducer: Reducer
/// The fetcher that fetches database values.
private let fetcher: Reducer.Fetcher
init(writer: Writer, readOnly: Bool, reducer: Reducer) {
init(writer: Writer, readOnly: Bool, fetcher: Reducer.Fetcher) {
self.writer = writer
self.readOnly = readOnly
self.reducer = reducer
self.fetcher = fetcher
}
func fetch(_ db: Database) throws -> Reducer.Fetched {
try db.isolated(readOnly: readOnly) {
try reducer._fetch(db)
try fetcher.fetch(db)
}
}
@@ -104,7 +104,7 @@ final class ValueWriteOnlyObserver<
var region = DatabaseRegion()
let fetchedValue = try db.isolated(readOnly: readOnly) {
try db.recordingSelection(&region) {
try reducer._fetch(db)
try fetcher.fetch(db)
}
}
return try (fetchedValue, region.observableRegion(db))
@@ -162,9 +162,9 @@ final class ValueWriteOnlyObserver<
self.databaseAccess = DatabaseAccess(
writer: writer,
readOnly: readOnly,
// ValueReducer semantics guarantees that reducer._fetch
// ValueReducer semantics guarantees that the fetcher
// is independent from the reducer state
reducer: reducer)
fetcher: reducer._makeFetcher())
self.notificationCallbacks = NotificationCallbacks(events: events, onChange: onChange)
self.reducer = reducer
self.reduceQueue = DispatchQueue(
+16 -8
View File
@@ -1,16 +1,24 @@
extension ValueReducers {
/// A `ValueReducer` that perform database fetches.
public struct Fetch<Value>: ValueReducer {
private let __fetch: (Database) throws -> Value
/// Creates a reducer which passes raw fetched values through.
init(fetch: @escaping (Database) throws -> Value) {
self.__fetch = fetch
public struct _Fetcher: _ValueReducerFetcher {
let _fetch: @Sendable (Database) throws -> Value
public func fetch(_ db: Database) throws -> Value {
assert(db.isInsideTransaction, "Fetching in a non-isolated way is illegal")
return try _fetch(db)
}
}
public func _fetch(_ db: Database) throws -> Value {
assert(db.isInsideTransaction, "Fetching in a non-isolated way is illegal")
return try __fetch(db)
private let _fetch: @Sendable (Database) throws -> Value
/// Creates a reducer which passes raw fetched values through.
init(fetch: @escaping @Sendable (Database) throws -> Value) {
self._fetch = fetch
}
public func _makeFetcher() -> _Fetcher {
_Fetcher(_fetch: _fetch)
}
public func _value(_ fetched: Value) -> Value? {
+2 -2
View File
@@ -39,8 +39,8 @@ extension ValueReducers {
self.transform = transform
}
public func _fetch(_ db: Database) throws -> Base.Fetched {
try base._fetch(db)
public func _makeFetcher() -> Base.Fetcher {
base._makeFetcher()
}
public mutating func _value(_ fetched: Base.Fetched) throws -> Value? {
@@ -77,8 +77,8 @@ extension ValueReducers {
self.predicate = predicate
}
public func _fetch(_ db: Database) throws -> Base.Fetched {
try base._fetch(db)
public func _makeFetcher() -> Base.Fetcher {
base._makeFetcher()
}
public mutating func _value(_ fetched: Base.Fetched) throws -> Base.Value? {
+13 -4
View File
@@ -5,13 +5,22 @@ extension ValueReducers {
/// See ``ValueObservation/handleEvents(willStart:willFetch:willTrackRegion:databaseDidChange:didReceiveValue:didFail:didCancel:)``
/// and ``ValueObservation/print(_:to:)``.
public struct Trace<Base: _ValueReducer>: ValueReducer {
public struct _Fetcher: _ValueReducerFetcher {
let base: Base.Fetcher
let willFetch: @Sendable () -> Void
public func fetch(_ db: Database) throws -> Base.Fetcher.Fetched {
willFetch()
return try base.fetch(db)
}
}
var base: Base
let willFetch: () -> Void
let willFetch: @Sendable () -> Void
let didReceiveValue: (Base.Value) -> Void
public func _fetch(_ db: Database) throws -> Base.Fetched {
willFetch()
return try base._fetch(db)
public func _makeFetcher() -> _Fetcher {
_Fetcher(base: base._makeFetcher(), willFetch: willFetch)
}
public mutating func _value(_ fetched: Base.Fetched) throws -> Base.Value? {
@@ -1,15 +1,30 @@
// A `ValueReducer` fetches and transforms the database values
// observed by a ``ValueObservation``.
//
// It is NOT Sendable, because we need `ValueReducers.RemoveDuplicates` to
// be able to call `Equatable.==`, which IS not a Sendable function.
// Thread-safety will be assured by `ValueObservation`, which will make sure
// it does not invoke the reducer concurrently.
//
// However, we need to be able to fetch from any database dispatch queue,
// and maybe concurrently. That's why a `ValueReducer` has a Sendable facet,
// which is its `Fetcher`.
/// Implementation details of `ValueReducer`.
public protocol _ValueReducer {
/// The type of fetched database values
associatedtype Fetched
/// The Sendable type that fetches database values
associatedtype Fetcher: _ValueReducerFetcher
/// The type of observed values
associatedtype Value
/// Fetches database values upon changes in an observed database region.
///
/// This method must does not depend on the state of the reducer.
func _fetch(_ db: Database) throws -> Fetched
/// The type of fetched database values
typealias Fetched = Fetcher.Fetched
/// Returns a value that fetches database values upon changes in an
/// observed database region. This method must not depend on the state
/// of the reducer.
func _makeFetcher() -> Fetcher
/// Transforms a fetched value into an eventual observed value. Returns nil
/// when observer should not be notified.
@@ -26,6 +41,13 @@ public protocol _ValueReducer {
mutating func _value(_ fetched: Fetched) throws -> Value?
}
public protocol _ValueReducerFetcher: Sendable {
/// The type of fetched database values
associatedtype Fetched
func fetch(_ db: Database) throws -> Fetched
}
/// `ValueReducer` supports ``ValueObservation``.
///
/// A `ValueReducer` fetches and transforms the database values
+3 -1
View File
@@ -267,7 +267,9 @@ extension ValueObservation: Refinable {
where Reducer: ValueReducer
{
var reducer = makeReducer()
guard let value = try reducer._value(reducer._fetch(db)) else {
let fetcher = reducer._makeFetcher()
let fetchedValue = try fetcher.fetch(db)
guard let value = try reducer._value(fetchedValue) else {
fatalError("Broken contract: reducer has no initial value")
}
return value
+20 -7
View File
@@ -231,26 +231,39 @@ extension FetchRequest {
}
/// A type-erased ValueReducer.
public struct AnyValueReducer<Fetched, Value>: ValueReducer {
private var __fetch: (Database) throws -> Fetched
struct AnyValueReducer<Fetched, Value>: ValueReducer {
private var __fetch: @Sendable (Database) throws -> Fetched
private var __value: (Fetched) -> Value?
public init(
fetch: @escaping (Database) throws -> Fetched,
init(
fetch: @escaping @Sendable (Database) throws -> Fetched,
value: @escaping (Fetched) -> Value?)
{
self.__fetch = fetch
self.__value = value
}
public func _fetch(_ db: Database) throws -> Fetched {
try __fetch(db)
func _makeFetcher() -> AnyValueReducerFetcher<Fetched> {
AnyValueReducerFetcher(fetch: __fetch)
}
public func _value(_ fetched: Fetched) -> Value? {
func _value(_ fetched: Fetched) -> Value? {
__value(fetched)
}
}
/// A type-erased _ValueReducerFetcher.
struct AnyValueReducerFetcher<Fetched>: _ValueReducerFetcher {
private var _fetch: @Sendable (Database) throws -> Fetched
init(fetch: @escaping @Sendable (Database) throws -> Fetched) {
self._fetch = fetch
}
func fetch(_ db: Database) throws -> Fetched {
try _fetch(db)
}
}
// Assume this is correct :-/
extension XCTestExpectation: @unchecked Sendable { }