@@ -12,6 +12,8 @@ import XPCShared
1212
1313@ServiceActor
1414var workspaces = [ URL: Workspace] ( )
15+
16+ #warning("Todo: Find a better place to store it!")
1517@ServiceActor
1618var inflightRealtimeSuggestionsTasks = Set < Task < Void , Never > > ( )
1719
@@ -95,18 +97,14 @@ public class XPCService: NSObject, XPCServiceProtocol {
9597 editorContent: Data ,
9698 file: StaticString = #file,
9799 line: UInt = #line,
98- cancelInFlightTasks : Bool = true ,
100+ isRealtimeSuggestionRelatedCommand : Bool = false ,
99101 withReply reply: @escaping ( Data ? , Error ? ) -> Void ,
100102 getUpdatedContent: @escaping @ServiceActor (
101103 SuggestionCommandHanlder,
102104 EditorContent
103105 ) async throws -> UpdatedContent ?
104106 ) -> Task < Void , Never > {
105- Task {
106- if cancelInFlightTasks {
107- await RealtimeSuggestionController . shared
108- . cancelInFlightTasksAndIgnoreTriggerForAWhile ( )
109- }
107+ let task = Task {
110108 do {
111109 let editor = try JSONDecoder ( ) . decode ( EditorContent . self, from: editorContent)
112110 let handler = CommentBaseCommandHandler ( )
@@ -120,6 +118,17 @@ public class XPCService: NSObject, XPCServiceProtocol {
120118 reply ( nil , NSError . from ( error) )
121119 }
122120 }
121+
122+ Task {
123+ if isRealtimeSuggestionRelatedCommand {
124+ await RealtimeSuggestionController . shared
125+ . cancelInFlightTasks ( excluding: task)
126+ } else {
127+ await RealtimeSuggestionController . shared
128+ . cancelInFlightTasksAndIgnoreTriggerForAWhile ( excluding: task)
129+ }
130+ }
131+ return task
123132 }
124133
125134 public func getSuggestedCode(
@@ -173,7 +182,7 @@ public class XPCService: NSObject, XPCServiceProtocol {
173182 ) {
174183 let task = replyWithUpdatedContent (
175184 editorContent: editorContent,
176- cancelInFlightTasks : false ,
185+ isRealtimeSuggestionRelatedCommand : true ,
177186 withReply: reply
178187 ) { handler, editor in
179188 try await handler. presentRealtimeSuggestions ( editor: editor)
@@ -186,9 +195,13 @@ public class XPCService: NSObject, XPCServiceProtocol {
186195 editorContent: Data ,
187196 withReply reply: @escaping ( ) -> Void
188197 ) {
198+ // We don't need to wait for this.
199+ reply ( )
200+
189201 let task = replyWithUpdatedContent (
190202 editorContent: editorContent,
191- withReply: { _, _ in reply ( ) }
203+ isRealtimeSuggestionRelatedCommand: true ,
204+ withReply: { _, _ in }
192205 ) { handler, editor in
193206 try await handler. generateRealtimeSuggestions ( editor: editor)
194207 }
0 commit comments