You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
1 year ago
|
'use client'
|
||
|
|
||
|
import { useEffect, useRef } from "react"
|
||
1 year ago
|
import { themeSignal } from "./Appearance"
|
||
1 year ago
|
|
||
|
interface Module {
|
||
1 year ago
|
setup?: (params: any, wasmModule: WebAssembly.Instance | undefined) => Promise<undefined>
|
||
1 year ago
|
cleanup?: () => Promise<undefined>
|
||
1 year ago
|
onThemeChange?: () => void
|
||
1 year ago
|
}
|
||
|
|
||
1 year ago
|
export default function ScriptLoaderClient({ src, wasmSrc, ...rest }: { src: string, wasmSrc?: string }) {
|
||
1 year ago
|
const moduleRef = useRef<Module | null>(null)
|
||
|
|
||
|
useEffect(() => {
|
||
1 year ago
|
if (!src) return undefined;
|
||
1 year ago
|
|
||
1 year ago
|
(async () => {
|
||
|
const scriptModule = await import(/*webpackIgnore: true */ src)
|
||
1 year ago
|
|
||
1 year ago
|
let wasmModule: WebAssembly.Instance | undefined = undefined;
|
||
1 year ago
|
|
||
1 year ago
|
if (wasmSrc) {
|
||
|
const wasm = await WebAssembly.instantiateStreaming(fetch(wasmSrc))
|
||
|
wasmModule = wasm.instance
|
||
|
}
|
||
|
|
||
|
moduleRef.current = scriptModule
|
||
|
|
||
|
|
||
|
if (scriptModule.setup && typeof scriptModule.setup === 'function')
|
||
|
scriptModule.setup(rest, wasmModule)
|
||
|
})();
|
||
1 year ago
|
|
||
|
return () => {
|
||
|
const mod = moduleRef.current
|
||
|
|
||
|
if (mod?.cleanup && typeof mod.cleanup === 'function') {
|
||
|
mod.cleanup()
|
||
|
}
|
||
|
}
|
||
|
}, [])
|
||
|
|
||
1 year ago
|
useEffect(() => {
|
||
|
function onThemeChange() {
|
||
|
const mod = moduleRef.current
|
||
|
|
||
|
if (mod?.onThemeChange && typeof mod?.onThemeChange === 'function') {
|
||
|
mod.onThemeChange()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
themeSignal.addListener('change', onThemeChange)
|
||
|
return () => { themeSignal.removeListener('change', onThemeChange) }
|
||
|
}, [])
|
||
|
|
||
1 year ago
|
return null
|
||
|
}
|