Skip to content

Commit

Permalink
πŸ”Œ connect w promise (#708)
Browse files Browse the repository at this point in the history
* ⏰ connect returns promise
* πŸ“— changeset
  • Loading branch information
stevejpurves authored Nov 21, 2023
1 parent 092b05c commit 358b62e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-comics-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'thebe-react': patch
---

ThebeServerProvider's connect function returns a promise
25 changes: 16 additions & 9 deletions packages/react/src/ThebeServerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,8 @@ export function ThebeServerProvider({
setServer(svr);
}, [core, thebeConfig, server]);

// 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;
// TODO is the user server really still alive? this would be an async call to server.check
const connectToServer = useCallback(() => {
if (!server) return;
setConnecting(true);
if (customConnectFn) customConnectFn(server);
else if (useBinder) server.connectToServerViaBinder(customRepoProviders);
Expand All @@ -117,6 +111,19 @@ export function ThebeServerProvider({
setReady(false);
},
);

return server.ready;
}, [server]);

// 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;
// TODO is the user server really still alive? this would be an async call to server.check
connectToServer();
}, [server, doConnect]);

return (
Expand All @@ -127,7 +134,7 @@ export function ThebeServerProvider({
server,
connecting,
ready: (server?.isReady ?? false) && ready, // TODO server status may change, affecting readiness
connect: () => setDoConnect(true),
connect: connectToServer,
disconnect: async () => {
if (core && thebeConfig && server) {
server.dispose();
Expand Down

0 comments on commit 358b62e

Please sign in to comment.