blob: 0b1adaccab6af8d28ec8b6d9ae74dd5a4489c727 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
//
// CancellableChain.swift
// MullvadTransport
//
// Created by pronebird on 23/10/2023.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Foundation
import MullvadTypes
/// Cancellable object that cancels all cancellable objects linked to it.
final class CancellableChain: Cancellable {
private let stateLock = NSLock()
private var isCancelled = false
private var linkedTokens: [Cancellable] = []
init() {}
/// Link cancellation token with some other.
///
/// The token is cancelled immediately, if the chain is already cancelled.
func link(_ token: Cancellable) {
stateLock.withLock {
if isCancelled {
token.cancel()
} else {
linkedTokens.append(token)
}
}
}
/// Request cancellation.
///
/// Cancels and releases any of the connected tokens.
func cancel() {
stateLock.withLock {
isCancelled = true
linkedTokens.forEach { $0.cancel() }
linkedTokens.removeAll()
}
}
}
|