提交 b197ca20 authored 作者: Will Chen's avatar Will Chen

Clean up sandpack client lifecycle management

上级 3f782c92
......@@ -435,7 +435,6 @@ const SandpackIframe = ({ reloadKey }: { reloadKey: number }) => {
const selectedAppId = useAtomValue(selectedAppIdAtom);
const { app } = useLoadApp(selectedAppId);
const keyRef = useRef<number | null>(null);
const isFirstRender = useRef(true);
const sandpackClientRef = useRef<SandpackClient | null>(null);
async function loadSandpack() {
......@@ -443,9 +442,14 @@ const SandpackIframe = ({ reloadKey }: { reloadKey: number }) => {
keyRef.current = reloadKey;
if (!selectedAppId) return;
if (sandpackClientRef.current) {
sandpackClientRef.current.destroy();
sandpackClientRef.current = null;
}
const sandboxConfig = await IpcClient.getInstance().getAppSandboxConfig(
selectedAppId
);
if (!iframeRef.current || !app) return;
const sandpackConfig: SandboxSetup = mapSandpackConfig(sandboxConfig);
......@@ -487,20 +491,8 @@ const SandpackIframe = ({ reloadKey }: { reloadKey: number }) => {
}, [app]);
useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
if (!iframeRef.current || !app || !selectedAppId) return () => {};
const clientPromise = loadSandpack();
return () => {
clientPromise.then((client) => {
client?.destroy();
sandpackClientRef.current = null;
});
};
if (!iframeRef.current || !app || !selectedAppId) return;
loadSandpack();
}, [reloadKey]);
const iframeRef = useRef<HTMLIFrameElement>(null);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论