From f14adb452da11a444bcc89f0f2c7c8bbfc599fe0 Mon Sep 17 00:00:00 2001 From: haritabh-z01 Date: Thu, 13 Nov 2025 09:09:22 +0530 Subject: [PATCH] chore: startup perf optimisation --- apps/desktop/src/hooks/useAudioCapture.ts | 52 ++++++++++++++++------- apps/desktop/src/services/auth-service.ts | 2 +- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/apps/desktop/src/hooks/useAudioCapture.ts b/apps/desktop/src/hooks/useAudioCapture.ts index dbe0349..48e8ce9 100644 --- a/apps/desktop/src/hooks/useAudioCapture.ts +++ b/apps/desktop/src/hooks/useAudioCapture.ts @@ -94,21 +94,40 @@ export const useAudioCapture = ({ `AudioCapture: getUserMedia took ${getUserMediaDuration.toFixed(2)}ms`, ); - // Create audio context + // Create or resume audio context const audioContextStartTime = performance.now(); - audioContextRef.current = new AudioContext({ sampleRate: SAMPLE_RATE }); - const audioContextDuration = performance.now() - audioContextStartTime; - console.log( - `AudioCapture: AudioContext creation took ${audioContextDuration.toFixed(2)}ms`, - ); + if ( + audioContextRef.current && + audioContextRef.current.state === "suspended" + ) { + // Resume existing context (faster) + await audioContextRef.current.resume(); + const resumeDuration = performance.now() - audioContextStartTime; + console.log( + `AudioCapture: AudioContext resumed took ${resumeDuration.toFixed(2)}ms`, + ); + } else if (!audioContextRef.current) { + // Create new context (first time only) + audioContextRef.current = new AudioContext({ + sampleRate: SAMPLE_RATE, + }); + const audioContextDuration = + performance.now() - audioContextStartTime; + console.log( + `AudioCapture: AudioContext creation took ${audioContextDuration.toFixed(2)}ms`, + ); - // Load audio worklet - const workletStartTime = performance.now(); - await audioContextRef.current.audioWorklet.addModule(audioWorkletUrl); - const workletDuration = performance.now() - workletStartTime; - console.log( - `AudioCapture: audioWorklet.addModule took ${workletDuration.toFixed(2)}ms`, - ); + // Load audio worklet (only needed on first creation) + const workletStartTime = performance.now(); + await audioContextRef.current.audioWorklet.addModule(audioWorkletUrl); + const workletDuration = performance.now() - workletStartTime; + console.log( + `AudioCapture: audioWorklet.addModule took ${workletDuration.toFixed(2)}ms`, + ); + } else { + // Context exists but not suspended (already running) + console.log("AudioCapture: AudioContext already running"); + } // Create nodes const nodeCreationStartTime = performance.now(); @@ -190,12 +209,13 @@ export const useAudioCapture = ({ sourceRef.current.disconnect(workletNodeRef.current); } - // Close audio context + // Suspend audio context (keep it alive for next recording) if ( audioContextRef.current && - audioContextRef.current.state !== "closed" + audioContextRef.current.state === "running" ) { - await audioContextRef.current.close(); + await audioContextRef.current.suspend(); + console.log("AudioCapture: AudioContext suspended (ready for reuse)"); } // Stop media stream diff --git a/apps/desktop/src/services/auth-service.ts b/apps/desktop/src/services/auth-service.ts index eb4db03..4be1f18 100644 --- a/apps/desktop/src/services/auth-service.ts +++ b/apps/desktop/src/services/auth-service.ts @@ -329,7 +329,7 @@ export class AuthService extends EventEmitter { // Check if token needs refresh (10 minutes before expiry) if ( authState.expiresAt && - authState.expiresAt - Date.now() > 1000 * 60 * 1000 + authState.expiresAt - Date.now() > 10 * 60 * 1000 ) { // Token still valid return;