blob: e79e5f25fe5c940de02e170c1068c5f3ad8090e1 (
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
|
//
// Promise+OperationQueue.swift
// Promise+OperationQueue
//
// Created by pronebird on 02/09/2021.
// Copyright © 2021 Mullvad VPN AB. All rights reserved.
//
import Foundation
extension Promise {
/// Returns a promise that adds a mutually exclusive operation that finishes along with the upstream.
func run(on operationQueue: OperationQueue, categories: [String] = []) -> Promise<Value> {
return Promise(parent: self) { resolver in
let operation = AsyncBlockOperation { operation in
let completionQueue = operationQueue.underlyingQueue
if operation.isCancelled {
resolver.resolve(completion: .cancelled, queue: completionQueue)
operation.finish()
} else {
self.observe { completion in
resolver.resolve(completion: completion, queue: completionQueue)
operation.finish()
}
}
}
resolver.setCancelHandler {
operation.cancel()
}
if !categories.isEmpty {
ExclusivityController.shared.addOperation(operation, categories: categories)
}
operationQueue.addOperation(operation)
}
}
}
|