From 44e3e886ca9cb5f1755254035917480345c520a6 Mon Sep 17 00:00:00 2001 From: stevejpurves Date: Wed, 22 Nov 2023 11:36:55 +0000 Subject: [PATCH] =?UTF-8?q?=E2=98=9D=F0=9F=8F=BCpromote=20session=20shutdo?= =?UTF-8?q?wn=20events=20to=20errors=20for=20thebe=20sessions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/tender-sloths-battle.md | 5 +++++ packages/react/src/ThebeSessionProvider.tsx | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 .changeset/tender-sloths-battle.md diff --git a/.changeset/tender-sloths-battle.md b/.changeset/tender-sloths-battle.md new file mode 100644 index 00000000..e1e50b8f --- /dev/null +++ b/.changeset/tender-sloths-battle.md @@ -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. diff --git a/packages/react/src/ThebeSessionProvider.tsx b/packages/react/src/ThebeSessionProvider.tsx index 820bc0ef..da4916c7 100644 --- a/packages/react/src/ThebeSessionProvider.tsx +++ b/packages/react/src/ThebeSessionProvider.tsx @@ -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; @@ -27,6 +29,7 @@ export function ThebeSessionProvider({ path?: string; shutdownOnUnmount?: boolean; }>) { + const { core } = useThebeLoader(); const { config, server, ready: serverReady } = useThebeServer(); const rendermime = useRenderMimeRegistry(); @@ -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);