You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As part of the ffmpeg demo (#361), I introduced a bunch of utilities for making wasmer/sdk easier to use inside a React application.
hooks.tsx
importReact,{useContext,useEffect,useState}from"react";import{Command,Runtime,Wasmer,init,initializeLogger}from"@wasmer/sdk";exporttypeWasmerSdkState={state: "loading"}|{state: "loaded"}|{state: "error",error: any};exporttypeWasmerSdkProps={/** * The filter passed to {@link initializeLogger}. */log?: string,wasm?: Parameters<typeofinit>[0],children: React.ReactElement,}constContext=React.createContext<WasmerSdkState|null>(null);// Note: The wasm-bindgen glue code only needs to be initialized once, and// initializing the logger multiple times will throw an exception, so we use a// global variable to keep track of the in-progress initialization.letpending: Promise<void>|undefined=undefined;/** * A wrapper component which will automatically initialize the Wasmer SDK. */exportfunctionWasmerSdk(props?: WasmerSdkProps){const[state,setState]=useState<WasmerSdkState>();useEffect(()=>{if(typeofpending=="undefined"){pending=init(props?.wasm).then(()=>initializeLogger(props?.log));}pending.then(()=>setState({state: "loaded"})).catch(e=>setState({state: "error",error: e}));},[])return(<Context.Providervalue={state||{state: "loading"}}>{props?.children}</Context.Provider>)}exportfunctionuseWasmerSdk(): WasmerSdkState{constctx=useContext(Context);if(ctx==null){thrownewError("Attempting to use the Wasmer SDK outside of a <WasmerSDK /> component");}returnctx;}typeLoadingPackageState={state: "loading-package"}|{state: "loaded",pkg: Wasmer,commands: Record<string,Command>,entrypoint?: Command,}|{state: "error",error: any};exporttypeUseWasmerPackageState=|{state: "loading-sdk"}|{state: "sdk-error",error: any}|LoadingPackageState;exportfunctionuseWasmerPackage(pkg: string|Uint8Array,runtime?: Runtime): UseWasmerPackageState{constsdk=useWasmerSdk();const[state,setState]=useState<LoadingPackageState>();// We can't do anything until the SDK has been loadedswitch(sdk.state){case"error":
return{state: "sdk-error",error: sdk.error};case"loading":
return{state: "loading-sdk"};}if(typeofstate!="undefined"){returnstate;}constnewState={state: "loading-package"}asconst;setState(newState);constpending=(typeofpkg=="string")
? Wasmer.fromRegistry(pkg,runtime)
: Wasmer.fromFile(pkg,runtime);pending.then(pkg=>{setState({state: "loaded", pkg,commands: pkg.commands,entrypoint: pkg.entrypoint});}).catch(error=>setState({state: "error", error }));returnnewState;}
If people want to initialize the JavaScript SDK, they can wrap their app in a <WasmerSdk> tag then use useWasmerSdk() to monitor the loading state or useWasmerPackage() to load a package.
We should extract this into its own @wasmer/react-sdk package so other people can use it.
The <WasmerSdk/> component will load the SDK in the background.
The associated useWasmerSdk() hook gives you a way to check the state of the loading and access the functionality it provides. You can also use the useWasmerPackage() hook to load a package in the background.
As part of the ffmpeg demo (#361), I introduced a bunch of utilities for making wasmer/sdk easier to use inside a React application.
hooks.tsx
If people want to initialize the JavaScript SDK, they can wrap their app in a
<WasmerSdk>
tag then useuseWasmerSdk()
to monitor the loading state oruseWasmerPackage()
to load a package.We should extract this into its own
@wasmer/react-sdk
package so other people can use it.SDK-55
The text was updated successfully, but these errors were encountered: