@perspective-dev/client 5.4.0 → 5.5.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.
@@ -5,7 +5,7 @@ import type * as psp from "../../../dist/wasm/perspective-js.d.ts";
5
5
  * @param module
6
6
  * @returns
7
7
  */
8
- export declare function worker(module: Promise<typeof psp>, server_wasm: Promise<WebAssembly.Module> | undefined, perspective_wasm_worker: Promise<SharedWorker | ServiceWorker | Worker | MessagePort>): Promise<psp.Client>;
8
+ export declare function worker(module: Promise<typeof psp>, server_wasm: Promise<WebAssembly.Module> | undefined, perspective_wasm_worker: Promise<SharedWorker | Worker | MessagePort>): Promise<psp.Client>;
9
9
  /**
10
10
  * Create a new client connected via WebSocket to a server implemnting the
11
11
  * Perspective Protocol.
@@ -2127,7 +2127,7 @@ function __wbg_get_imports() {
2127
2127
  const ret = Promise.resolve(getObject(arg0));
2128
2128
  return addHeapObject(ret);
2129
2129
  },
2130
- __wbg_run_a7d22113de2ff7d8: function() { return handleError(function (arg0, arg1) {
2130
+ __wbg_run_26ff8525c6787d9c: function() { return handleError(function (arg0, arg1) {
2131
2131
  const ret = getObject(arg0).run(getObject(arg1));
2132
2132
  return addHeapObject(ret);
2133
2133
  }, arguments); },
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perspective-dev/client",
3
- "version": "5.4.0",
3
+ "version": "5.5.0",
4
4
  "description": "Client for Perspective, a high-performance WASM engine with reactive Tables, Views, and joins.",
5
5
  "keywords": [
6
6
  "perspective",
@@ -66,7 +66,7 @@
66
66
  "pro_self_extracting_wasm": "0.0.9",
67
67
  "stoppable": "=1.1.0",
68
68
  "ws": "^8.17.0",
69
- "@perspective-dev/server": "^5.4.0"
69
+ "@perspective-dev/server": "^5.5.0"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@clickhouse/client-web": "^1.12.0",
@@ -84,9 +84,9 @@
84
84
  "typescript": ">=5 <6",
85
85
  "web-worker": "1.4.1",
86
86
  "zx": ">=8 <9",
87
- "@perspective-dev/esbuild-plugin": "^5.4.0",
88
- "@perspective-dev/test": "^5.4.0",
89
- "@perspective-dev/metadata": "^5.4.0"
87
+ "@perspective-dev/esbuild-plugin": "^5.5.0",
88
+ "@perspective-dev/metadata": "^5.5.0",
89
+ "@perspective-dev/test": "^5.5.0"
90
90
  },
91
91
  "scripts": {
92
92
  "build": "node ./build.mjs",
@@ -108,6 +108,14 @@ where
108
108
  pub fn new_throttled<U: Future<Output = ApiResult<T>> + 'static>(x: U) -> ApiFuture<()> {
109
109
  ApiFuture::<()>::new(async move { x.await.ignore_view_delete().map(|_| ()) })
110
110
  }
111
+
112
+ pub fn new_pure(t: T) -> ApiFuture<T> {
113
+ ApiFuture::<T>::new(async move { Ok(t) })
114
+ }
115
+
116
+ pub fn new_err<E: Into<ApiError> + 'static>(err: E) -> ApiFuture<T> {
117
+ ApiFuture::<T>::new(async move { Err(err.into()) })
118
+ }
111
119
  }
112
120
 
113
121
  impl<T> ApiFuture<T>
@@ -56,6 +56,16 @@ function bindPortSharedWorker(msg: MessageEvent) {
56
56
  port.start();
57
57
  }
58
58
 
59
+ /**
60
+ * Provided by `@perspective-dev/esbuild-plugin/worker`, which replaces this
61
+ * module with a stub when it is imported (rather than built as an entry
62
+ * point). Constructs a `Worker` from the bundled source, or falls back to
63
+ * running it on the main thread and returns a `MessagePort` to it.
64
+ */
65
+ export declare function initialize(
66
+ opts?: WorkerOptions,
67
+ ): Promise<Worker | MessagePort>;
68
+
59
69
  // @ts-expect-error wrong scope
60
70
  self.addEventListener("connect", bindPortSharedWorker);
61
71
  self.addEventListener(
@@ -235,10 +235,6 @@ async function compilerize(
235
235
  disable_stage_0: boolean = false,
236
236
  ) {
237
237
  const wasm_buff = disable_stage_0 ? wasm : await load_wasm_stage_0(wasm);
238
- // Compile to a `WebAssembly.Module` once so it can be both instantiated
239
- // locally and forwarded to other workers via `getCompiledClientWasm()`.
240
- // `WebAssembly.Module` is structured-cloneable across workers, so the
241
- // recipient can instantiate without re-fetching or re-compiling.
242
238
  const compiled = await compile_module(wasm_buff);
243
239
  GLOBAL_CLIENT_MODULE = Promise.resolve(compiled);
244
240
  await wasm_module.default({ module_or_path: compiled });
@@ -342,24 +338,10 @@ function get_server() {
342
338
  let GLOBAL_WORKER: undefined | (() => Promise<Worker>) = undefined;
343
339
 
344
340
  // `WorkerPlugin` resolves this import to a stub that exports
345
- // `getPerspectiveWorkerURL(): Promise<string>`. The URL is either a
346
- // Blob URL (inline mode — production builds) or a real file path
347
- // resolved against `import.meta.url` (file mode — debug builds).
348
- // Constructing the `Worker` lives here in the consumer rather than
349
- // inside the plugin so the same module text can also be loaded
350
- // in-process via dynamic `import(url)` when a future caller wants
351
- // it; the plugin no longer owns Worker lifecycle.
352
- //
353
- // `initialize()` constructs `new Worker(blobUrl)` and falls back to
354
- // running the worker source on the main thread via `new Function(...)`
355
- // when Worker construction is unavailable (e.g. `file://` origins where
356
- // module-Worker support is gated). The shim it returns is
357
- // MessagePort-shaped so downstream code can treat it like a real
358
- // Worker.
359
- // @ts-ignore — resolved at build time by `@perspective-dev/esbuild-plugin/worker`
341
+ // `getPerspectiveWorkerURL(): Promise<string>`.
360
342
  import { initialize as initializePerspectiveWorker } from "../../src/ts/perspective-server.worker.js";
361
343
 
362
- async function get_worker(): Promise<Worker> {
344
+ async function get_worker(): Promise<Worker | MessagePort> {
363
345
  if (GLOBAL_WORKER === undefined) {
364
346
  return await initializePerspectiveWorker({
365
347
  type: "module",
@@ -375,7 +357,7 @@ export async function websocket(url: string | URL) {
375
357
  }
376
358
 
377
359
  export async function worker(
378
- worker?: Promise<SharedWorker | ServiceWorker | Worker | MessagePort>,
360
+ worker?: Promise<SharedWorker | Worker | MessagePort>,
379
361
  ) {
380
362
  if (typeof worker === "undefined") {
381
363
  worker = get_worker();
@@ -70,25 +70,31 @@ async function _init(
70
70
  export async function worker(
71
71
  module: Promise<typeof psp>,
72
72
  server_wasm: Promise<WebAssembly.Module> | undefined,
73
- perspective_wasm_worker: Promise<
74
- SharedWorker | ServiceWorker | Worker | MessagePort
75
- >,
73
+ perspective_wasm_worker: Promise<SharedWorker | Worker | MessagePort>,
76
74
  ) {
77
- let [wasm, webworker]: [
75
+ const [wasm, webworker]: [
78
76
  WebAssembly.Module | undefined,
79
- SharedWorker | ServiceWorker | Worker | MessagePort,
77
+ SharedWorker | Worker | MessagePort,
80
78
  ] = await Promise.all([server_wasm, perspective_wasm_worker]);
81
79
 
82
80
  const { Client } = await module;
83
- let port: MessagePort;
81
+ let port: MessagePort | Worker;
82
+ let close: () => void;
84
83
  if (
85
84
  typeof SharedWorker !== "undefined" &&
86
85
  webworker instanceof SharedWorker
87
86
  ) {
88
- port = webworker.port;
87
+ const shared_port = webworker.port;
88
+ port = shared_port;
89
+ close = () => shared_port.close();
90
+ } else if (typeof Worker !== "undefined" && webworker instanceof Worker) {
91
+ port = webworker;
92
+ close = () => webworker.terminate();
93
+ } else if (webworker instanceof MessagePort) {
94
+ port = webworker;
95
+ close = () => webworker.close();
89
96
  } else {
90
- // Assume `MessagePort`
91
- port = webworker as MessagePort;
97
+ throw new Error("Unsupported worker type");
92
98
  }
93
99
 
94
100
  const client = new Client(
@@ -98,17 +104,14 @@ export async function worker(
98
104
  },
99
105
  async () => {
100
106
  console.debug("Closing WebWorker");
101
- port.close();
102
- if (webworker instanceof Worker) {
103
- webworker.terminate();
104
- }
107
+ close();
105
108
  },
106
109
  );
107
110
 
108
111
  await _init(port, wasm);
109
- port.addEventListener("message", (json: MessageEvent<Uint8Array>) => {
112
+ port.onmessage = (json: MessageEvent<Uint8Array>) => {
110
113
  client.handle_response(json.data);
111
- });
114
+ };
112
115
 
113
116
  return client;
114
117
  }