egui-workbench 0.0.0-stage → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,6 @@
1
- # Temporary Holding Version
1
+ # egui-workbench wasm
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The WebAssembly host for `egui-workbench`. `wasm-pack build --target web` produces the
4
+ ESM module and the wasm binary; `js/index.js` is the npm wrapper with `createWorkbench`
5
+ and `connectRemote`. TypeScript types for the versioned spec are generated from the host
6
+ JSON Schema by `scripts/generate-types.mjs`.
package/js/index.js ADDED
@@ -0,0 +1,112 @@
1
+ // ESM wrapper for the wasm host. The generated wasm-bindgen module lives in `../pkg`.
2
+ import init, { WebWorkbench } from "../pkg/egui_workbench_wasm.js";
3
+
4
+ let moduleReady;
5
+
6
+ async function load(wasmUrl) {
7
+ if (!moduleReady) {
8
+ moduleReady = init(wasmUrl);
9
+ }
10
+ return moduleReady;
11
+ }
12
+
13
+ function specText(spec) {
14
+ return typeof spec === "string" ? spec : JSON.stringify(spec);
15
+ }
16
+
17
+ /**
18
+ * Create a workbench on a canvas.
19
+ * @param {{ spec: object | string, canvas: HTMLCanvasElement, wasmUrl?: string }} options
20
+ */
21
+ export async function createWorkbench({ spec, canvas, wasmUrl }) {
22
+ await load(wasmUrl);
23
+ const native = new WebWorkbench(specText(spec));
24
+ await native.start(canvas);
25
+ return {
26
+ native,
27
+ applyPatch(patch) {
28
+ native.apply_patch(JSON.stringify(patch));
29
+ },
30
+ setSpec(next) {
31
+ native.set_spec(specText(next));
32
+ },
33
+ spec() {
34
+ return JSON.parse(native.spec_json());
35
+ },
36
+ pollEvents() {
37
+ return JSON.parse(native.poll_events_json());
38
+ },
39
+ setTheme(id) {
40
+ native.set_theme(id);
41
+ },
42
+ schema() {
43
+ return JSON.parse(WebWorkbench.schema());
44
+ },
45
+ destroy() {
46
+ native.destroy();
47
+ },
48
+ };
49
+ }
50
+
51
+ function encode(message) {
52
+ const json = new TextEncoder().encode(JSON.stringify(message));
53
+ const bytes = new Uint8Array(4 + json.length);
54
+ new DataView(bytes.buffer).setUint32(0, json.length);
55
+ bytes.set(json, 4);
56
+ return bytes.buffer;
57
+ }
58
+
59
+ function decode(buffer) {
60
+ const bytes = new Uint8Array(buffer);
61
+ const length = new DataView(buffer).getUint32(0);
62
+ return JSON.parse(new TextDecoder().decode(bytes.subarray(4, 4 + length)));
63
+ }
64
+
65
+ /**
66
+ * Render a host running on a server. The browser is the renderer; commands and patches
67
+ * travel as the length-prefixed 0.11 messages inside WebSocket binary frames.
68
+ * @param {{ url: string, canvas: HTMLCanvasElement, spec: object | string, wasmUrl?: string, onEvent?: (event: object) => void }} options
69
+ */
70
+ export async function connectRemote({ url, canvas, spec, wasmUrl, onEvent }) {
71
+ const workbench = await createWorkbench({ spec, canvas, wasmUrl });
72
+ const socket = new WebSocket(url);
73
+ socket.binaryType = "arraybuffer";
74
+ let nextId = 0;
75
+ const send = (request) => socket.send(encode({ type: "request", id: ++nextId, request }));
76
+
77
+ socket.addEventListener("open", () => send({ op: "get_spec" }));
78
+ socket.addEventListener("message", (message) => {
79
+ const decoded = decode(message.data);
80
+ if (decoded.type === "response" && decoded.response.result === "spec") {
81
+ workbench.setSpec(decoded.response.spec);
82
+ } else if (decoded.type === "event") {
83
+ onEvent?.(decoded.event);
84
+ }
85
+ });
86
+ const timer = setInterval(() => {
87
+ for (const event of workbench.pollEvents()) {
88
+ if (event.event === "command") {
89
+ send({ op: "run_command", id: event.id });
90
+ }
91
+ onEvent?.(event);
92
+ }
93
+ }, 100);
94
+
95
+ return {
96
+ workbench,
97
+ applyPatch(patch) {
98
+ send({ op: "apply_patch", patch });
99
+ },
100
+ sendCommand(id) {
101
+ send({ op: "run_command", id });
102
+ },
103
+ close() {
104
+ clearInterval(timer);
105
+ socket.close();
106
+ workbench.destroy();
107
+ },
108
+ };
109
+ }
110
+
111
+ export { WebWorkbench };
112
+ export default createWorkbench;
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "egui-workbench",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.17.0",
4
+ "description": "The egui-workbench host in WebAssembly: a spec-driven canvas and a remote UI client.",
5
+ "type": "module",
6
+ "main": "./js/index.js",
7
+ "types": "./types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/index.d.ts",
11
+ "import": "./js/index.js"
12
+ },
13
+ "./schema": {
14
+ "types": "./types/schema.d.ts"
15
+ },
16
+ "./pkg/*": "./pkg/*"
17
+ },
18
+ "files": ["js", "pkg", "types", "README.md"],
19
+ "sideEffects": false,
20
+ "scripts": {
21
+ "build": "wasm-pack build --target web --out-dir pkg --out-name egui_workbench_wasm && node scripts/generate-types.mjs && node scripts/clean-pkg.mjs",
22
+ "types": "node scripts/generate-types.mjs",
23
+ "types:check": "node scripts/generate-types.mjs --check"
24
+ },
25
+ "keywords": ["egui", "wasm", "workbench", "ui", "plots"],
26
+ "license": "MIT OR Apache-2.0",
27
+ "repository": "https://github.com/jigonzalez930209/egui-workbench"
28
+ }
@@ -0,0 +1,103 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * A native spec rendered in a browser canvas.
6
+ */
7
+ export class WebWorkbench {
8
+ free(): void;
9
+ [Symbol.dispose](): void;
10
+ /**
11
+ * Apply a JSON-pointer patch to the spec.
12
+ */
13
+ apply_patch(patch_json: string): void;
14
+ /**
15
+ * Stop eframe and release its listeners.
16
+ */
17
+ destroy(): void;
18
+ /**
19
+ * Create a workbench from a versioned `HostSpec` JSON document.
20
+ */
21
+ constructor(spec_json: string);
22
+ /**
23
+ * Drain host events as a JSON array.
24
+ */
25
+ poll_events_json(): string;
26
+ /**
27
+ * The `HostSpec` JSON Schema.
28
+ */
29
+ static schema(): string;
30
+ /**
31
+ * Replace the spec.
32
+ */
33
+ set_spec(spec_json: string): void;
34
+ /**
35
+ * Change the theme id without rebuilding the window.
36
+ */
37
+ set_theme(id: string): void;
38
+ /**
39
+ * The current spec as JSON, including queued actions that were applied.
40
+ */
41
+ spec_json(): string;
42
+ /**
43
+ * Mount the workbench on a canvas. Call once.
44
+ */
45
+ start(canvas: HTMLCanvasElement): Promise<void>;
46
+ /**
47
+ * The host crate version.
48
+ */
49
+ static version(): string;
50
+ }
51
+
52
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
53
+
54
+ export interface InitOutput {
55
+ readonly memory: WebAssembly.Memory;
56
+ readonly __wbg_webworkbench_free: (a: number, b: number) => void;
57
+ readonly webworkbench_apply_patch: (a: number, b: number, c: number) => [number, number];
58
+ readonly webworkbench_destroy: (a: number) => void;
59
+ readonly webworkbench_new: (a: number, b: number) => [number, number, number];
60
+ readonly webworkbench_poll_events_json: (a: number) => [number, number];
61
+ readonly webworkbench_schema: () => [number, number];
62
+ readonly webworkbench_set_spec: (a: number, b: number, c: number) => [number, number];
63
+ readonly webworkbench_set_theme: (a: number, b: number, c: number) => void;
64
+ readonly webworkbench_spec_json: (a: number) => [number, number];
65
+ readonly webworkbench_start: (a: number, b: any) => any;
66
+ readonly webworkbench_version: () => [number, number];
67
+ readonly wasm_bindgen_e8fadb86c21d1ac6___convert__closures_____invoke___js_sys_4d64f92870bcf95f___Function_fn_wasm_bindgen_e8fadb86c21d1ac6___JsValue_____wasm_bindgen_e8fadb86c21d1ac6___sys__Undefined___js_sys_4d64f92870bcf95f___Function_fn_wasm_bindgen_e8fadb86c21d1ac6___JsValue_____wasm_bindgen_e8fadb86c21d1ac6___sys__Undefined_______true_: (a: number, b: number, c: any, d: any) => void;
68
+ readonly wasm_bindgen_e8fadb86c21d1ac6___convert__closures_____invoke___wasm_bindgen_e8fadb86c21d1ac6___JsValue__core_9b3796e30d99ddb7___result__Result_____wasm_bindgen_e8fadb86c21d1ac6___JsError___true_: (a: number, b: number, c: any) => [number, number];
69
+ readonly wasm_bindgen_e8fadb86c21d1ac6___convert__closures_____invoke___core_9b3796e30d99ddb7___result__Result_____wasm_bindgen_e8fadb86c21d1ac6___JsValue___true_: (a: number, b: number) => [number, number];
70
+ readonly wasm_bindgen_e8fadb86c21d1ac6___convert__closures_____invoke___js_sys_4d64f92870bcf95f___Array______true_: (a: number, b: number, c: any) => void;
71
+ readonly wasm_bindgen_e8fadb86c21d1ac6___convert__closures_____invoke___js_sys_4d64f92870bcf95f___Array______true__13: (a: number, b: number, c: any) => void;
72
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
73
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
74
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
75
+ readonly __externref_table_alloc: () => number;
76
+ readonly __wbindgen_externrefs: WebAssembly.Table;
77
+ readonly __wbindgen_exn_store: (a: number) => void;
78
+ readonly __wbindgen_destroy_closure: (a: number, b: number) => void;
79
+ readonly __externref_table_dealloc: (a: number) => void;
80
+ readonly __wbindgen_start: () => void;
81
+ }
82
+
83
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
84
+
85
+ /**
86
+ * Instantiates the given `module`, which can either be bytes or
87
+ * a precompiled `WebAssembly.Module`.
88
+ *
89
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
90
+ *
91
+ * @returns {InitOutput}
92
+ */
93
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
94
+
95
+ /**
96
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
97
+ * for everything else, calls `WebAssembly.instantiate` directly.
98
+ *
99
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
100
+ *
101
+ * @returns {Promise<InitOutput>}
102
+ */
103
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;