Files
GRDB.swift/GRDB/ValueObservation/DatabaseCancellable.swift
Gwendal Roué 381f716e4b AnyDatabaseCancellable is public
This helps GRDBCombine defaulting to a default cancellable when database connection has been closed before the ValueObservation publisher has been subscribed to.
2020-03-29 20:31:02 +02:00

35 lines
974 B
Swift

/// A protocol indicating that an activity or action supports cancellation.
public protocol DatabaseCancellable {
/// Cancel the activity.
func cancel()
}
/// A type-erasing cancellable object that executes a provided closure
/// when canceled.
///
/// An AnyDatabaseCancellable instance automatically calls cancel()
/// when deinitialized.
public class AnyDatabaseCancellable: DatabaseCancellable {
private var _cancel: (() -> Void)?
/// Initializes the cancellable object with the given cancel-time closure.
public init(cancel: @escaping () -> Void) {
_cancel = cancel
}
public convenience init(_ cancellable: DatabaseCancellable) {
self.init(cancel: cancellable.cancel)
}
deinit {
_cancel?()
}
public func cancel() {
// Don't prevent multiple concurrent calls to _cancel, because it is
// pointless. But release memory!
_cancel?()
_cancel = nil
}
}