@@ -18,16 +18,42 @@ import * as wasm_module from "../../dist/pkg/perspective-js.js";
1818import wasm_binary from "../../dist/pkg/perspective-js.wasm" ;
1919import { load_wasm_stage_0 } from "@finos/perspective/src/ts/decompress.ts" ;
2020
21- const module = await load_wasm_stage_0 ( wasm_binary as unknown as ArrayBuffer ) ;
22- await wasm_module . default ( module ) ;
23- await wasm_module . init ( ) ;
21+ import type * as psp from "../../dist/pkg/perspective-js.d.ts" ;
22+
23+ type WasmElement = {
24+ __wasm_module__ : Promise < typeof psp > ;
25+ } ;
26+
27+ export async function compile_perspective ( ) {
28+ let elem = customElements . get (
29+ "perspective-viewer"
30+ ) as unknown as WasmElement ;
31+
32+ if ( ! elem ) {
33+ console . warn (
34+ "No `<perspective-viewer>` Custom Element found, using inline `Client`."
35+ ) ;
36+
37+ const module = await load_wasm_stage_0 (
38+ wasm_binary as unknown as ArrayBuffer
39+ ) ;
40+
41+ await wasm_module . default ( module ) ;
42+ await wasm_module . init ( ) ;
43+ return wasm_module ;
44+ }
45+
46+ return elem . __wasm_module__ ;
47+ }
2448
2549export async function websocket ( url : string | URL ) {
26- return await api . websocket ( Promise . resolve ( wasm_module ) , url ) ;
50+ const wasm_module = compile_perspective ( ) ;
51+ return await api . websocket ( wasm_module , url ) ;
2752}
2853
2954export async function worker ( ) {
30- return await api . worker . call ( undefined , Promise . resolve ( wasm_module ) ) ;
55+ const wasm_module = compile_perspective ( ) ;
56+ return await api . worker ( wasm_module ) ;
3157}
3258
3359export default { websocket, worker } ;
0 commit comments