Skip to content

Commit 9631d2e

Browse files
committed
Fix inline builds
Signed-off-by: Andrew Stein <steinlink@gmail.com>
1 parent d11cbda commit 9631d2e

6 files changed

Lines changed: 93 additions & 6 deletions

File tree

rust/perspective-js/src/ts/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ export async function websocket(
8888
module: Promise<typeof psp>,
8989
url: string | URL
9090
) {
91-
const { Client } = await module;
9291
const ws = new WebSocket(url);
9392
let [sender, receiver] = invert_promise();
9493
ws.onopen = sender;
9594
ws.binaryType = "arraybuffer";
9695
await receiver;
96+
const { Client } = await module;
9797
const client = new Client(
9898
(proto: Uint8Array) => {
9999
const buffer = proto.slice().buffer;

rust/perspective-js/src/ts/perspective.inline.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,42 @@ import * as wasm_module from "../../dist/pkg/perspective-js.js";
1818
import wasm_binary from "../../dist/pkg/perspective-js.wasm";
1919
import { 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

2549
export 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

2954
export 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

3359
export default { websocket, worker };

rust/perspective-viewer/build.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ async function build_all() {
4040

4141
// JavaScript
4242
const BUILD = [
43+
{
44+
entryPoints: ["src/ts/perspective-viewer.ts"],
45+
format: "esm",
46+
plugins: [
47+
PerspectiveEsbuildPlugin({
48+
wasm: { inline: true },
49+
}),
50+
],
51+
outfile: "dist/esm/perspective-viewer.inline.js",
52+
},
4353
{
4454
entryPoints: ["src/ts/perspective-viewer.ts"],
4555
format: "esm",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script type="module">
5+
import "/node_modules/@finos/perspective-viewer/dist/esm/perspective-viewer.inline.js";
6+
import perspective from "/node_modules/@finos/perspective/dist/esm/perspective.inline.js";
7+
8+
async function load() {
9+
let resp = await fetch("/node_modules/@finos/perspective-test/assets/superstore.csv");
10+
let csv = await resp.text();
11+
const viewer = document.querySelector("perspective-viewer");
12+
const worker = await perspective.worker();
13+
const table = worker.table(csv, { index: "Row ID" });
14+
await viewer.load(table);
15+
window.__TEST_WORKER__ = worker;
16+
}
17+
18+
await load();
19+
window.__TEST_PERSPECTIVE_READY__ = true;
20+
</script>
21+
<link rel="stylesheet" href="../css/demo.css" />
22+
<link rel="stylesheet" href="/node_modules/@finos/perspective-viewer/dist/css/pro.css" />
23+
<link rel="stylesheet" href="/node_modules/@fontsource/roboto-mono/400.css" />
24+
</head>
25+
<body>
26+
<perspective-viewer></perspective-viewer>
27+
</body>
28+
</html>

rust/perspective-viewer/test/js/superstore.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ test.describe("Superstore", () => {
2727
await page.goto(
2828
"/node_modules/@finos/perspective-viewer/test/html/superstore.html"
2929
);
30+
3031
await page.evaluate(async () => {
3132
while (!window["__TEST_PERSPECTIVE_READY__"]) {
3233
await new Promise((x) => setTimeout(x, 10));
@@ -42,3 +43,25 @@ test.describe("Superstore", () => {
4243

4344
run_standard_tests("superstore", get_contents);
4445
});
46+
47+
test.describe("Superstore inline", () => {
48+
test.beforeEach(async function init({ page }) {
49+
await page.goto(
50+
"/node_modules/@finos/perspective-viewer/test/html/superstore-inline.html"
51+
);
52+
53+
await page.evaluate(async () => {
54+
while (!window["__TEST_PERSPECTIVE_READY__"]) {
55+
await new Promise((x) => setTimeout(x, 10));
56+
}
57+
});
58+
59+
await page.evaluate(async () => {
60+
await document.querySelector("perspective-viewer").restore({
61+
plugin: "Debug",
62+
});
63+
});
64+
});
65+
66+
run_standard_tests("superstore inline", get_contents);
67+
});
12.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)