-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathPluginImportProgress.swift
More file actions
91 lines (76 loc) · 2.12 KB
/
PluginImportProgress.swift
File metadata and controls
91 lines (76 loc) · 2.12 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// PluginImportProgress.swift
// TableProPluginKit
//
import Foundation
public final class PluginImportProgress: @unchecked Sendable {
private let lock = NSLock()
private var _processedStatements: Int = 0
private var _estimatedTotalStatements: Int = 0
private var _statusMessage: String = ""
private var _isCancelled: Bool = false
private let updateInterval: Int = 500
private var internalCount: Int = 0
public var onUpdate: (@Sendable (Int, Int, String) -> Void)?
public init() {}
public func setEstimatedTotal(_ count: Int) {
lock.lock()
_estimatedTotalStatements = count
lock.unlock()
}
public func incrementStatement() {
lock.lock()
internalCount += 1
_processedStatements = internalCount
let shouldNotify = internalCount % updateInterval == 0
lock.unlock()
if shouldNotify {
notifyUpdate()
}
}
public func setStatus(_ message: String) {
lock.lock()
_statusMessage = message
lock.unlock()
notifyUpdate()
}
public func checkCancellation() throws {
lock.lock()
let cancelled = _isCancelled
lock.unlock()
if cancelled || Task.isCancelled {
throw PluginImportCancellationError()
}
}
public func cancel() {
lock.lock()
_isCancelled = true
lock.unlock()
}
public var isCancelled: Bool {
lock.lock()
defer { lock.unlock() }
return _isCancelled
}
public var processedStatements: Int {
lock.lock()
defer { lock.unlock() }
return _processedStatements
}
public var estimatedTotalStatements: Int {
lock.lock()
defer { lock.unlock() }
return _estimatedTotalStatements
}
public func finalize() {
notifyUpdate()
}
private func notifyUpdate() {
lock.lock()
let processed = _processedStatements
let total = _estimatedTotalStatements
let status = _statusMessage
lock.unlock()
onUpdate?(processed, total, status)
}
}