forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceCommandQueue.swift
More file actions
70 lines (62 loc) · 2.04 KB
/
Copy pathDeviceCommandQueue.swift
File metadata and controls
70 lines (62 loc) · 2.04 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
import Foundation
enum DeviceCommandQueueError: LocalizedError, Equatable {
case closed
var errorDescription: String? {
"The device command channel is closed"
}
}
/// Serializes adapter commands that share one uncorrelated BLE write channel.
///
/// Device command responses carry a command ID, but the physical write callback
/// identifies only the characteristic. Keeping the whole request/response
/// exchange serial prevents a second command from being rejected locally after
/// its response gate was registered, and makes callback ownership explicit.
@MainActor
final class DeviceCommandQueue {
private var tail = Task<Void, Never> {}
private var nextToken: UInt64 = 0
private var cancellationActions: [UInt64: @MainActor () -> Void] = [:]
private var isClosed = false
func run<Value: Sendable>(
_ operation: @escaping @MainActor @Sendable () async throws -> Value
) async throws -> Value {
guard !isClosed else { throw DeviceCommandQueueError.closed }
nextToken &+= 1
let token = nextToken
let predecessor = tail
let task = Task { @MainActor [weak self] in
await predecessor.value
guard let self, !self.isClosed else {
throw DeviceCommandQueueError.closed
}
try Task.checkCancellation()
return try await operation()
}
cancellationActions[token] = {
task.cancel()
}
tail = Task { @MainActor [weak self] in
_ = try? await task.value
self?.cancellationActions.removeValue(forKey: token)
}
return try await withTaskCancellationHandler {
try await task.value
} onCancel: {
task.cancel()
}
}
/// Permanently closes the one-session queue, cancels active and queued
/// commands, and joins the tail before adapter brokers are reset.
func close() async {
guard !isClosed else {
await tail.value
return
}
isClosed = true
let actions = Array(cancellationActions.values)
let drainingTail = tail
actions.forEach { $0() }
await drainingTail.value
cancellationActions.removeAll()
}
}