diff --git a/packages/react/src/ThebeServerProvider.tsx b/packages/react/src/ThebeServerProvider.tsx index 87e6626b..9d5da836 100644 --- a/packages/react/src/ThebeServerProvider.tsx +++ b/packages/react/src/ThebeServerProvider.tsx @@ -142,6 +142,8 @@ export function ThebeServerProvider({ } setReady(false); setDoConnect(false); + setConnecting(false); + setError(undefined); }, error, }} diff --git a/packages/react/src/ThebeSessionProvider.tsx b/packages/react/src/ThebeSessionProvider.tsx index 27a5694d..dad29615 100644 --- a/packages/react/src/ThebeSessionProvider.tsx +++ b/packages/react/src/ThebeSessionProvider.tsx @@ -38,6 +38,7 @@ export function ThebeSessionProvider({ const [session, setSession] = useState(); const [ready, setReady] = useState(false); const [error, setError] = useState(); + const [unsubscribe, setUnsubscribe] = useState<(() => void) | undefined>(); /// Once server connection is open, auto start a session if start prop is true useEffect(() => { @@ -59,7 +60,7 @@ export function ThebeSessionProvider({ setError(`session ${session.path} - ${data.status} - ${data.message}`); } }; - config.events.on(core.ThebeEventType.status, handler); + setUnsubscribe(config.events.on(core.ThebeEventType.status, handler)); }, [core, config, session]); const startSession = () => { @@ -92,8 +93,12 @@ export function ThebeSessionProvider({ useEffect(() => { return () => { if (shutdownOnUnmount) { + unsubscribe?.(); + setUnsubscribe(undefined); session?.shutdown().then(() => { setReady(false); + setStarting(false); + setError(undefined); }); } }; @@ -116,9 +121,13 @@ export function ThebeSessionProvider({ }, shutdown: async () => { if (session) { + unsubscribe?.(); + setUnsubscribe(undefined); await session.shutdown(); setSession(undefined); setReady(false); + setStarting(false); + setError(undefined); } }, error,