Skip to content

Commit 7e38c07

Browse files
committed
Merge branch 'feature/fix-codeium-cancellation-error' into develop
2 parents 4bbd5f6 + bd3100a commit 7e38c07

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

Core/Sources/Service/SuggestionPresenter/PresentInWindowSuggestionPresenter.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct PresentInWindowSuggestionPresenter {
2828

2929
func presentError(_ error: Error) {
3030
if error is CancellationError { return }
31+
if let urlError = error as? URLError, urlError.code == URLError.cancelled { return }
3132
Task { @MainActor in
3233
let controller = GraphicalUserInterfaceController.shared.suggestionWidget
3334
controller.presentError(error.localizedDescription)

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public final class SuggestionWidgetController: NSObject {
275275

276276
public extension SuggestionWidgetController {
277277
func suggestCode(fileURL: URL) {
278-
widgetViewModel.isProcessing = false
278+
widgetViewModel.endIsProcessing()
279279
Task {
280280
if let suggestion = await dataSource?.suggestionForFile(at: fileURL) {
281281
suggestionPanelViewModel.content = .suggestion(suggestion)
@@ -285,24 +285,28 @@ public extension SuggestionWidgetController {
285285
}
286286

287287
func discardSuggestion(fileURL: URL) {
288-
widgetViewModel.isProcessing = false
288+
widgetViewModel.endIsProcessing()
289289
Task {
290290
await updateContentForActiveEditor(fileURL: fileURL)
291291
}
292292
}
293293

294294
func markAsProcessing(_ isProcessing: Bool) {
295-
widgetViewModel.isProcessing = isProcessing
295+
if isProcessing {
296+
widgetViewModel.markIsProcessing()
297+
} else {
298+
widgetViewModel.endIsProcessing()
299+
}
296300
}
297301

298302
func presentError(_ errorDescription: String) {
299303
suggestionPanelViewModel.content = .error(errorDescription)
300304
suggestionPanelViewModel.isPanelDisplayed = true
301-
widgetViewModel.isProcessing = false
305+
widgetViewModel.endIsProcessing()
302306
}
303307

304308
func presentChatRoom(fileURL: URL) {
305-
widgetViewModel.isProcessing = false
309+
widgetViewModel.endIsProcessing()
306310
Task {
307311
if let chat = await dataSource?.chatForFile(at: fileURL) {
308312
chatWindowViewModel.chat = chat
@@ -343,14 +347,14 @@ public extension SuggestionWidgetController {
343347
}
344348

345349
func closeChatRoom(fileURL: URL) {
346-
widgetViewModel.isProcessing = false
350+
widgetViewModel.endIsProcessing()
347351
Task {
348352
await updateContentForActiveEditor(fileURL: fileURL)
349353
}
350354
}
351355

352356
func presentPromptToCode(fileURL: URL) {
353-
widgetViewModel.isProcessing = false
357+
widgetViewModel.endIsProcessing()
354358
Task {
355359
if let provider = await dataSource?.promptToCodeForFile(at: fileURL) {
356360
suggestionPanelViewModel.content = .promptToCode(provider)
@@ -367,7 +371,7 @@ public extension SuggestionWidgetController {
367371
}
368372

369373
func discardPromptToCode(fileURL: URL) {
370-
widgetViewModel.isProcessing = false
374+
widgetViewModel.endIsProcessing()
371375
Task {
372376
await updateContentForActiveEditor(fileURL: fileURL)
373377
}

Core/Sources/SuggestionWidget/WidgetView.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,40 @@ import SwiftUI
66

77
@MainActor
88
final class WidgetViewModel: ObservableObject {
9+
struct IsProcessingCounter {
10+
var expirationDate: TimeInterval
11+
}
12+
13+
private var isProcessingCounters = [IsProcessingCounter]()
14+
private var cleanupIsProcessingCounterTask: Task<Void, Error>?
915
@Published var isProcessing: Bool
1016
@Published var currentFileURL: URL?
1117

18+
func markIsProcessing(date: Date = Date()) {
19+
let deadline = date.timeIntervalSince1970 + 20
20+
isProcessingCounters.append(IsProcessingCounter(expirationDate: deadline))
21+
isProcessing = true
22+
23+
cleanupIsProcessingCounterTask?.cancel()
24+
cleanupIsProcessingCounterTask = Task { [weak self] in
25+
try await Task.sleep(nanoseconds: 20 * 1_000_000_000)
26+
try Task.checkCancellation()
27+
Task { @MainActor [weak self] in
28+
guard let self else { return }
29+
isProcessingCounters.removeAll()
30+
isProcessing = false
31+
}
32+
}
33+
}
34+
35+
func endIsProcessing(date: Date = Date()) {
36+
if !isProcessingCounters.isEmpty {
37+
isProcessingCounters.removeFirst()
38+
}
39+
isProcessingCounters.removeAll(where: { $0.expirationDate < date.timeIntervalSince1970 })
40+
isProcessing = !isProcessingCounters.isEmpty
41+
}
42+
1243
init(isProcessing: Bool = false) {
1344
self.isProcessing = isProcessing
1445
}

0 commit comments

Comments
 (0)