Skip to content

Commit 6c86dfc

Browse files
authored
⚙️ enable server re-configuration (#785)
1 parent 2f69ba2 commit 6c86dfc

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

.changeset/selfish-boats-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'thebe-react': patch
3+
---
4+
5+
Enable `ThebeServer` to be reconfigured via the provider.

packages/react/src/ThebeServerProvider.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
1+
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
22
import type {
33
Config,
44
CoreOptions,
5-
EventSubject,
65
RepoProviderSpec,
76
ThebeEventCb,
87
ThebeEventData,
@@ -53,21 +52,24 @@ export function ThebeServerProvider({
5352
const [server, setServer] = useState<ThebeServer | undefined>();
5453
const [ready, setReady] = useState<boolean>(false);
5554
const [error, setError] = useState<string | undefined>();
55+
const configRef = useRef<Config | undefined>();
5656

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

6565
// create an iniital server
6666
useEffect(() => {
67-
if (!core || !thebeConfig || server) return;
67+
if (!core || !thebeConfig) return; // not intialized yet
68+
else if (thebeConfig === configRef.current && server) return; // config has not changed and server is already created
69+
6870
const svr = new core.ThebeServer(thebeConfig);
6971

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

@@ -118,7 +121,6 @@ export function ThebeServerProvider({
118121
// Once the core is loaded, connect to a server
119122
// TODO: this should be an action not a side effect
120123
useEffect(() => {
121-
if (!core || !thebeConfig) return; // TODO is there a better way to keep typescript happy here?
122124
if (!server || !doConnect) return;
123125
// do not reconnect if already connected!
124126
if (server.isReady && server.userServerUrl) return;

packages/react/src/ThebeSessionProvider.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useContext, useEffect, useState } from 'react';
2-
import type { ThebeSession } from 'thebe-core';
2+
import type { ThebeSession, ThebeEventData } from 'thebe-core';
33
import { useThebeServer } from './ThebeServerProvider';
44
import { useRenderMimeRegistry } from './ThebeRenderMimeRegistryProvider';
5-
import { ThebeEventData } from 'thebe-core';
65
import { useThebeLoader } from './ThebeLoaderProvider';
76

87
interface ThebeSessionContextData {

0 commit comments

Comments
 (0)