Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚙️ enable server re-configuration #785

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/selfish-boats-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'thebe-react': patch
---

Enable `ThebeServer` to be reconfigured via the provider.
14 changes: 8 additions & 6 deletions packages/react/src/ThebeServerProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import type {
Config,
CoreOptions,
EventSubject,
RepoProviderSpec,
ThebeEventCb,
ThebeEventData,
Expand Down Expand Up @@ -53,21 +52,24 @@ export function ThebeServerProvider({
const [server, setServer] = useState<ThebeServer | undefined>();
const [ready, setReady] = useState<boolean>(false);
const [error, setError] = useState<string | undefined>();
const configRef = useRef<Config | undefined>();

// create a valid configuration, either using the one supplied
// or based on the options provided
// once only - if options/config were to change, we'd need logic to create a new server etc..
const thebeConfig = useMemo(
() => config ?? core?.makeConfiguration(options ?? {}, events),
[core, options],
[core, config, options],
);

// create an iniital server
useEffect(() => {
if (!core || !thebeConfig || server) return;
if (!core || !thebeConfig) return; // not intialized yet
else if (thebeConfig === configRef.current && server) return; // config has not changed and server is already created

const svr = new core.ThebeServer(thebeConfig);

// register an error handler immedately
// register an error handler immediately
const handler = (evt: string, data: ThebeEventData) => {
const subjects = [
core.EventSubject.server,
Expand All @@ -81,6 +83,7 @@ export function ThebeServerProvider({
// TODO we need a way to unsubscribe from this that does not cause
// error events to be missed due to rerenders
thebeConfig.events.on(core.ThebeEventType.error, handler);
configRef.current = thebeConfig;
setServer(svr);
}, [core, thebeConfig, server]);

Expand Down Expand Up @@ -118,7 +121,6 @@ export function ThebeServerProvider({
// Once the core is loaded, connect to a server
// TODO: this should be an action not a side effect
useEffect(() => {
if (!core || !thebeConfig) return; // TODO is there a better way to keep typescript happy here?
if (!server || !doConnect) return;
// do not reconnect if already connected!
if (server.isReady && server.userServerUrl) return;
Expand Down
3 changes: 1 addition & 2 deletions packages/react/src/ThebeSessionProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useContext, useEffect, useState } from 'react';
import type { ThebeSession } from 'thebe-core';
import type { ThebeSession, ThebeEventData } from 'thebe-core';
import { useThebeServer } from './ThebeServerProvider';
import { useRenderMimeRegistry } from './ThebeRenderMimeRegistryProvider';
import { ThebeEventData } from 'thebe-core';
import { useThebeLoader } from './ThebeLoaderProvider';

interface ThebeSessionContextData {
Expand Down
Loading