Skip to content

Commit

Permalink
🎚enable clean shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejpurves committed Nov 23, 2023
1 parent 6b4f619 commit 04bf249
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/react/src/ThebeServerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export function ThebeServerProvider({
}
setReady(false);
setDoConnect(false);
setConnecting(false);
setError(undefined);
},
error,
}}
Expand Down
11 changes: 10 additions & 1 deletion packages/react/src/ThebeSessionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function ThebeSessionProvider({
const [session, setSession] = useState<ThebeSession | undefined>();
const [ready, setReady] = useState(false);
const [error, setError] = useState<string | undefined>();
const [unsubscribe, setUnsubscribe] = useState<(() => void) | undefined>();

/// Once server connection is open, auto start a session if start prop is true
useEffect(() => {
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -92,8 +93,12 @@ export function ThebeSessionProvider({
useEffect(() => {
return () => {
if (shutdownOnUnmount) {
unsubscribe?.();
setUnsubscribe(undefined);
session?.shutdown().then(() => {
setReady(false);
setStarting(false);
setError(undefined);
});
}
};
Expand All @@ -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,
Expand Down

0 comments on commit 04bf249

Please sign in to comment.