Fix cancellation race conditions

This commit is contained in:
Beingpax 2025-10-04 23:08:50 +05:45
parent 0265333722
commit 602654e733
3 changed files with 18 additions and 24 deletions

View File

@ -88,8 +88,7 @@ class MiniRecorderShortcutManager: ObservableObject {
if let firstTime = self.escFirstPressTime,
now.timeIntervalSince(firstTime) <= self.escSecondPressThreshold {
self.escFirstPressTime = nil
SoundManager.shared.playEscSound()
await self.whisperState.dismissMiniRecorder()
await self.whisperState.cancelRecording()
} else {
self.escFirstPressTime = now
SoundManager.shared.playEscSound()
@ -125,9 +124,8 @@ class MiniRecorderShortcutManager: ObservableObject {
guard let self = self,
await self.whisperState.isMiniRecorderVisible,
KeyboardShortcuts.getShortcut(for: .cancelRecorder) != nil else { return }
SoundManager.shared.playEscSound()
await self.whisperState.dismissMiniRecorder()
await self.whisperState.cancelRecording()
}
}
}

View File

@ -54,11 +54,9 @@ extension WhisperState {
func dismissMiniRecorder() async {
if recordingState == .busy { return }
let wasRecording = recordingState == .recording
logger.notice("📱 Dismissing \(self.recorderType) recorder")
await MainActor.run {
self.recordingState = .busy
}

View File

@ -351,12 +351,8 @@ class WhisperState: NSObject, ObservableObject {
finalPastedText = enhancedText
} catch {
transcription.enhancedText = "Enhancement failed: \(error)"
await MainActor.run {
NotificationManager.shared.showNotification(
title: "AI enhancement failed",
type: .error
)
}
if await checkCancellationAndCleanup() { return }
}
}
@ -374,7 +370,9 @@ class WhisperState: NSObject, ObservableObject {
// --- Finalize and save ---
try? modelContext.save()
NotificationCenter.default.post(name: .transcriptionCreated, object: transcription)
if await checkCancellationAndCleanup() { return }
if var textToPaste = finalPastedText, transcription.transcriptionStatus == TranscriptionStatus.completed.rawValue {
if case .trialExpired = licenseViewModel.licenseState {
textToPaste = """
@ -382,14 +380,12 @@ class WhisperState: NSObject, ObservableObject {
\n\(textToPaste)
"""
}
let shouldAddSpace = UserDefaults.standard.object(forKey: "AppendTrailingSpace") as? Bool ?? true
if shouldAddSpace {
textToPaste += " "
}
if await checkCancellationAndCleanup() { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 0.05) {
CursorPaster.pasteAtCursor(textToPaste)
@ -402,14 +398,16 @@ class WhisperState: NSObject, ObservableObject {
}
}
}
if let result = promptDetectionResult,
let enhancementService = enhancementService,
result.shouldEnableAI {
await promptDetectionService.restoreOriginalSettings(result, to: enhancementService)
}
await self.dismissMiniRecorder()
shouldCancelRecording = false
}
func getEnhancementService() -> AIEnhancementService? {
@ -418,12 +416,12 @@ class WhisperState: NSObject, ObservableObject {
private func checkCancellationAndCleanup() async -> Bool {
if shouldCancelRecording {
await dismissMiniRecorder()
await cleanupModelResources()
return true
}
return false
}
private func cleanupAndDismiss() async {
await dismissMiniRecorder()
}