-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate project runtime from editor (#2788)
- Loading branch information
Showing
23 changed files
with
276 additions
and
304 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Emitter } from '@mui/toolpad-utils/events'; | ||
import { QueryClient, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import * as React from 'react'; | ||
import { useNonNullableContext } from '@mui/toolpad-utils/react'; | ||
import invariant from 'invariant'; | ||
import { ProjectEvents } from './types'; | ||
import { ServerDefinition } from './server/projectRpcServer'; | ||
import { createRpcApi } from './rpcClient'; | ||
|
||
async function fetchAppDevManifest(url: string) { | ||
const response = await fetch(`${url}/__toolpad_dev__/manifest.json`); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch app dev manifest: ${response.status}`); | ||
} | ||
return response.text(); | ||
} | ||
|
||
function createProject(url: string, serializedManifest: string, queryClient: QueryClient) { | ||
const manifest = JSON.parse(serializedManifest); | ||
const events = new Emitter<ProjectEvents>(); | ||
|
||
const ws = new WebSocket(manifest.wsUrl); | ||
|
||
ws.addEventListener('error', () => console.error(`Websocket failed to connect "${ws.url}"`)); | ||
|
||
ws.addEventListener('open', () => { | ||
// eslint-disable-next-line no-console | ||
console.log('Socket connected'); | ||
}); | ||
|
||
ws.addEventListener('message', (event) => { | ||
const message = JSON.parse(event.data); | ||
switch (message.kind) { | ||
case 'projectEvent': { | ||
events.emit(message.event, message.payload); | ||
break; | ||
} | ||
default: | ||
throw new Error(`Unknown message kind: ${message.kind}`); | ||
} | ||
}); | ||
|
||
const api = createRpcApi<ServerDefinition>(queryClient, `${url}/__toolpad_dev__/rpc`); | ||
|
||
const unsubExternalChange = events.subscribe('externalChange', () => { | ||
api.invalidateQueries('loadDom', []); | ||
}); | ||
|
||
const unsubFunctionsChanged = events.subscribe('functionsChanged', () => { | ||
api.invalidateQueries('introspect', []); | ||
}); | ||
|
||
const dispose = () => { | ||
ws.close(); | ||
unsubExternalChange(); | ||
unsubFunctionsChanged(); | ||
}; | ||
|
||
return { | ||
url, | ||
rootDir: manifest.rootDir, | ||
api, | ||
events, | ||
dispose, | ||
}; | ||
} | ||
|
||
type Project = Awaited<ReturnType<typeof createProject>>; | ||
|
||
const ProjectContext = React.createContext<Project | undefined>(undefined); | ||
|
||
export interface ProjectProps { | ||
url: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
export function ProjectProvider({ url, children }: ProjectProps) { | ||
const { data: manifest } = useQuery(['app-dev-manifest', url], () => fetchAppDevManifest(url), { | ||
suspense: true, | ||
}); | ||
|
||
invariant(manifest, "manifest should be defined, we're using suspense"); | ||
|
||
const queryClient = useQueryClient(); | ||
|
||
const project = React.useMemo( | ||
() => createProject(url, manifest, queryClient), | ||
[url, manifest, queryClient], | ||
); | ||
|
||
React.useEffect(() => { | ||
return () => { | ||
project.dispose(); | ||
}; | ||
}, [project]); | ||
|
||
return <ProjectContext.Provider value={project}>{children}</ProjectContext.Provider>; | ||
} | ||
|
||
export function useProject() { | ||
return useNonNullableContext(ProjectContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,6 @@ | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import * as React from 'react'; | ||
import { useNonNullableContext } from '@mui/toolpad-utils/react'; | ||
import { ApiClient, createRpcApi } from './rpcClient'; | ||
import type { ServerDefinition } from './server/projectRpcServer'; | ||
import { useProjectEvents } from './projectEvents'; | ||
|
||
const ApiContext = React.createContext<ApiClient<ServerDefinition> | null>(null); | ||
|
||
export interface ApiProviderProps { | ||
url: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
export function ProjectApiProvider({ url, children }: ApiProviderProps) { | ||
const queryClient = useQueryClient(); | ||
const projectEvents = useProjectEvents(); | ||
|
||
const api = React.useMemo( | ||
() => createRpcApi<ServerDefinition>(queryClient, url), | ||
[queryClient, url], | ||
); | ||
|
||
React.useEffect(() => { | ||
const unsubExternalChange = projectEvents.subscribe('externalChange', () => { | ||
api.invalidateQueries('loadDom', []); | ||
}); | ||
|
||
const unsubFunctionsChanged = projectEvents.subscribe('functionsChanged', () => { | ||
api.invalidateQueries('introspect', []); | ||
}); | ||
|
||
return () => { | ||
unsubExternalChange(); | ||
unsubFunctionsChanged(); | ||
}; | ||
}, [projectEvents, api]); | ||
|
||
return <ApiContext.Provider value={api}>{children}</ApiContext.Provider>; | ||
} | ||
import { useProject } from './project'; | ||
|
||
export function useProjectApi() { | ||
return useNonNullableContext(ApiContext); | ||
const project = useProject(); | ||
return project.api; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.