mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
ValueObservation is Sendable
This commit is contained in:
1 parent
28faa97a37
commit
5f384a4b22
14 files changed
+113
-68
No files matched your search
@@ -750,9 +750,8 @@ extension DatabasePool: DatabaseReader {
|
||||
public func _add<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
if configuration.readonly {
|
||||
// The easy case: the database does not change
|
||||
return _addReadOnly(
|
||||
@@ -781,9 +780,8 @@ extension DatabasePool: DatabaseReader {
|
||||
private func _addConcurrent<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
assert(!configuration.readonly, "Use _addReadOnly(observation:) instead")
|
||||
assert(!observation.requiresWriteAccess, "Use _addWriteOnly(observation:) instead")
|
||||
let observer = ValueConcurrentObserver(
|
||||
|
||||
@@ -321,9 +321,8 @@ extension DatabaseQueue: DatabaseReader {
|
||||
public func _add<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
if configuration.readonly {
|
||||
// The easy case: the database does not change
|
||||
return _addReadOnly(
|
||||
|
||||
@@ -419,8 +419,8 @@ public protocol DatabaseReader: AnyObject, Sendable {
|
||||
func _add<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable
|
||||
}
|
||||
|
||||
extension DatabaseReader {
|
||||
@@ -585,9 +585,8 @@ extension DatabaseReader {
|
||||
func _addReadOnly<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
if scheduler.immediateInitialValue() {
|
||||
do {
|
||||
// Perform a reentrant read, in case the observation would be
|
||||
@@ -699,9 +698,8 @@ extension AnyDatabaseReader: DatabaseReader {
|
||||
public func _add<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
base._add(
|
||||
observation: observation,
|
||||
scheduling: scheduler,
|
||||
|
||||
@@ -195,9 +195,8 @@ extension DatabaseSnapshot: DatabaseSnapshotReader {
|
||||
public func _add<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
_addReadOnly(
|
||||
observation: observation,
|
||||
scheduling: scheduler,
|
||||
|
||||
@@ -378,9 +378,8 @@ extension DatabaseSnapshotPool: DatabaseSnapshotReader {
|
||||
public func _add<Reducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable where Reducer: ValueReducer
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable where Reducer: ValueReducer {
|
||||
_addReadOnly(observation: observation, scheduling: scheduler, onChange: onChange)
|
||||
}
|
||||
|
||||
|
||||
@@ -600,9 +600,8 @@ extension DatabaseWriter {
|
||||
func _addWriteOnly<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
assert(!configuration.readonly, "Use _addReadOnly(observation:) instead")
|
||||
let observer = ValueWriteOnlyObserver(
|
||||
writer: self,
|
||||
@@ -950,9 +949,8 @@ extension AnyDatabaseWriter: DatabaseReader {
|
||||
public func _add<Reducer: ValueReducer>(
|
||||
observation: ValueObservation<Reducer>,
|
||||
scheduling scheduler: some ValueObservationScheduler,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void
|
||||
) -> AnyDatabaseCancellable {
|
||||
base._add(
|
||||
observation: observation,
|
||||
scheduling: scheduler,
|
||||
|
||||
@@ -52,3 +52,32 @@ extension Mutex where T: Numeric {
|
||||
}
|
||||
|
||||
extension Mutex: @unchecked Sendable where T: Sendable { }
|
||||
|
||||
// MARK: - UnsafeSendableMutex
|
||||
|
||||
/// `UnsafeSendableMutex` is a Mutex that is always Sendable. It is unsafe,
|
||||
/// because it does not guarantee that its value won't escape from the
|
||||
/// critical section.
|
||||
///
|
||||
/// We'll replace it with the SE-0433 Mutex when it is available.
|
||||
/// <https://github.com/apple/swift-evolution/blob/main/proposals/0433-mutex.md>
|
||||
///
|
||||
/// For a longer discussion about Sendable and mutexes, see
|
||||
/// <https://github.com/apple/swift-evolution/blob/main/proposals/0433-mutex.md#interactions-with-swift-concurrency>
|
||||
final class UnsafeSendableMutex<T>: @unchecked Sendable {
|
||||
private var _value: T
|
||||
private var lock = NSLock()
|
||||
|
||||
init(_ value: sending T) {
|
||||
_value = value
|
||||
}
|
||||
|
||||
/// Runs the provided closure while holding a lock on the value.
|
||||
///
|
||||
/// - parameter body: A closure that can modify the value.
|
||||
func withLock<U>(_ body: (inout T) throws -> U) rethrows -> U {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return try body(&_value)
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func throwingFirstError<T>(execute: () throws -> T, finally: () throws -> Void)
|
||||
return result!
|
||||
}
|
||||
|
||||
struct PrintOutputStream: TextOutputStream {
|
||||
struct PrintOutputStream: TextOutputStream, Sendable {
|
||||
func write(_ string: String) {
|
||||
Swift.print(string)
|
||||
}
|
||||
@@ -137,7 +137,10 @@ final class StrongReference<Value: AnyObject>: @unchecked Sendable {
|
||||
}
|
||||
|
||||
/// Concatenates two functions
|
||||
func concat(_ rhs: (() -> Void)?, _ lhs: (() -> Void)?) -> (() -> Void)? {
|
||||
func concat(
|
||||
_ rhs: (@Sendable () -> Void)?,
|
||||
_ lhs: (@Sendable () -> Void)?
|
||||
) -> (@Sendable () -> Void)? {
|
||||
switch (rhs, lhs) {
|
||||
case let (rhs, nil):
|
||||
return rhs
|
||||
@@ -152,7 +155,10 @@ func concat(_ rhs: (() -> Void)?, _ lhs: (() -> Void)?) -> (() -> Void)? {
|
||||
}
|
||||
|
||||
/// Concatenates two functions
|
||||
func concat<T>(_ rhs: ((T) -> Void)?, _ lhs: ((T) -> Void)?) -> ((T) -> Void)? {
|
||||
func concat<T>(
|
||||
_ rhs: (@Sendable (T) -> Void)?,
|
||||
_ lhs: (@Sendable (T) -> Void)?
|
||||
) -> (@Sendable (T) -> Void)? {
|
||||
switch (rhs, lhs) {
|
||||
case let (rhs, nil):
|
||||
return rhs
|
||||
|
||||
@@ -161,7 +161,7 @@ final class ValueConcurrentObserver<Reducer: ValueReducer, Scheduler: ValueObser
|
||||
trackingMode: ValueObservationTrackingMode,
|
||||
reducer: Reducer,
|
||||
events: ValueObservationEvents,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void)
|
||||
{
|
||||
// Configuration
|
||||
self.scheduler = scheduler
|
||||
|
||||
@@ -150,7 +150,7 @@ final class ValueWriteOnlyObserver<
|
||||
trackingMode: ValueObservationTrackingMode,
|
||||
reducer: Reducer,
|
||||
events: ValueObservationEvents,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void)
|
||||
{
|
||||
// Configuration
|
||||
self.scheduler = scheduler
|
||||
|
||||
@@ -18,7 +18,7 @@ extension ValueObservation {
|
||||
///
|
||||
/// - parameter transform: A closure that takes one value as its parameter
|
||||
/// and returns a new value.
|
||||
public func map<T>(_ transform: @escaping (Reducer.Value) throws -> T)
|
||||
public func map<T>(_ transform: @escaping @Sendable (Reducer.Value) throws -> T)
|
||||
-> ValueObservation<ValueReducers.Map<Reducer, T>>
|
||||
{
|
||||
mapReducer { ValueReducers.Map($0, transform) }
|
||||
@@ -34,7 +34,7 @@ extension ValueReducers {
|
||||
private var base: Base
|
||||
private let transform: (Base.Value) throws -> Value
|
||||
|
||||
init(_ base: Base, _ transform: @escaping (Base.Value) throws -> Value) {
|
||||
init(_ base: Base, _ transform: @escaping @Sendable (Base.Value) throws -> Value) {
|
||||
self.base = base
|
||||
self.transform = transform
|
||||
}
|
||||
|
||||
@@ -5,10 +5,22 @@ extension ValueObservation {
|
||||
/// - parameter predicate: A closure to evaluate whether two values are
|
||||
/// equivalent, for purposes of filtering. Return true from this closure
|
||||
/// to indicate that the second element is a duplicate of the first.
|
||||
public func removeDuplicates(by predicate: @escaping (Reducer.Value, Reducer.Value) -> Bool)
|
||||
-> ValueObservation<ValueReducers.RemoveDuplicates<Reducer>>
|
||||
{
|
||||
mapReducer { ValueReducers.RemoveDuplicates($0, predicate: predicate) }
|
||||
public func removeDuplicates(
|
||||
by predicate: sending @escaping (Reducer.Value, Reducer.Value) -> Bool
|
||||
) -> ValueObservation<ValueReducers.RemoveDuplicates<Reducer>> {
|
||||
// The predicate is marked `sending`, which allows us to statically
|
||||
// determine that it will have no other uses after this call.
|
||||
// (according to <https://github.com/swiftlang/swift-evolution/blob/main/proposals/0433-mutex.md#interactions-with-swift-concurrency>)
|
||||
//
|
||||
// And because `predicate` will only be used serially, in the
|
||||
// reducer queue of `ValueObservation` observers, we can say that
|
||||
// this is safe.
|
||||
//
|
||||
// Anyway if we would not accept non-sendable closures, we could
|
||||
// not deal with `Equatable.==`...
|
||||
nonisolated(unsafe) let predicate = predicate
|
||||
|
||||
return mapReducer { ValueReducers.RemoveDuplicates($0, predicate: predicate) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -180,10 +180,13 @@ public final class SharedValueObservation<Element> {
|
||||
private var lastResult: Result<Element, Error>?
|
||||
|
||||
private final class Client {
|
||||
var onError: (Error) -> Void
|
||||
var onChange: (Element) -> Void
|
||||
var onError: @Sendable (Error) -> Void
|
||||
var onChange: @Sendable (Element) -> Void
|
||||
|
||||
init(onError: @escaping (Error) -> Void, onChange: @escaping (Element) -> Void) {
|
||||
init(
|
||||
onError: @escaping @Sendable (Error) -> Void,
|
||||
onChange: @escaping @Sendable (Element) -> Void
|
||||
) {
|
||||
self.onError = onError
|
||||
self.onChange = onChange
|
||||
}
|
||||
@@ -224,8 +227,8 @@ public final class SharedValueObservation<Element> {
|
||||
/// fresh value.
|
||||
/// - returns: A DatabaseCancellable that can stop the observation.
|
||||
public func start(
|
||||
onError: @escaping (Error) -> Void,
|
||||
onChange: @escaping (Element) -> Void)
|
||||
onError: @escaping @Sendable (Error) -> Void,
|
||||
onChange: @escaping @Sendable (Element) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
synchronized {
|
||||
|
||||
@@ -4,7 +4,7 @@ import Combine
|
||||
import Dispatch
|
||||
import Foundation
|
||||
|
||||
public struct ValueObservation<Reducer: ValueReducer> {
|
||||
public struct ValueObservation<Reducer: ValueReducer>: Sendable {
|
||||
var events = ValueObservationEvents()
|
||||
|
||||
/// A boolean value indicating whether the observation requires write access
|
||||
@@ -30,10 +30,10 @@ public struct ValueObservation<Reducer: ValueReducer> {
|
||||
|
||||
/// The reducer is created when observation starts, and is triggered upon
|
||||
/// each database change.
|
||||
var makeReducer: () -> Reducer
|
||||
var makeReducer: @Sendable () -> Reducer
|
||||
|
||||
/// Returns a ValueObservation with a transformed reducer.
|
||||
func mapReducer<R>(_ transform: @escaping (Reducer) -> R) -> ValueObservation<R> {
|
||||
func mapReducer<R>(_ transform: @escaping @Sendable (Reducer) -> R) -> ValueObservation<R> {
|
||||
let makeReducer = self.makeReducer
|
||||
return ValueObservation<R>(
|
||||
events: events,
|
||||
@@ -74,16 +74,16 @@ enum ValueObservationTrackingMode {
|
||||
}
|
||||
|
||||
struct ValueObservationEvents: Refinable {
|
||||
var willStart: (() -> Void)?
|
||||
var willTrackRegion: ((DatabaseRegion) -> Void)?
|
||||
var databaseDidChange: (() -> Void)?
|
||||
var didFail: ((Error) -> Void)?
|
||||
var didCancel: (() -> Void)?
|
||||
var willStart: (@Sendable () -> Void)?
|
||||
var willTrackRegion: (@Sendable (DatabaseRegion) -> Void)?
|
||||
var databaseDidChange: (@Sendable () -> Void)?
|
||||
var didFail: (@Sendable (Error) -> Void)?
|
||||
var didCancel: (@Sendable () -> Void)?
|
||||
}
|
||||
|
||||
typealias ValueObservationStart<T> = (
|
||||
_ onError: @escaping (Error) -> Void,
|
||||
_ onChange: @escaping (T) -> Void)
|
||||
typealias ValueObservationStart<T> = @Sendable (
|
||||
_ onError: @escaping @Sendable (Error) -> Void,
|
||||
_ onChange: @escaping @Sendable (T) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
|
||||
extension ValueObservation: Refinable {
|
||||
@@ -138,8 +138,8 @@ extension ValueObservation: Refinable {
|
||||
public func start(
|
||||
in reader: some DatabaseReader,
|
||||
scheduling scheduler: some ValueObservationScheduler = .async(onQueue: .main),
|
||||
onError: @escaping (Error) -> Void,
|
||||
onChange: @escaping (Reducer.Value) -> Void)
|
||||
onError: @escaping @Sendable (Error) -> Void,
|
||||
onChange: @escaping @Sendable (Reducer.Value) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
where Reducer: ValueReducer
|
||||
{
|
||||
@@ -175,13 +175,13 @@ extension ValueObservation: Refinable {
|
||||
/// - returns: A `ValueObservation` that performs the specified closures
|
||||
/// when ValueObservation events occur.
|
||||
public func handleEvents(
|
||||
willStart: (() -> Void)? = nil,
|
||||
willStart: (@Sendable () -> Void)? = nil,
|
||||
willFetch: (@Sendable () -> Void)? = nil,
|
||||
willTrackRegion: ((DatabaseRegion) -> Void)? = nil,
|
||||
databaseDidChange: (() -> Void)? = nil,
|
||||
didReceiveValue: ((Reducer.Value) -> Void)? = nil,
|
||||
didFail: ((Error) -> Void)? = nil,
|
||||
didCancel: (() -> Void)? = nil)
|
||||
willTrackRegion: (@Sendable (DatabaseRegion) -> Void)? = nil,
|
||||
databaseDidChange: (@Sendable () -> Void)? = nil,
|
||||
didReceiveValue: (@Sendable (Reducer.Value) -> Void)? = nil,
|
||||
didFail: (@Sendable (Error) -> Void)? = nil,
|
||||
didCancel: (@Sendable () -> Void)? = nil)
|
||||
-> ValueObservation<ValueReducers.Trace<Reducer>>
|
||||
{
|
||||
self
|
||||
@@ -231,10 +231,10 @@ extension ValueObservation: Refinable {
|
||||
/// used to log messages to other destinations.
|
||||
public func print(
|
||||
_ prefix: String = "",
|
||||
to stream: TextOutputStream? = nil)
|
||||
to stream: sending TextOutputStream? = nil)
|
||||
-> ValueObservation<ValueReducers.Trace<Reducer>>
|
||||
{
|
||||
let streamMutex = Mutex(stream ?? PrintOutputStream())
|
||||
let streamMutex = UnsafeSendableMutex(stream ?? PrintOutputStream())
|
||||
let prefix = prefix.isEmpty ? "" : "\(prefix): "
|
||||
return handleEvents(
|
||||
willStart: {
|
||||
@@ -476,9 +476,13 @@ extension DatabasePublishers {
|
||||
}
|
||||
}
|
||||
|
||||
private class ValueSubscription<Downstream: Subscriber>: Subscription
|
||||
where Downstream.Failure == Error
|
||||
private class ValueSubscription<Downstream>:
|
||||
Subscription, @unchecked Sendable
|
||||
where Downstream: Subscriber,
|
||||
Downstream.Failure == Error
|
||||
{
|
||||
// @unchecked Sendable because `cancellable` and `state` are
|
||||
// protected by `lock`.
|
||||
private struct WaitingForDemand {
|
||||
let downstream: Downstream
|
||||
let start: ValueObservationStart<Downstream.Input>
|
||||
|
||||
Reference in new issue
Block a user