Skip to content

Commit 8a3550a

Browse files
committed
Fix real-time suggestion cancellation
1 parent 12018dc commit 8a3550a

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

Core/Sources/Service/AutoTrigger.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public actor AutoTrigger {
6464
if task == nil {
6565
task = Task { [stream = eventObserver.stream] in
6666
var triggerTask: Task<Void, Error>?
67-
for await _ in stream {
67+
for await eventType in stream {
6868
triggerTask?.cancel()
6969
if Task.isCancelled { break }
7070
guard await Environment.isXcodeActive() else { continue }
@@ -77,16 +77,17 @@ public actor AutoTrigger {
7777
}
7878
}
7979

80+
guard eventType == .keyUp else { continue }
81+
8082
triggerTask = Task { @ServiceActor in
81-
try? await Task.sleep(nanoseconds: 2_000_000_000)
83+
try? await Task.sleep(nanoseconds: 1_500_000_000)
8284
if Task.isCancelled { return }
8385
let fileURL = try? await Environment.fetchCurrentFileURL()
84-
guard let folderURL = try? await Environment
85-
.fetchCurrentProjectRootURL(fileURL)
86-
else { return }
87-
let workspace = workspaces[folderURL] ??
88-
Workspace(projectRootURL: folderURL)
89-
workspaces[folderURL] = workspace
86+
let folderURL = try? await Environment.fetchCurrentProjectRootURL(fileURL)
87+
guard let workspaceURL = folderURL ?? fileURL else { return }
88+
let workspace = workspaces[workspaceURL]
89+
?? Workspace(projectRootURL: workspaceURL)
90+
workspaces[workspaceURL] = workspace
9091
guard workspace.isRealtimeSuggestionEnabled else { return }
9192
if Task.isCancelled { return }
9293
try? await Environment.triggerAction("Prefetch Suggestions")

Core/Sources/Service/CGEventObserver.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ public protocol CGEventObserverType {
66
@discardableResult
77
func activateIfPossible() -> Bool
88
func deactivate()
9-
var stream: AsyncStream<Void> { get }
9+
var stream: AsyncStream<CGEventType> { get }
1010
var isEnabled: Bool { get }
1111
}
1212

1313
final class CGEventObserver: CGEventObserverType {
14-
let stream: AsyncStream<Void>
14+
let stream: AsyncStream<CGEventType>
1515
var isEnabled: Bool { port != nil }
1616

17-
private var continuation: AsyncStream<Void>.Continuation
17+
private var continuation: AsyncStream<CGEventType>.Continuation
1818
private var port: CFMachPort?
19-
private let eventsOfInterest: Set<CGEventType> = [.keyUp]
19+
private let eventsOfInterest: Set<CGEventType> = [
20+
.keyUp,
21+
.keyDown,
22+
.rightMouseDown,
23+
.leftMouseDown,
24+
]
2025
private let tapLocation: CGEventTapLocation = .cghidEventTap
2126
private let tapPlacement: CGEventTapPlacement = .tailAppendEventTap
2227
private let tapOptions: CGEventTapOptions = .listenOnly
@@ -28,7 +33,7 @@ final class CGEventObserver: CGEventObserverType {
2833
}
2934

3035
init() {
31-
var continuation: AsyncStream<Void>.Continuation!
36+
var continuation: AsyncStream<CGEventType>.Continuation!
3237
stream = AsyncStream { c in
3338
continuation = c
3439
}
@@ -66,9 +71,9 @@ final class CGEventObserver: CGEventObserverType {
6671
}
6772

6873
if let continuation = continuationPointer?
69-
.assumingMemoryBound(to: AsyncStream<Void>.Continuation.self)
74+
.assumingMemoryBound(to: AsyncStream<CGEventType>.Continuation.self)
7075
{
71-
continuation.pointee.yield(())
76+
continuation.pointee.yield(eventType)
7277
}
7378

7479
return .passRetained(event)

0 commit comments

Comments
 (0)