@@ -31,35 +31,11 @@ import {
3131} from "@/client/components/Tooltip" ;
3232import { rpc } from "@/utils/rpc" ;
3333import { useLoaderData , type LoaderFunctionArgs } from "react-router" ;
34- import type { WorkspaceOwner } from "@/gen/types.ts" ;
35-
36- type GoPreviewDef = (
37- v : Record < string , string > ,
38- owner : WorkspaceOwner ,
39- params : Record < string , string > ,
40- ) => Promise < string > ;
41-
42- // Extend the Window object to include the Go related code that is added from
43- // wasm_exec.js and our loaded Go code.
44- declare global {
45- interface Window {
46- // Loaded from wasm
47- go_preview ?: GoPreviewDef ;
48- Go : { new ( ) : Go } ;
49- CODE ?: string ;
50- }
51- }
52-
53- declare class Go {
54- argv : string [ ] ;
55- env : { [ envKey : string ] : string } ;
56- exit : ( code : number ) => void ;
57- importObject : WebAssembly . Imports ;
58- exited : boolean ;
59- mem : DataView ;
60- run ( instance : WebAssembly . Instance ) : Promise < void > ;
61- }
34+ import { initWasm , type WasmLoadState } from "@/utils/wasm" ;
6235
36+ /**
37+ * Load the shared code if present.
38+ */
6339export const loader = async ( { params } : LoaderFunctionArgs ) => {
6440 const { id } = params ;
6541 if ( ! id ) {
@@ -79,8 +55,12 @@ export const loader = async ({ params }: LoaderFunctionArgs) => {
7955} ;
8056
8157export const App = ( ) => {
82- const $wasmState = useStore ( ( state ) => state . wasmState ) ;
83- const $setWasmState = useStore ( ( state ) => state . setWasmState ) ;
58+ const [ wasmLoadState , setWasmLoadingState ] = useState < WasmLoadState > ( ( ) => {
59+ if ( window . go_preview ) {
60+ return "loaded" ;
61+ }
62+ return "loading" ;
63+ } ) ;
8464 const $setCode = useStore ( ( store ) => store . setCode ) ;
8565 const code = useLoaderData < typeof loader > ( ) ;
8666
@@ -92,30 +72,15 @@ export const App = () => {
9272 $setCode ( code ) ;
9373 } , [ code , $setCode ] ) ;
9474
95- // biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
9675 useEffect ( ( ) => {
97- const initWasm = async ( ) => {
98- try {
99- const goWasm = new window . Go ( ) ;
100- const result = await WebAssembly . instantiateStreaming (
101- fetch (
102- import . meta. env . PROD
103- ? "/assets/build/preview.wasm"
104- : "/build/preview.wasm" ,
105- ) ,
106- goWasm . importObject ,
107- ) ;
108-
109- goWasm . run ( result . instance ) ;
110- $setWasmState ( "loaded" ) ;
111- } catch ( e ) {
112- $setWasmState ( "error" ) ;
113- console . error ( e ) ;
114- }
115- } ;
116-
117- if ( $wasmState !== "loaded" ) {
118- initWasm ( ) ;
76+ if ( ! window . go_preview ) {
77+ initWasm ( ) . then ( ( loadState ) => {
78+ setWasmLoadingState ( loadState ) ;
79+ } ) ;
80+ } else {
81+ // We assume that if `window.go_preview` has already created then the wasm
82+ // has already been initiated.
83+ setWasmLoadingState ( "loaded" ) ;
11984 }
12085 } , [ ] ) ;
12186
@@ -163,14 +128,14 @@ export const App = () => {
163128 </ div >
164129 </ nav >
165130
166- < ResizablePanelGroup aria-hidden = { ! $wasmState } direction = { "horizontal" } >
131+ < ResizablePanelGroup direction = { "horizontal" } >
167132 { /* EDITOR */ }
168133 < Editor />
169134
170135 < ResizableHandle className = "bg-surface-quaternary" />
171136
172137 { /* PREVIEW */ }
173- < Preview />
138+ < Preview wasmLoadState = { wasmLoadState } />
174139 </ ResizablePanelGroup >
175140 </ main >
176141 ) ;
0 commit comments