22 lines
760 B
Swift
22 lines
760 B
Swift
/// A handle that can cancel a live subscription.
|
|
///
|
|
/// The subscription lives exactly as long as the token: ``cancel()`` ends it
|
|
/// early, and releasing the last reference to the token ends it too. Store the
|
|
/// token (or hand it to a ``NodeRegistry``) for as long as the callback should
|
|
/// keep firing. ``cancel()`` is idempotent.
|
|
@MainActor public final class SubscriptionToken {
|
|
private var onCancel: (() -> Void)?
|
|
|
|
@_spi(Portico) public init(onCancel: @escaping () -> Void) {
|
|
self.onCancel = onCancel
|
|
}
|
|
|
|
/// Removes the underlying subscription. Idempotent.
|
|
public func cancel() {
|
|
onCancel?()
|
|
onCancel = nil
|
|
}
|
|
|
|
/// Ends the subscription when the token is released.
|
|
isolated deinit { cancel() }
|
|
}
|