-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
72 additions
and
70 deletions.
There are no files selected for viewing
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
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,16 @@ | ||
import React, { Fragment, useEffect, memo } from 'react'; | ||
|
||
import { useWebContainerStore } from '@/store/webContainerStore'; | ||
|
||
interface WebContainerProviderProps {} | ||
|
||
const WebContainerProvider: React.FC<WebContainerProviderProps> = () => { | ||
const { initWebContainer } = useWebContainerStore(); | ||
useEffect(() => { | ||
initWebContainer(); | ||
}, [initWebContainer]); // 注意这里需要将 initWebContainer 添加到依赖数组中 | ||
|
||
return <Fragment></Fragment>; | ||
}; | ||
|
||
export default memo(WebContainerProvider); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { create } from 'zustand'; | ||
import { WebContainer } from '@webcontainer/api'; | ||
|
||
import { WebContainerFileSystemTreeSavePoint, saveFileSystemTree } from '@/utils'; | ||
|
||
interface WebContainerState { | ||
webContainerInstance: WebContainer | null; | ||
isInitialized: boolean; | ||
} | ||
|
||
interface WebContainerActions { | ||
initWebContainer: () => Promise<void>; | ||
saveFileSystemOnUnload: () => void; | ||
} | ||
|
||
type WebContainerStore = WebContainerState & WebContainerActions; | ||
|
||
export const useWebContainerStore = create<WebContainerStore>((set, get) => ({ | ||
webContainerInstance: null, | ||
isInitialized: false, | ||
|
||
async initWebContainer() { | ||
const { webContainerInstance, isInitialized } = get(); | ||
|
||
if (!isInitialized && !webContainerInstance) { | ||
const newWebContainerInstance = await WebContainer.boot(); | ||
|
||
const treeData = localStorage.getItem(WebContainerFileSystemTreeSavePoint); | ||
|
||
if (treeData) { | ||
const fileSystemTree = JSON.parse(treeData); | ||
await newWebContainerInstance.mount(fileSystemTree); | ||
} | ||
|
||
set({ webContainerInstance: newWebContainerInstance, isInitialized: true }); | ||
|
||
// Add the beforeunload listener here | ||
window.addEventListener('beforeunload', get().saveFileSystemOnUnload); | ||
} | ||
}, | ||
|
||
saveFileSystemOnUnload() { | ||
const { webContainerInstance } = get(); | ||
|
||
if (webContainerInstance) { | ||
saveFileSystemTree(webContainerInstance); | ||
} | ||
|
||
// Remove the listener when the function is called | ||
window.removeEventListener('beforeunload', this.saveFileSystemOnUnload); | ||
}, | ||
})); |