summaryrefslogtreecommitdiffhomepage
path: root/ios/Operations/NoFailedDependenciesCondition.swift
blob: e10d09556d622cab39e9a36c23d5e63608c9e647 (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
44
//
//  NoFailedDependenciesCondition.swift
//  Operations
//
//  Created by pronebird on 25/09/2022.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation

public final class NoFailedDependenciesCondition: OperationCondition {
    public var name: String {
        "NoFailedDependenciesCondition"
    }

    public var isMutuallyExclusive: Bool {
        false
    }

    public let ignoreCancellations: Bool
    public init(ignoreCancellations: Bool) {
        self.ignoreCancellations = ignoreCancellations
    }

    public func evaluate(for operation: Operation, completion: @escaping (Bool) -> Void) {
        let satisfy = operation.dependencies.allSatisfy { operation in
            let operationError = (operation as? AsyncOperation)?.error
            let isCancellationError = operationError?.isOperationCancellationError ?? false

            if operationError != nil, !isCancellationError {
                return false
            }

            // Treat OperationError.cancelled and isCancelled equally.
            if operation.isCancelled || isCancellationError, !self.ignoreCancellations {
                return false
            }

            return true
        }

        completion(satisfy)
    }
}