mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
SharedValueObservation is Sendable
This commit is contained in:
1 parent
5f384a4b22
commit
ec5b7d96e2
1 file changed
+10
-9
@@ -167,7 +167,8 @@ extension ValueObservation {
|
||||
/// let cancellable1 = ValueObservation.tracking { db in ... }.shared(in: dbQueue).start(...)
|
||||
/// let cancellable2 = ValueObservation.tracking { db in ... }.shared(in: dbQueue).start(...)
|
||||
/// ```
|
||||
public final class SharedValueObservation<Element> {
|
||||
public final class SharedValueObservation<Element>: @unchecked Sendable {
|
||||
// @unchecked Sendable because state is protected by `lock`.
|
||||
private let scheduler: any ValueObservationScheduler
|
||||
private let extent: SharedValueObservationExtent
|
||||
private let startObservation: ValueObservationStart<Element>
|
||||
@@ -179,9 +180,9 @@ public final class SharedValueObservation<Element> {
|
||||
private var cancellable: AnyDatabaseCancellable?
|
||||
private var lastResult: Result<Element, Error>?
|
||||
|
||||
private final class Client {
|
||||
var onError: @Sendable (Error) -> Void
|
||||
var onChange: @Sendable (Element) -> Void
|
||||
private final class Client: Sendable {
|
||||
let onError: @Sendable (Error) -> Void
|
||||
let onChange: @Sendable (Element) -> Void
|
||||
|
||||
init(
|
||||
onError: @escaping @Sendable (Error) -> Void,
|
||||
@@ -231,7 +232,7 @@ public final class SharedValueObservation<Element> {
|
||||
onChange: @escaping @Sendable (Element) -> Void)
|
||||
-> AnyDatabaseCancellable
|
||||
{
|
||||
synchronized {
|
||||
withLock {
|
||||
// Support for reentrancy: a shared immediate observation is
|
||||
// started from the first value notification of that same shared
|
||||
// immediate observation. Yeah, users are nasty.
|
||||
@@ -303,7 +304,7 @@ public final class SharedValueObservation<Element> {
|
||||
#endif
|
||||
|
||||
private func handleError(_ error: Error) {
|
||||
synchronized {
|
||||
withLock {
|
||||
let notifiedClients = clients
|
||||
|
||||
// State change
|
||||
@@ -324,7 +325,7 @@ public final class SharedValueObservation<Element> {
|
||||
}
|
||||
|
||||
private func handleChange(_ value: Element) {
|
||||
synchronized {
|
||||
withLock {
|
||||
// State change
|
||||
lastResult = .success(value)
|
||||
|
||||
@@ -336,7 +337,7 @@ public final class SharedValueObservation<Element> {
|
||||
}
|
||||
|
||||
private func handleCancel(_ client: Client) {
|
||||
synchronized {
|
||||
withLock {
|
||||
// State change
|
||||
clients.removeFirst(where: { $0 === client })
|
||||
if clients.isEmpty && extent == .whileObserved {
|
||||
@@ -347,7 +348,7 @@ public final class SharedValueObservation<Element> {
|
||||
}
|
||||
}
|
||||
|
||||
private func synchronized<T>(_ execute: () throws -> T) rethrows -> T {
|
||||
private func withLock<T>(_ execute: () throws -> T) rethrows -> T {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return try execute()
|
||||
|
||||
Reference in new issue
Block a user