Skip to content

Commit a7113b1

Browse files
authored
Merge pull request #21 from layercodedev/fix/stable-disconnect-reference
fix: make disconnect() have stable reference to prevent unexpected cleanup
2 parents d535328 + 2085a4c commit a7113b1

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,19 @@ const useLayercodeAgent = (
313313

314314
// Class methods
315315
const mute = useCallback(() => {
316-
clientRef.current?.mute();
316+
if (!clientRef.current) {
317+
console.warn('[Layercode] mute() called but no client exists. Did you call connect() first?');
318+
return;
319+
}
320+
clientRef.current.mute();
317321
}, []);
318322

319323
const unmute = useCallback(() => {
320-
clientRef.current?.unmute();
324+
if (!clientRef.current) {
325+
console.warn('[Layercode] unmute() called but no client exists. Did you call connect() first?');
326+
return;
327+
}
328+
clientRef.current.unmute();
321329
}, []);
322330

323331
const setAudioInput = useCallback(
@@ -407,6 +415,7 @@ const useLayercodeAgent = (
407415
}, [createClient, internalConversationId, onError, refreshInputDevices]);
408416
const disconnect = useCallback(async () => {
409417
if (!clientRef.current) {
418+
console.warn('[Layercode] disconnect() called but no client exists');
410419
return;
411420
}
412421

@@ -417,10 +426,9 @@ const useLayercodeAgent = (
417426
await client.disconnect();
418427
} catch (error) {
419428
console.error('Failed to disconnect from agent:', error);
420-
onError?.(error as Error);
421429
throw error;
422430
}
423-
}, [onError]);
431+
}, []);
424432

425433
// Return methods and state
426434
return {

0 commit comments

Comments
 (0)