forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoTrigger.swift
More file actions
33 lines (28 loc) · 914 Bytes
/
AutoTrigger.swift
File metadata and controls
33 lines (28 loc) · 914 Bytes
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
import Foundation
import XPCShared
actor AutoTrigger {
static let shared = AutoTrigger()
var listeners = Set<ObjectIdentifier>()
private var task: Task<Void, Error>?
private init() {}
func start(by listener: ObjectIdentifier) {
listeners.insert(listener)
guard task == nil else { return }
task = Task {
while !Task.isCancelled {
guard UserDefaults.shared.bool(forKey: SettingsKey.isAutoTriggerEnabled) else {
continue
}
try await Task.sleep(nanoseconds: 2_000_000_000)
try? await Environment.triggerAction("Realtime Suggestions")
print("run")
}
}
}
func stop(by listener: ObjectIdentifier) {
listeners.remove(listener)
guard listeners.isEmpty else { return }
task?.cancel()
task = nil
}
}