Skip to content

Commit

Permalink
☝🏼promote session shutdown events to errors for thebe sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejpurves committed Nov 22, 2023
1 parent 203cc04 commit 44e3e88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-sloths-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'thebe-react': patch
---

`ThebeSessionProvider` monitors status events for session/kernel shutdown messages and promotes this to an error if the corresponding session has shutdown.
20 changes: 20 additions & 0 deletions packages/react/src/ThebeSessionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { useContext, useEffect, useState } from 'react';
import type { ThebeSession } from 'thebe-core';
import { useThebeServer } from './ThebeServerProvider';
import { useRenderMimeRegistry } from './ThebeRenderMimeRegistryProvider';
import { ThebeEventData } from 'thebe-core';
import { useThebeLoader } from './ThebeLoaderProvider';

interface ThebeSessionContextData {
path?: string;
Expand All @@ -27,6 +29,7 @@ export function ThebeSessionProvider({
path?: string;
shutdownOnUnmount?: boolean;
}>) {
const { core } = useThebeLoader();
const { config, server, ready: serverReady } = useThebeServer();
const rendermime = useRenderMimeRegistry();

Expand All @@ -42,6 +45,23 @@ export function ThebeSessionProvider({
startSession();
}, [ready, doStart, starting, server, serverReady]);

// register an event handler to monitor for session status changes
useEffect(() => {
if (!core || !config || !session) return;
const handler = (evt: string, data: ThebeEventData) => {
const subjects = [core.EventSubject.session, core.EventSubject.kernel];
if (
data.subject &&
subjects.includes(data.subject) &&
data.status === 'shutdown' &&
data.id === session.id
) {
setError(`session ${path} - ${data.status} - ${data.message}`);
}
};
config.events.on(core.ThebeEventType.status, handler);
}, [core, config, session]);

const startSession = () => {
if (!rendermime) throw new Error('ThebeSessionProvider requires a RenderMimeRegistryProvider');
setStarting(true);
Expand Down

0 comments on commit 44e3e88

Please sign in to comment.