mirror of
https://github.com/samsonjs/AsyncMonitor.git
synced 2026-03-25 08:25:47 +00:00
31 lines
868 B
Swift
31 lines
868 B
Swift
/// Type-erasing wrapper for ``AsyncCancellable`` that ties its instance lifetime to cancellation. In other words, when you release
|
|
/// an instance of ``AnyAsyncCancellable`` and it's deallocated then it automatically cancels its given ``AsyncCancellable``.
|
|
public class AnyAsyncCancellable: AsyncCancellable {
|
|
lazy var id = ObjectIdentifier(self)
|
|
|
|
let canceller: () -> Void
|
|
|
|
public init<AC: AsyncCancellable>(cancellable: AC) {
|
|
canceller = { cancellable.cancel() }
|
|
}
|
|
|
|
deinit {
|
|
cancel()
|
|
}
|
|
|
|
// MARK: AsyncCancellable conformance
|
|
|
|
public func cancel() {
|
|
canceller()
|
|
}
|
|
|
|
// MARK: Hashable conformance
|
|
|
|
public static func == (lhs: AnyAsyncCancellable, rhs: AnyAsyncCancellable) -> Bool {
|
|
lhs.id == rhs.id
|
|
}
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
hasher.combine(id)
|
|
}
|
|
}
|