From 9f05f067a6baeeab23718fcbf1356d8ded7d4fd7 Mon Sep 17 00:00:00 2001 From: Xin Wang Date: Tue, 16 Dec 2025 11:41:06 +0800 Subject: [PATCH] fix end call bug --- src/components/playground/PhoneSimulator.tsx | 16 +++++- src/components/playground/Playground.tsx | 59 +++++++++++++++++--- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/components/playground/PhoneSimulator.tsx b/src/components/playground/PhoneSimulator.tsx index 24305ea..1773209 100644 --- a/src/components/playground/PhoneSimulator.tsx +++ b/src/components/playground/PhoneSimulator.tsx @@ -218,7 +218,21 @@ export function PhoneSimulator({ }; const handleDisconnect = () => { - onDisconnect(); + try { + // Only disconnect if we're actually connected + if (roomState === ConnectionState.Connected || roomState === ConnectionState.Connecting) { + onDisconnect(); + } + } catch (error) { + // Silently handle any errors during disconnect + console.warn("Error during disconnect:", error); + // Still try to call onDisconnect to ensure cleanup + try { + onDisconnect(); + } catch (e) { + // Ignore secondary errors + } + } }; const validateImageFile = (file: File) => { diff --git a/src/components/playground/Playground.tsx b/src/components/playground/Playground.tsx index 4a14ee4..3af2719 100644 --- a/src/components/playground/Playground.tsx +++ b/src/components/playground/Playground.tsx @@ -76,6 +76,47 @@ export default function Playground({ const [rpcPayload, setRpcPayload] = useState(""); const [showRpc, setShowRpc] = useState(false); + // Clean up RPC resolvers before disconnecting to prevent errors + const cleanupRpcResolvers = useCallback(() => { + // Clean up any pending important message RPC + if (importantMessageResolverRef.current) { + const resolver = importantMessageResolverRef.current; + importantMessageResolverRef.current = null; + try { + // Only resolve if room is still connected to avoid RPC errors + if (roomState === ConnectionState.Connected) { + resolver("disconnected"); + } + } catch (error) { + // Ignore errors during cleanup - room might be disconnecting + } + } + // Clean up any pending image capture RPC + if (imageCaptureResolverRef.current) { + const resolver = imageCaptureResolverRef.current; + imageCaptureResolverRef.current = null; + try { + // Only resolve if room is still connected to avoid RPC errors + if (roomState === ConnectionState.Connected) { + resolver(JSON.stringify({ error: "disconnected" })); + } + } catch (error) { + // Ignore errors during cleanup - room might be disconnecting + } + } + }, [roomState]); + + // Wrapper for disconnect that cleans up RPC resolvers first + const handleDisconnect = useCallback(() => { + cleanupRpcResolvers(); + try { + onConnect(false); + } catch (error) { + // Silently handle any errors during disconnect + console.warn("Error during disconnect:", error); + } + }, [onConnect, cleanupRpcResolvers]); + useEffect(() => { if (roomState === ConnectionState.Connected) { localParticipant.setCameraEnabled(config.settings.inputs.camera); @@ -145,7 +186,7 @@ export default function Playground({ 'hangUpCall', async () => { // Disconnect the call - onConnect(false); + handleDisconnect(); return JSON.stringify({ success: true }); } ); @@ -179,7 +220,7 @@ export default function Playground({ }); } ); - }, [localParticipant, roomState, onConnect]); + }, [localParticipant, roomState, handleDisconnect]); useEffect(() => { if (roomState === ConnectionState.Connected) { @@ -721,7 +762,7 @@ export default function Playground({ > onConnect(true)} - onDisconnect={() => onConnect(false)} + onDisconnect={handleDisconnect} phoneMode={phoneMode} capturePrompt={capturePrompt} importantMessage={importantMessage} @@ -792,9 +833,13 @@ export default function Playground({ height={headerHeight} accentColor={config.settings.theme_color} connectionState={roomState} - onConnectClicked={() => - onConnect(roomState === ConnectionState.Disconnected) - } + onConnectClicked={() => { + if (roomState === ConnectionState.Disconnected) { + onConnect(true); + } else { + handleDisconnect(); + } + }} />
onConnect(true)} - onDisconnect={() => onConnect(false)} + onDisconnect={handleDisconnect} phoneMode={phoneMode} capturePrompt={capturePrompt} importantMessage={importantMessage}