Skip to content

Commit

Permalink
☝🏼promote session shutdown events to errors for thebe sessions (#712)
Browse files Browse the repository at this point in the history
* ☝🏼promote session shutdown events to errors for thebe sessions
* 📃added default args to clear session storage helpers
* 🦠 better path injection
* 👍 allow hypens in session names
  • Loading branch information
stevejpurves authored Nov 22, 2023
1 parent 203cc04 commit 6d29986
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 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.
5 changes: 5 additions & 0 deletions .changeset/twenty-pigs-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'thebe-core': patch
---

Added default arguments to clear sesion storage utility functions
2 changes: 1 addition & 1 deletion packages/core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class ThebeServer implements ServerRuntime, ServerRestAPI {
// https://jupyterlab.readthedocs.io/en/3.4.x/api/modules/services.session.html#isessionoptions
const path = kernelOptions?.path ?? this.config.kernels.path;
let name = 'thebe.ipynb';
const match = path.match(/\/*([a-zA-Z0-9]+.ipynb)$/);
const match = path.match(/\/*([a-zA-Z0-9-]+.ipynb)$/);
if (match) {
name = match[1];
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export async function getExistingServer(
*
* @param storagePrefix
*/
export function clearAllSavedSessions(storagePrefix: string) {
export function clearAllSavedSessions(storagePrefix: string = 'thebe-binder') {
const keysToRemove: string[] = [];
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
Expand All @@ -117,7 +117,7 @@ export function clearAllSavedSessions(storagePrefix: string) {
* @param storagePrefix
* @param url
*/
export function clearSavedSession(storagePrefix: string, url: string) {
export function clearSavedSession(storagePrefix: string = 'thebe-binder', url: string = '') {
console.debug(`thebe:clearSavedSession - removing ${makeStorageKey(storagePrefix, url)}`);
window.localStorage.removeItem(makeStorageKey(storagePrefix, url));
}
24 changes: 22 additions & 2 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 @@ -19,14 +21,15 @@ export const ThebeSessionContext = React.createContext<ThebeSessionContextData |

export function ThebeSessionProvider({
start = true,
path = '/thebe.ipynb',
path,
shutdownOnUnmount = false,
children,
}: React.PropsWithChildren<{
start?: boolean;
path?: string;
shutdownOnUnmount?: boolean;
}>) {
const { core } = useThebeLoader();
const { config, server, ready: serverReady } = useThebeServer();
const rendermime = useRenderMimeRegistry();

Expand All @@ -42,10 +45,27 @@ 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 ${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);
server?.startNewSession(rendermime, { ...config?.kernels, path }).then(
server?.startNewSession(rendermime, { path }).then(
(sesh: ThebeSession | null) => {
setStarting(false);
if (sesh == null) {
Expand Down

0 comments on commit 6d29986

Please sign in to comment.