-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAsyncOperation.swift
More file actions
77 lines (66 loc) · 2.09 KB
/
AsyncOperation.swift
File metadata and controls
77 lines (66 loc) · 2.09 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Foundation
extension OperationQueue {
func addAsyncOperation(asyncBlock: @escaping () async -> Void) {
addOperation(AsyncOperation(asyncBlock: asyncBlock))
}
}
public final class AsyncOperation: Operation, @unchecked Sendable {
private let activeTask: Synchronized<Task<Void, Never>?> = .init(initial: nil)
public let asyncBlock: () async -> Void
public init(asyncBlock: @escaping () async -> Void) {
self.asyncBlock = asyncBlock
}
// Only required when you want to manually start an operation
// Ignored when an operation is added to a queue.
override public var isAsynchronous: Bool { true }
// State is accessed and modified in a thread safe and KVO compliant way.
private let _isExecuting: Synchronized<Bool> = .init(initial: false)
override public private(set) var isExecuting: Bool {
get {
_isExecuting.wrappedValue
}
set {
willChangeValue(forKey: "isExecuting")
_isExecuting.wrappedValue = newValue
didChangeValue(forKey: "isExecuting")
}
}
private let _isFinished: Synchronized<Bool> = .init(initial: false)
override public private(set) var isFinished: Bool {
get {
_isFinished.wrappedValue
}
set {
willChangeValue(forKey: "isFinished")
_isFinished.wrappedValue = newValue
didChangeValue(forKey: "isFinished")
}
}
override public func start() {
guard !isCancelled else {
finish()
return
}
isFinished = false
isExecuting = true
main()
}
override public func main() {
activeTask.wrappedValue = Task { [weak self] in
guard let self, !isCancelled else { return }
await asyncBlock()
finish()
}
}
override public func cancel() {
activeTask.mutating { value in
value?.cancel()
value = nil
}
super.cancel()
}
func finish() {
isExecuting = false
isFinished = true
}
}