Skip to content

Commit 430c7b8

Browse files
committed
Add cancel in-flight real-time suggestion request on keyboard or mouse events
1 parent 0327f82 commit 430c7b8

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

Core/Sources/Service/AutoTrigger.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@ actor AutoTrigger {
3232
for await _ in stream {
3333
triggerTask?.cancel()
3434
if Task.isCancelled { break }
35+
guard await Environment.isXcodeActive() else { continue }
36+
37+
await withTaskGroup(of: Void.self) { group in
38+
for (_, workspace) in await workspaces {
39+
group.addTask {
40+
await workspace.cancelAllRealtimeSuggestionFulfillmentTasks()
41+
}
42+
}
43+
}
44+
3545
triggerTask = Task { @ServiceActor in
36-
try? await Task.sleep(nanoseconds: 3_000_000_000)
46+
try? await Task.sleep(nanoseconds: 2_500_000_000)
3747
if Task.isCancelled { return }
3848
let fileURL = try? await Environment.fetchCurrentFileURL()
3949
guard let folderURL = try? await Environment.fetchCurrentProjectRootURL(fileURL),

Core/Sources/Service/CGEventObserver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class CGEventObserver: CGEventObserverType {
1515

1616
private var continuation: AsyncStream<Void>.Continuation
1717
private var port: CFMachPort?
18-
private let eventsOfInterest: Set<CGEventType> = [.keyUp, .leftMouseUp, .mouseMoved]
18+
private let eventsOfInterest: Set<CGEventType> = [.keyUp, .leftMouseUp, .rightMouseUp]
1919
private let tapLocation: CGEventTapLocation = .cghidEventTap
2020
private let tapPlacement: CGEventTapPlacement = .tailAppendEventTap
2121
private let tapOptions: CGEventTapOptions = .listenOnly

Core/Sources/Service/Environment.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ private struct FailedToFetchFileURLError: Error, LocalizedError {
1616

1717
enum Environment {
1818
static var now = { Date() }
19+
20+
static var isXcodeActive: () async -> Bool = {
21+
var activeXcodes = [NSRunningApplication]()
22+
var retryCount = 0
23+
// Sometimes runningApplications returns 0 items.
24+
while activeXcodes.isEmpty, retryCount < 3 {
25+
activeXcodes = NSRunningApplication
26+
.runningApplications(withBundleIdentifier: "com.apple.dt.Xcode")
27+
.sorted { lhs, _ in
28+
if lhs.isActive { return true }
29+
return false
30+
}
31+
if retryCount > 0 { try? await Task.sleep(nanoseconds: 1_000_000) }
32+
retryCount += 1
33+
}
34+
return !activeXcodes.isEmpty
35+
}
1936

2037
static var fetchCurrentProjectRootURL: (_ fileURL: URL?) async throws -> URL? = { fileURL in
2138
let appleScript = """

0 commit comments

Comments
 (0)