perspective_js/view.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 js_sys::{Array, ArrayBuffer, Function, Object};
14use perspective_client::{
15 ColumnWindow, OnRemoveData, OnUpdateData, OnUpdateOptions, ViewWindow, assert_view_api,
16};
17use wasm_bindgen::prelude::*;
18use wasm_bindgen_futures::spawn_local;
19
20#[cfg(doc)]
21use crate::table::Table;
22use crate::utils::{ApiFuture, ApiResult, JsValueSerdeExt, LocalPollLoop};
23
24#[wasm_bindgen]
25unsafe extern "C" {
26 #[wasm_bindgen(typescript_type = "ViewWindow")]
27 #[derive(Clone)]
28 pub type JsViewWindow;
29
30 #[wasm_bindgen(typescript_type = "ColumnWindow")]
31 #[derive(Clone)]
32 pub type JsColumnWindow;
33
34 #[wasm_bindgen(method, setter, js_name = "formatted")]
35 pub fn set_formatted(this: &JsViewWindow, x: bool);
36
37 #[wasm_bindgen(typescript_type = "OnUpdateOptions")]
38 pub type JsOnUpdateOptions;
39
40}
41
42impl From<ViewWindow> for JsViewWindow {
43 fn from(value: ViewWindow) -> Self {
44 JsViewWindow::from_serde_ext(&value)
45 .unwrap()
46 .unchecked_into()
47 }
48}
49
50fn scalar_to_jsvalue(scalar: &perspective_client::config::Scalar) -> JsValue {
51 match scalar {
52 perspective_client::config::Scalar::Float(x) => JsValue::from_f64(*x),
53 perspective_client::config::Scalar::String(x) => JsValue::from_str(x),
54 perspective_client::config::Scalar::Bool(x) => JsValue::from_bool(*x),
55 perspective_client::config::Scalar::Null => JsValue::NULL,
56 }
57}
58
59/// The [`View`] struct is Perspective's query and serialization interface. It
60/// represents a query on the `Table`'s dataset and is always created from an
61/// existing `Table` instance via the [`Table::view`] method.
62///
63/// [`View`]s are immutable with respect to the arguments provided to the
64/// [`Table::view`] method; to change these parameters, you must create a new
65/// [`View`] on the same [`Table`]. However, each [`View`] is _live_ with
66/// respect to the [`Table`]'s data, and will (within a conflation window)
67/// update with the latest state as its parent [`Table`] updates, including
68/// incrementally recalculating all aggregates, pivots, filters, etc. [`View`]
69/// query parameters are composable, in that each parameter works independently
70/// _and_ in conjunction with each other, and there is no limit to the number of
71/// pivots, filters, etc. which can be applied.
72#[wasm_bindgen]
73#[derive(Clone)]
74pub struct View(pub(crate) perspective_client::View);
75
76assert_view_api!(View);
77
78impl From<perspective_client::View> for View {
79 fn from(value: perspective_client::View) -> Self {
80 View(value)
81 }
82}
83
84#[wasm_bindgen]
85impl View {
86 #[doc(hidden)]
87 pub fn __get_model(&self) -> View {
88 self.clone()
89 }
90
91 #[wasm_bindgen]
92 #[doc(hidden)]
93 pub fn __unsafe_get_name(&self) -> String {
94 self.0.name.clone()
95 }
96
97 /// Returns an array of strings containing the column paths of the [`View`]
98 /// without any of the source columns.
99 ///
100 /// A column path shows the columns that a given cell belongs to after
101 /// pivots are applied.
102 #[wasm_bindgen]
103 pub async fn column_paths(&self, window: Option<JsColumnWindow>) -> ApiResult<JsValue> {
104 let window = window.into_serde_ext::<Option<ColumnWindow>>()?;
105 let columns = self.0.column_paths(window.unwrap_or_default()).await?;
106 Ok(JsValue::from_serde_ext(&columns)?)
107 }
108
109 /// Delete this [`View`] and clean up all resources associated with it.
110 /// [`View`] objects do not stop consuming resources or processing
111 /// updates when they are garbage collected - you must call this method
112 /// to reclaim these.
113 #[wasm_bindgen]
114 pub async fn delete(self) -> ApiResult<()> {
115 self.0.delete().await?;
116 Ok(())
117 }
118
119 /// Returns this [`View`]'s _dimensions_, row and column count, as well as
120 /// those of the [`crate::Table`] from which it was derived.
121 ///
122 /// - `num_table_rows` - The number of rows in the underlying
123 /// [`crate::Table`].
124 /// - `num_table_columns` - The number of columns in the underlying
125 /// [`crate::Table`] (including the `index` column if this
126 /// [`crate::Table`] was constructed with one).
127 /// - `num_view_rows` - The number of rows in this [`View`]. If this
128 /// [`View`] has a `group_by` clause, `num_view_rows` will also include
129 /// aggregated rows.
130 /// - `num_view_columns` - The number of columns in this [`View`]. If this
131 /// [`View`] has a `split_by` clause, `num_view_columns` will include all
132 /// _column paths_, e.g. the number of `columns` clause times the number
133 /// of `split_by` groups.
134 #[wasm_bindgen]
135 pub async fn dimensions(&self) -> ApiResult<JsValue> {
136 let dimensions = self.0.dimensions().await?;
137 Ok(JsValue::from_serde_ext(&dimensions)?)
138 }
139
140 /// The expression schema of this [`View`], which contains only the
141 /// expressions created on this [`View`]. See [`View::schema`] for
142 /// details.
143 #[wasm_bindgen]
144 pub async fn expression_schema(&self) -> ApiResult<JsValue> {
145 let schema = self.0.expression_schema().await?;
146 Ok(JsValue::from_serde_ext(&schema)?)
147 }
148
149 /// A copy of the config object passed to the [`Table::view`] method which
150 /// created this [`View`].
151 #[wasm_bindgen]
152 pub async fn get_config(&self) -> ApiResult<JsValue> {
153 let config = self.0.get_config().await?;
154 Ok(JsValue::from_serde_ext(&config)?)
155 }
156
157 /// Calculates the [min, max] of the leaf nodes of a column `column_name`.
158 ///
159 /// # Returns
160 ///
161 /// A tuple of [min, max], whose types are column and aggregate dependent.
162 #[wasm_bindgen]
163 pub async fn get_min_max(&self, name: String) -> ApiResult<Array> {
164 let result = self.0.get_min_max(name).await?;
165 let arr = Array::new();
166 arr.push(&scalar_to_jsvalue(&result.0));
167 arr.push(&scalar_to_jsvalue(&result.1));
168 Ok(arr)
169 }
170
171 /// The number of aggregated rows in this [`View`]. This is affected by the
172 /// "group_by" configuration parameter supplied to this view's contructor.
173 ///
174 /// # Returns
175 ///
176 /// The number of aggregated rows.
177 #[wasm_bindgen]
178 pub async fn num_rows(&self) -> ApiResult<i32> {
179 let size = self.0.num_rows().await?;
180 Ok(size as i32)
181 }
182
183 /// The schema of this [`View`].
184 ///
185 /// The [`View`] schema differs from the `schema` returned by
186 /// [`Table::schema`]; it may have different column names due to
187 /// `expressions` or `columns` configs, or it maye have _different
188 /// column types_ due to the application og `group_by` and `aggregates`
189 /// config. You can think of [`Table::schema`] as the _input_ schema and
190 /// [`View::schema`] as the _output_ schema of a Perspective pipeline.
191 #[wasm_bindgen]
192 pub async fn schema(&self) -> ApiResult<JsValue> {
193 let schema = self.0.schema().await?;
194 Ok(JsValue::from_serde_ext(&schema)?)
195 }
196
197 /// Serializes a [`View`] to the Apache Arrow data format.
198 ///
199 /// # Arguments
200 ///
201 /// - `window` - a [`ViewWindow`]; its `compression` key selects Arrow IPC
202 /// body compression, `"lz4"` or `"zstd"` (uncompressed when omitted).
203 ///
204 /// # JavaScript Examples
205 ///
206 /// ```javascript
207 /// const arrow = await view.to_arrow({ compression: "zstd" });
208 /// ```
209 #[wasm_bindgen]
210 pub async fn to_arrow(&self, window: Option<JsViewWindow>) -> ApiResult<ArrayBuffer> {
211 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
212 let arrow = self.0.to_arrow(window.unwrap_or_default()).await?;
213 Ok(js_sys::Uint8Array::from(&arrow[..])
214 .buffer()
215 .unchecked_into())
216 }
217
218 /// Serializes this [`View`] to a string of JSON data. Useful if you want to
219 /// save additional round trip serialize/deserialize cycles.
220 #[wasm_bindgen]
221 pub async fn to_columns_string(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
222 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
223 let json = self.0.to_columns_string(window.unwrap_or_default()).await?;
224 Ok(json)
225 }
226
227 /// Serializes this [`View`] to JavaScript objects in a column-oriented
228 /// format.
229 #[wasm_bindgen]
230 pub async fn to_columns(&self, window: Option<JsViewWindow>) -> ApiResult<Object> {
231 let json = self.to_columns_string(window).await?;
232 Ok(js_sys::JSON::parse(&json)?.unchecked_into())
233 }
234
235 /// Render this `View` as a JSON string.
236 #[wasm_bindgen]
237 pub async fn to_json_string(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
238 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
239 let json = self.0.to_json_string(window.unwrap_or_default()).await?;
240 Ok(json)
241 }
242
243 /// Serializes this [`View`] to JavaScript objects in a row-oriented
244 /// format.
245 #[wasm_bindgen]
246 pub async fn to_json(&self, window: Option<JsViewWindow>) -> ApiResult<Array> {
247 let json = self.to_json_string(window).await?;
248 Ok(js_sys::JSON::parse(&json)?.unchecked_into())
249 }
250
251 /// Renders this [`View`] as an [NDJSON](https://github.com/ndjson/ndjson-spec)
252 /// formatted [`String`].
253 #[wasm_bindgen]
254 pub async fn to_ndjson(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
255 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
256 let ndjson = self.0.to_ndjson(window.unwrap_or_default()).await?;
257 Ok(ndjson)
258 }
259
260 /// Serializes this [`View`] to CSV data in a standard format.
261 #[wasm_bindgen]
262 pub async fn to_csv(&self, window: Option<JsViewWindow>) -> ApiResult<String> {
263 let window = window.into_serde_ext::<Option<ViewWindow>>()?;
264 Ok(self.0.to_csv(window.unwrap_or_default()).await?)
265 }
266
267 /// Fetches columns from the [`View`] in Arrow format, decodes them, and
268 /// passes typed array views to `callback`. All arrays are only valid for
269 /// the duration of the callback — if `callback` returns a `Promise`, it
270 /// is awaited before the backing Arrow buffer is released, so async
271 /// callbacks may use the views for the full duration of the awaited
272 /// work (e.g. across an `await requestAnimationFrame`-backed promise).
273 ///
274 /// # Arguments
275 ///
276 /// - `window` - Optional [`TypedArrayWindow`] controlling row/column
277 /// windowing and output options (e.g., `float32` mode).
278 /// - `callback` - A JS function called with `(names: string[], values:
279 /// TypedArray[], validities: (Uint8Array|null)[], dictionaries:
280 /// (string[]|null)[]) => void | Promise<void>`.
281 #[wasm_bindgen]
282 pub async fn with_typed_arrays(
283 &self,
284 window: Option<crate::typed_array::JsTypedArrayWindow>,
285 callback: Function,
286 ) -> ApiResult<()> {
287 let opts: crate::typed_array::TypedArrayWindow = window
288 .into_serde_ext::<Option<crate::typed_array::TypedArrayWindow>>()?
289 .unwrap_or_default();
290
291 let float32 = opts.float32;
292 let mut view_window: ViewWindow = opts.into();
293 view_window.emit_legacy_row_path_names = Some(false);
294 let arrow = self.0.to_arrow(view_window).await?;
295 crate::typed_array::decode_and_call(&arrow, float32, &callback).await?;
296 Ok(())
297 }
298
299 /// Register a callback with this [`View`]. Whenever the view's underlying
300 /// table emits an update, this callback will be invoked with an object
301 /// containing `port_id`, indicating which port the update fired on, and
302 /// optionally `delta`, which is the new data that was updated for each
303 /// cell or each row (as an Arrow).
304 ///
305 /// # Arguments
306 ///
307 /// - `on_update` - A callback function invoked on update, which receives an
308 /// object with two keys: `port_id`, indicating which port the update was
309 /// triggered on, and `delta`, whose value is dependent on the mode
310 /// parameter.
311 /// - `options` - If this is provided as `{mode: "row"}`, then `delta` is an
312 /// Arrow of the updated rows. Otherwise `delta` will be `null`.
313 ///
314 /// # JavaScript Examples
315 ///
316 /// ```javascript
317 /// view.on_update((updated) => console.log(updated.port_id));
318 /// ```
319 ///
320 /// ```javascript
321 /// view.on_update((updated) => console.log(updated.delta), { mode: "row" });
322 /// ```
323 #[wasm_bindgen]
324 pub fn on_update(
325 &self,
326 #[wasm_bindgen(unchecked_param_type = "(data: OnUpdateData) => void")]
327 on_update_js: Function,
328 options: Option<JsOnUpdateOptions>,
329 ) -> ApiFuture<u32> {
330 let poll_loop = LocalPollLoop::new(move |args: OnUpdateData| {
331 let js_obj = JsValue::from_serde_ext(&*args)?;
332 on_update_js.call1(&JsValue::UNDEFINED, &js_obj)
333 });
334
335 let on_update = Box::new(move |msg| poll_loop.poll(msg));
336 let view = self.0.clone();
337 ApiFuture::new(async move {
338 let on_update_opts = options
339 .into_serde_ext::<Option<OnUpdateOptions>>()?
340 .unwrap_or_default();
341
342 let id = view.on_update(on_update, on_update_opts).await?;
343 Ok(id)
344 })
345 }
346
347 /// Unregister a previously registered update callback with this [`View`].
348 ///
349 /// # Arguments
350 ///
351 /// - `id` - A callback `id` as returned by a recipricol call to
352 /// [`View::on_update`].
353 #[wasm_bindgen]
354 pub async fn remove_update(&self, callback_id: u32) -> ApiResult<()> {
355 Ok(self.0.remove_update(callback_id).await?)
356 }
357
358 /// Register a callback which is invoked whenever rows are removed from
359 /// this [`View`]'s [`Table`] by [`Table::remove`], with an object
360 /// containing `port_id` and `indices`, the removed `index` column
361 /// values as an Arrow of one column named after the index.
362 ///
363 /// [`Table::replace`] reports the keys it does not re-supply and
364 /// [`Table::clear`] reports every key. `on_remove` never fires for a
365 /// [`Table`] without an `index`.
366 ///
367 /// # JavaScript Examples
368 ///
369 /// ```javascript
370 /// const id = await view.on_remove(({ indices, port_id }) => {
371 /// replica.remove(indices);
372 /// });
373 /// ```
374 #[wasm_bindgen]
375 pub fn on_remove(
376 &self,
377 #[wasm_bindgen(unchecked_param_type = "(data: OnRemoveData) => void")]
378 on_remove_js: Function,
379 ) -> ApiFuture<u32> {
380 let poll_loop = LocalPollLoop::new(move |args: OnRemoveData| {
381 let js_obj = JsValue::from_serde_ext(&*args)?;
382 on_remove_js.call1(&JsValue::UNDEFINED, &js_obj)
383 });
384
385 let on_remove = Box::new(move |msg| poll_loop.poll(msg));
386 let view = self.0.clone();
387 ApiFuture::new(async move { Ok(view.on_remove(on_remove).await?) })
388 }
389
390 /// Unregister a previously registered [`View::on_remove`] callback.
391 ///
392 /// # Arguments
393 ///
394 /// - `id` - A callback `id` as returned by a reciprocal call to
395 /// [`View::on_remove`].
396 #[wasm_bindgen]
397 pub async fn remove_remove(&self, callback_id: u32) -> ApiResult<()> {
398 Ok(self.0.remove_remove(callback_id).await?)
399 }
400
401 /// Register a callback with this [`View`]. Whenever the [`View`] is
402 /// deleted, this callback will be invoked.
403 #[wasm_bindgen]
404 pub fn on_delete(&self, on_delete: Function) -> ApiFuture<u32> {
405 let view = self.clone();
406 ApiFuture::new(async move {
407 let emit = LocalPollLoop::new(move |()| on_delete.call0(&JsValue::UNDEFINED));
408 let on_delete = Box::new(move || spawn_local(emit.poll(())));
409 Ok(view.0.on_delete(on_delete).await?)
410 })
411 }
412
413 /// The number of aggregated columns in this [`View`]. This is affected by
414 /// the "split_by" configuration parameter supplied to this view's
415 /// contructor.
416 ///
417 /// # Returns
418 ///
419 /// The number of aggregated columns.
420 #[wasm_bindgen]
421 pub async fn num_columns(&self) -> ApiResult<u32> {
422 // TODO: This is broken because of how split by creates a
423 // cartesian product of columns * unique values.
424 Ok(self.0.dimensions().await?.num_view_columns)
425 }
426
427 /// Unregister a previously registered [`View::on_delete`] callback.
428 #[wasm_bindgen]
429 pub fn remove_delete(&self, callback_id: u32) -> ApiFuture<()> {
430 let client = self.0.clone();
431 ApiFuture::new(async move {
432 client.remove_delete(callback_id).await?;
433 Ok(())
434 })
435 }
436
437 /// Collapses the `group_by` row at `row_index`.
438 #[wasm_bindgen]
439 pub async fn collapse(&self, row_index: u32) -> ApiResult<u32> {
440 Ok(self.0.collapse(row_index).await?)
441 }
442
443 /// Expand the `group_by` row at `row_index`.
444 #[wasm_bindgen]
445 pub async fn expand(&self, row_index: u32) -> ApiResult<u32> {
446 Ok(self.0.expand(row_index).await?)
447 }
448
449 /// Set expansion `depth` of the `group_by` tree.
450 #[wasm_bindgen]
451 pub async fn set_depth(&self, depth: u32) -> ApiResult<()> {
452 Ok(self.0.set_depth(depth).await?)
453 }
454}