perspective_js/client.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13use std::error::Error;
14use std::future::Future;
15use std::sync::Arc;
16
17use derivative::Derivative;
18use futures::channel::oneshot;
19use js_sys::{Function, Uint8Array};
20#[cfg(doc)]
21use perspective_client::SystemInfo;
22use perspective_client::{
23 ClientError, ReconnectCallback, Session, TableData, TableInitOptions, TableRef, asyncfn,
24};
25use wasm_bindgen::prelude::*;
26use wasm_bindgen_derive::TryFromJsValue;
27use wasm_bindgen_futures::{JsFuture, future_to_promise};
28
29pub use crate::table::*;
30use crate::utils::{ApiError, ApiResult, JsValueSerdeExt, LocalPollLoop};
31use crate::{TableDataExt, apierror};
32
33#[wasm_bindgen]
34extern "C" {
35 #[derive(Clone)]
36 #[wasm_bindgen(typescript_type = "TableInitOptions")]
37 pub type JsTableInitOptions;
38
39 #[derive(Clone)]
40 #[wasm_bindgen(typescript_type = "JoinOptions")]
41 pub type JsJoinOptions;
42}
43
44async fn js_to_table_ref(val: &JsValue) -> ApiResult<TableRef> {
45 if let Some(name) = val.as_string() {
46 Ok(TableRef::from(name))
47 } else {
48 let get_name = js_sys::Reflect::get(val, &wasm_bindgen::intern("get_name").into())
49 .map_err(|_| apierror!(TableRefError))?
50 .dyn_into::<js_sys::Function>()
51 .map_err(|_| apierror!(TableRefError))?;
52
53 let promise = get_name
54 .call0(val)
55 .map_err(|_| apierror!(TableRefError))?
56 .dyn_into::<js_sys::Promise>()
57 .map_err(|_| apierror!(TableRefError))?;
58
59 let name = wasm_bindgen_futures::JsFuture::from(promise)
60 .await
61 .map_err(|_| apierror!(TableRefError))?
62 .as_string()
63 .ok_or_else(|| apierror!(TableRefError))?;
64
65 Ok(TableRef::from(name))
66 }
67}
68
69#[wasm_bindgen]
70#[derive(Clone)]
71pub struct ProxySession(perspective_client::ProxySession);
72
73#[wasm_bindgen]
74impl ProxySession {
75 #[wasm_bindgen(constructor)]
76 pub fn new(client: &Client, on_response: &Function) -> Self {
77 let poll_loop = LocalPollLoop::new({
78 let on_response = on_response.clone();
79 move |msg: Vec<u8>| {
80 let msg = Uint8Array::from(&msg[..]);
81 on_response.call1(&JsValue::UNDEFINED, &JsValue::from(msg))?;
82 Ok(JsValue::null())
83 }
84 });
85 // NB: This swallows any errors raised by the inner callback
86 let on_response = Box::new(move |msg: &[u8]| {
87 wasm_bindgen_futures::spawn_local(poll_loop.poll(msg.to_vec()));
88 Ok(())
89 });
90 Self(perspective_client::ProxySession::new(
91 client.client.clone(),
92 on_response,
93 ))
94 }
95
96 #[wasm_bindgen]
97 pub async fn handle_request(&self, value: JsValue) -> ApiResult<()> {
98 let uint8array = Uint8Array::new(&value);
99 let slice = uint8array.to_vec();
100 self.0.handle_request(&slice).await?;
101 Ok(())
102 }
103
104 pub async fn close(self) {
105 self.0.close().await;
106 }
107}
108
109/// An instance of a [`Client`] is a connection to a single
110/// `perspective_server::Server`, whether locally in-memory or remote over some
111/// transport like a WebSocket.
112///
113/// The browser and node.js libraries both support the `websocket(url)`
114/// constructor, which connects to a remote `perspective_server::Server`
115/// instance over a WebSocket transport.
116///
117/// In the browser, the `worker()` constructor creates a new Web Worker
118/// `perspective_server::Server` and returns a [`Client`] connected to it.
119///
120/// In node.js, a pre-instantied [`Client`] connected synhronously to a global
121/// singleton `perspective_server::Server` is the default module export.
122///
123/// # JavaScript Examples
124///
125/// Create a Web Worker `perspective_server::Server` in the browser and return a
126/// [`Client`] instance connected for it:
127///
128/// ```javascript
129/// import perspective from "@perspective-dev/client";
130/// const client = await perspective.worker();
131/// ```
132///
133/// Create a WebSocket connection to a remote `perspective_server::Server`:
134///
135/// ```javascript
136/// import perspective from "@perspective-dev/client";
137/// const client = await perspective.websocket("ws://locahost:8080/ws");
138/// ```
139///
140/// Access the synchronous client in node.js:
141///
142/// ```javascript
143/// import { default as client } from "@perspective-dev/client";
144/// ```
145#[wasm_bindgen]
146#[derive(TryFromJsValue, Clone)]
147pub struct Client {
148 pub(crate) client: perspective_client::Client,
149 pub(crate) close: Option<Function>,
150}
151
152impl From<perspective_client::Client> for Client {
153 fn from(client: perspective_client::Client) -> Self {
154 Client {
155 client,
156 close: None,
157 }
158 }
159}
160
161impl PartialEq for Client {
162 fn eq(&self, other: &Self) -> bool {
163 self.client.get_name() == other.client.get_name()
164 }
165}
166
167/// A wrapper around [`js_sys::Function`] to ease async integration for the
168/// `reconnect` argument of [`Client::on_error`] callback.
169#[derive(Derivative)]
170#[derivative(Clone(bound = ""))]
171struct JsReconnect<I>(Arc<dyn Fn(I) -> js_sys::Promise>);
172
173// This type is not thread safe, but the JavaScript environment does not allow
174// threading.
175unsafe impl<I> Send for JsReconnect<I> {}
176unsafe impl<I> Sync for JsReconnect<I> {}
177
178impl<I> JsReconnect<I> {
179 fn run(&self, args: I) -> js_sys::Promise {
180 self.0(args)
181 }
182
183 fn run_all(
184 &self,
185 args: I,
186 ) -> impl Future<Output = Result<(), Box<dyn Error + Send + Sync + 'static>>>
187 + Send
188 + Sync
189 + 'static
190 + use<I> {
191 let (sender, receiver) = oneshot::channel::<Result<(), Box<dyn Error + Send + Sync>>>();
192 let p = self.0(args);
193 let _ = future_to_promise(async move {
194 let result = JsFuture::from(p)
195 .await
196 .map(|_| ())
197 .map_err(|x| format!("{x:?}").into());
198
199 sender.send(result).unwrap();
200 Ok(JsValue::UNDEFINED)
201 });
202
203 async move { receiver.await.unwrap() }
204 }
205}
206
207impl<F, I> From<F> for JsReconnect<I>
208where
209 F: Fn(I) -> js_sys::Promise + 'static,
210{
211 fn from(value: F) -> Self {
212 JsReconnect(Arc::new(value))
213 }
214}
215
216impl Client {
217 pub fn get_client(&self) -> &'_ perspective_client::Client {
218 &self.client
219 }
220}
221
222#[wasm_bindgen]
223impl Client {
224 #[wasm_bindgen(constructor)]
225 pub fn new(send_request: Function, close: Option<Function>) -> ApiResult<Self> {
226 let send_request = JsReconnect::from(move |mut v: Vec<u8>| {
227 let buff2 = unsafe { js_sys::Uint8Array::view_mut_raw(v.as_mut_ptr(), v.len()) };
228 send_request
229 .call1(&JsValue::UNDEFINED, &buff2)
230 .unwrap()
231 .unchecked_into::<js_sys::Promise>()
232 });
233
234 let client = perspective_client::Client::new_with_callback(None, move |msg| {
235 send_request.run_all(msg)
236 })?;
237
238 Ok(Client { close, client })
239 }
240
241 #[wasm_bindgen]
242 pub fn new_proxy_session(&self, on_response: &Function) -> ProxySession {
243 ProxySession::new(self, on_response)
244 }
245
246 #[wasm_bindgen]
247 pub async fn handle_response(&self, value: &JsValue) -> ApiResult<()> {
248 let uint8array = Uint8Array::new(value);
249 let slice = uint8array.to_vec();
250 self.client.handle_response(&slice).await?;
251 Ok(())
252 }
253
254 #[wasm_bindgen]
255 pub async fn handle_error(&self, error: String, reconnect: Option<Function>) -> ApiResult<()> {
256 self.client
257 .handle_error(
258 ClientError::Unknown(error),
259 reconnect.map(|reconnect| {
260 let reconnect =
261 JsReconnect::from(move |()| match reconnect.call0(&JsValue::UNDEFINED) {
262 Ok(x) => x.unchecked_into::<js_sys::Promise>(),
263 Err(e) => {
264 // This error may occur when _invoking_ the
265 // function
266 tracing::warn!("{:?}", e);
267 js_sys::Promise::reject(&format!("C {e:?}").into())
268 },
269 });
270
271 asyncfn!(reconnect, async move || {
272 if let Err(e) = JsFuture::from(reconnect.run(())).await {
273 if let Some(e) = e.dyn_ref::<js_sys::Object>() {
274 Err(ClientError::Unknown(e.to_string().as_string().unwrap()))
275 } else {
276 Err(ClientError::Unknown(e.as_string().unwrap()))
277 }
278 } else {
279 Ok(())
280 }
281 })
282 }),
283 )
284 .await?;
285
286 Ok(())
287 }
288
289 #[wasm_bindgen]
290 pub async fn on_error(&self, callback: Function) -> ApiResult<u32> {
291 let callback = JsReconnect::from(
292 move |(message, reconnect): (ClientError, Option<ReconnectCallback>)| {
293 let cl: Closure<dyn Fn() -> js_sys::Promise> = Closure::new(move || {
294 let reconnect = reconnect.clone();
295 future_to_promise(async move {
296 if let Some(f) = reconnect {
297 (*f)().await.map_err(|e| JsValue::from(format!("A {e}")))?;
298 }
299
300 Ok(JsValue::UNDEFINED)
301 })
302 });
303
304 if let Err(e) = callback.call2(
305 &JsValue::UNDEFINED,
306 &JsValue::from(apierror!(ClientError(message))),
307 &cl.into_js_value(),
308 ) {
309 tracing::warn!("{:?}", e);
310 }
311
312 js_sys::Promise::resolve(&JsValue::UNDEFINED)
313 },
314 );
315
316 let poll_loop = LocalPollLoop::new_async(move |x| JsFuture::from(callback.run(x)));
317 let id = self
318 .client
319 .on_error(asyncfn!(poll_loop, async move |message, reconnect| {
320 poll_loop.poll((message, reconnect)).await;
321 Ok(())
322 }))
323 .await?;
324
325 Ok(id)
326 }
327
328 /// Creates a new [`Table`] from either a _schema_ or _data_.
329 ///
330 /// The [`Client::table`] factory function can be initialized with either a
331 /// _schema_ (see [`Table::schema`]), or data in one of these formats:
332 ///
333 /// - Apache Arrow
334 /// - CSV
335 /// - JSON row-oriented
336 /// - JSON column-oriented
337 /// - NDJSON
338 ///
339 /// When instantiated with _data_, the schema is inferred from this data.
340 /// While this is convenient, inferrence is sometimes imperfect e.g.
341 /// when the input is empty, null or ambiguous. For these cases,
342 /// [`Client::table`] can first be instantiated with a explicit schema.
343 ///
344 /// When instantiated with a _schema_, the resulting [`Table`] is empty but
345 /// with known column names and column types. When subsqeuently
346 /// populated with [`Table::update`], these columns will be _coerced_ to
347 /// the schema's type. This behavior can be useful when
348 /// [`Client::table`]'s column type inferences doesn't work.
349 ///
350 /// The resulting [`Table`] is _virtual_, and invoking its methods
351 /// dispatches events to the `perspective_server::Server` this
352 /// [`Client`] connects to, where the data is stored and all calculation
353 /// occurs.
354 ///
355 /// # Arguments
356 ///
357 /// - `arg` - Either _schema_ or initialization _data_.
358 /// - `options` - Optional configuration which provides one of:
359 /// - `limit` - The max number of rows the resulting [`Table`] can
360 /// store.
361 /// - `index` - The column name, or an array of column names, to use as
362 /// an _index_. If this `Table` is being instantiated by _data_, these
363 /// column names must be present in the data. An array keys rows by
364 /// the combination of the columns' values.
365 /// - `name` - The name of the table. This will be generated if it is
366 /// not provided.
367 /// - `format` - The explicit format of the input data, can be one of
368 /// `"json"`, `"columns"`, `"csv"` or `"arrow"`. This overrides
369 /// language-specific type dispatch behavior, which allows stringified
370 /// and byte array alternative inputs.
371 ///
372 /// # JavaScript Examples
373 ///
374 /// Load a CSV from a `string`:
375 ///
376 /// ```javascript
377 /// const table = await client.table("x,y\n1,2\n3,4");
378 /// ```
379 ///
380 /// Load an Arrow from an `ArrayBuffer`:
381 ///
382 /// ```javascript
383 /// import * as fs from "node:fs/promises";
384 /// const table2 = await client.table(await fs.readFile("superstore.arrow"));
385 /// ```
386 ///
387 /// Load a CSV from a `UInt8Array` (the default for this type is Arrow)
388 /// using a format override:
389 ///
390 /// ```javascript
391 /// const enc = new TextEncoder();
392 /// const table = await client.table(enc.encode("x,y\n1,2\n3,4"), {
393 /// format: "csv",
394 /// });
395 /// ```
396 ///
397 /// Create a table with an `index`:
398 ///
399 /// ```javascript
400 /// const table = await client.table(data, { index: "Row ID" });
401 /// ```
402 ///
403 /// Create a table with a composite `index`:
404 ///
405 /// ```javascript
406 /// const table = await client.table(data, { index: ["name", "sector"] });
407 /// ```
408 #[wasm_bindgen]
409 pub async fn table(
410 &self,
411 value: &JsTableInitData,
412 options: Option<JsTableInitOptions>,
413 ) -> ApiResult<Table> {
414 let options = options
415 .into_serde_ext::<Option<TableInitOptions>>()?
416 .unwrap_or_default();
417
418 let args = TableData::from_js_value(value, options.format)?;
419 Ok(Table(self.client.table(args, options).await?))
420 }
421
422 /// Creates a new read-only [`Table`] by performing an INNER JOIN on two
423 /// source tables. The resulting table is reactive: when either source
424 /// table is updated, the join is automatically recomputed.
425 ///
426 /// # Arguments
427 ///
428 /// - `left` - The left source table (a [`Table`] instance or a table name
429 /// string).
430 /// - `right` - The right source table (a [`Table`] instance or a table name
431 /// string).
432 /// - `on` - The column name to join on. Must exist in both tables with the
433 /// same type.
434 /// - `options` - Optional join configuration: `{ join_type?: "inner" |
435 /// "left" | "outer", name?: string }`.
436 ///
437 /// # JavaScript Examples
438 ///
439 /// ```javascript
440 /// const joined = await client.join(orders_table, products_table, "Product ID", { join_type: "left" });
441 /// const joined = await client.join("orders", "products", "Product ID", { join_type: "left" });
442 /// ```
443 #[wasm_bindgen]
444 pub async fn join(
445 &self,
446 left: JsValue,
447 right: JsValue,
448 on: &str,
449 options: Option<JsJoinOptions>,
450 ) -> ApiResult<Table> {
451 let options = options
452 .into_serde_ext::<Option<perspective_client::JoinOptions>>()?
453 .unwrap_or_default();
454
455 let left_ref = js_to_table_ref(&left).await?;
456 let right_ref = js_to_table_ref(&right).await?;
457 Ok(Table(
458 self.client.join(left_ref, right_ref, on, options).await?,
459 ))
460 }
461
462 /// Terminates this [`Client`], cleaning up any [`crate::View`] handles the
463 /// [`Client`] has open as well as its callbacks.
464 #[wasm_bindgen]
465 pub fn terminate(&self) -> ApiResult<JsValue> {
466 if let Some(f) = self.close.clone() {
467 Ok(f.call0(&JsValue::UNDEFINED)?)
468 } else {
469 Err(ApiError::new("Client type cannot be terminated"))
470 }
471 }
472
473 /// Opens a [`Table`] that is hosted on the `perspective_server::Server`
474 /// that is connected to this [`Client`].
475 ///
476 /// The `name` property of [`TableInitOptions`] is used to identify each
477 /// [`Table`]. [`Table`] `name`s can be looked up for each [`Client`]
478 /// via [`Client::get_hosted_table_names`].
479 ///
480 /// # JavaScript Examples
481 ///
482 /// Get a virtual [`Table`] named "table_one" from this [`Client`]
483 ///
484 /// ```javascript
485 /// const tables = await client.open_table("table_one");
486 /// ```
487 #[wasm_bindgen]
488 pub async fn open_table(&self, entity_id: String) -> ApiResult<Table> {
489 Ok(Table(self.client.open_table(entity_id).await?))
490 }
491
492 /// Unsafely gets a [`View`] by raw ID, useful for JavaScript multi-threaded
493 /// (via Web Worker) context where a standard `View` cannot otherwise be
494 /// shared because its wrapper is not serializable.
495 ///
496 /// # Safety
497 ///
498 /// This method is unsafe because the lifetime of a [`View`] is bound to
499 /// the [`Client`] which created it.
500 ///
501 /// The caller must guarantee that `entity_id` corresponds to a live
502 /// [`crate::View`] on the connected server (obtained from another
503 /// [`Client`]'s [`crate::View::get_name`] and forwarded across the
504 /// serialization boundary).
505 ///
506 /// # JavaScript Examples
507 ///
508 /// ```javascript
509 /// const view = client.__unsafe_open_view(name_from_main_thread);
510 /// const cols = await view.to_columns();
511 /// ```
512 #[wasm_bindgen]
513 pub fn __unsafe_open_view(&self, entity_id: String) -> crate::view::View {
514 crate::view::View(perspective_client::View::new(
515 entity_id,
516 self.client.clone(),
517 ))
518 }
519
520 /// Retrieves the names of all tables that this client has access to.
521 ///
522 /// `name` is a string identifier unique to the [`Table`] (per [`Client`]),
523 /// which can be used in conjunction with [`Client::open_table`] to get
524 /// a [`Table`] instance without the use of [`Client::table`]
525 /// constructor directly (e.g., one created by another [`Client`]).
526 ///
527 /// # JavaScript Examples
528 ///
529 /// ```javascript
530 /// const tables = await client.get_hosted_table_names();
531 /// ```
532 #[wasm_bindgen]
533 pub async fn get_hosted_table_names(&self) -> ApiResult<Vec<String>> {
534 Ok(self.client.get_hosted_table_names().await?)
535 }
536
537 /// Register a callback which is invoked whenever [`Client::table`] (on this
538 /// [`Client`]) or [`Table::delete`] (on a [`Table`] belinging to this
539 /// [`Client`]) are called.
540 #[wasm_bindgen]
541 pub async fn on_hosted_tables_update(&self, on_update_js: Function) -> ApiResult<u32> {
542 let poll_loop = LocalPollLoop::new(move |_| on_update_js.call0(&JsValue::UNDEFINED));
543 let on_update = Box::new(move || poll_loop.poll(()));
544 let id = self.client.on_hosted_tables_update(on_update).await?;
545 Ok(id)
546 }
547
548 /// Remove a callback previously registered via
549 /// `Client::on_hosted_tables_update`.
550 #[wasm_bindgen]
551 pub async fn remove_hosted_tables_update(&self, update_id: u32) -> ApiResult<()> {
552 self.client.remove_hosted_tables_update(update_id).await?;
553 Ok(())
554 }
555
556 /// Provides the [`SystemInfo`] struct, implementation-specific metadata
557 /// about the [`perspective_server::Server`] runtime such as Memory and
558 /// CPU usage.
559 ///
560 /// For WebAssembly servers, this method includes the WebAssembly heap size.
561 ///
562 /// # JavaScript Examples
563 ///
564 /// ```javascript
565 /// const info = await client.system_info();
566 /// ```
567 #[wasm_bindgen]
568 pub async fn system_info(&self) -> ApiResult<JsSystemInfo> {
569 let mut info = self.client.system_info().await?;
570
571 #[cfg(feature = "trace-allocator")]
572 if let perspective_client::SystemInfo {
573 client_used: None,
574 client_heap: None,
575 ..
576 } = &info
577 {
578 let (client_used, client_heap) = crate::utils::get_used();
579 info.client_used = Some(client_used as u64);
580 info.client_heap = Some(client_heap as u64);
581 };
582
583 let timestamp = web_sys::window()
584 .and_then(|x| x.performance())
585 .map(|x| x.now() as u64);
586
587 info.timestamp = timestamp;
588 let record = JsValue::from_serde_ext(&info.cast::<f64>())?;
589 Ok(record.unchecked_into())
590 }
591}
592
593#[wasm_bindgen]
594extern "C" {
595 #[wasm_bindgen(typescript_type = "SystemInfo")]
596 pub type JsSystemInfo;
597}