mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
37 lines
1.1 KiB
Swift
37 lines
1.1 KiB
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
|
|
}
|
|
|
|
/// Initializes a cancellable object that forwards cancellation to the
|
|
/// provided cancellable.
|
|
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
|
|
}
|
|
}
|