Skip to main content

perspective_viewer/
lib.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
13//! The API for the [`@perspective-dev/viewer`](https://perspective-dev.github.io)
14//! JavaScript library.
15
16// Required by yew's `html` macro.
17#![recursion_limit = "1024"]
18#![feature(const_type_name)]
19#![feature(iter_intersperse)]
20#![feature(stmt_expr_attributes)]
21#![feature(try_blocks)]
22#![allow(async_fn_in_trait)]
23#![warn(
24    clippy::all,
25    clippy::panic_in_result_fn,
26    clippy::fallible_impl_from,
27    clippy::unneeded_field_pattern
28)]
29#![deny(clippy::await_holding_refcell_ref)]
30
31#[cfg(feature = "llm-agent")]
32mod agent;
33pub mod components;
34pub mod config;
35pub mod custom_elements;
36mod custom_events;
37pub mod exprtk;
38mod js;
39mod presentation;
40mod root;
41
42#[doc(hidden)]
43pub mod queries;
44mod renderer;
45mod session;
46
47#[doc(hidden)]
48pub mod tasks;
49pub mod ui;
50pub mod utils;
51mod workspace;
52
53#[macro_use]
54extern crate macro_rules_attribute;
55extern crate alloc;
56
57use std::cell::RefCell;
58
59use perspective_js::utils::*;
60use wasm_bindgen::prelude::*;
61
62use crate::custom_elements::debug_plugin::PerspectiveDebugPluginElement;
63use crate::custom_elements::viewer::PerspectiveViewerElement;
64use crate::utils::define_web_component;
65
66#[wasm_bindgen(typescript_custom_section)]
67const TS_APPEND_CONTENT: &'static str = r#"
68import type {
69    ColumnType,
70    ColumnWindow,
71    DeleteOptions,
72    Features,
73    JoinOptions,
74    OnRemoveData,
75    OnUpdateData,
76    OnUpdateOptions,
77    Scalar,
78    SystemInfo,
79    TableInitOptions,
80    TypedArrayWindow,
81    UpdateOptions,
82    ViewConfig,
83    ViewConfigUpdate,
84    ViewWindow,
85} from "@perspective-dev/client";
86
87export type * from "../../src/ts/ts-rs/ViewerConfig.d.ts";
88export type * from "../../src/ts/ts-rs/ViewerConfigUpdate.d.ts";
89export type * from "../../src/ts/ts-rs/ViewerConfigInitial.d.ts";
90export type * from "../../src/ts/ts-rs/PluginStaticConfig.d.ts";
91export type * from "../../src/ts/ts-rs/WorkspaceConfig.d.ts";
92export type * from "../../src/ts/ts-rs/WorkspaceConfigUpdate.d.ts";
93export type * from "../../src/ts/ts-rs/ExportMethod.d.ts";
94export type * from "../../src/ts/ts-rs/PanelOptions.d.ts";
95export type * from "../../src/ts/ts-rs/RestoreOptions.d.ts";
96export type * from "../../src/ts/ts-rs/RestoreWorkspaceOptions.d.ts";
97export type * from "../../src/ts/ts-rs/AddPanelOptions.d.ts";
98export type * from "../../src/ts/ts-rs/SaveWorkspaceOptions.d.ts";
99export type * from "../../src/ts/ts-rs/ClientOptions.d.ts";
100export type * from "../../src/ts/ts-rs/ExportOptions.d.ts";
101export type * from "../../src/ts/ts-rs/GetTableOptions.d.ts";
102export type * from "../../src/ts/ts-rs/GetClientOptions.d.ts";
103export type * from "../../src/ts/ts-rs/GetViewOptions.d.ts";
104export type * from "../../src/ts/ts-rs/GetViewMode.d.ts";
105export type * from "../../src/ts/ts-rs/CustomNumberFormatConfig.d.ts";
106export type * from "../../src/ts/ts-rs/NumberFormatStyle.d.ts";
107export type * from "../../src/ts/ts-rs/Notation.d.ts";
108export type * from "../../src/ts/ts-rs/DatetimeFormatType.d.ts";
109
110import type {GetTableOptions} from "../../src/ts/ts-rs/GetTableOptions.d.ts";
111import type {PanelOptions} from "../../src/ts/ts-rs/PanelOptions.d.ts";
112import type {RestoreOptions} from "../../src/ts/ts-rs/RestoreOptions.d.ts";
113import type {RestoreWorkspaceOptions} from "../../src/ts/ts-rs/RestoreWorkspaceOptions.d.ts";
114import type {AddPanelOptions} from "../../src/ts/ts-rs/AddPanelOptions.d.ts";
115import type {SaveWorkspaceOptions} from "../../src/ts/ts-rs/SaveWorkspaceOptions.d.ts";
116import type {ExportOptions} from "../../src/ts/ts-rs/ExportOptions.d.ts";
117import type {GetClientOptions} from "../../src/ts/ts-rs/GetClientOptions.d.ts";
118import type {GetViewOptions} from "../../src/ts/ts-rs/GetViewOptions.d.ts";
119import type {ClientOptions} from "../../src/ts/ts-rs/ClientOptions.d.ts";
120import type {ViewerConfig} from "../../src/ts/ts-rs/ViewerConfig.d.ts";
121import type {ViewerConfigUpdate} from "../../src/ts/ts-rs/ViewerConfigUpdate.d.ts";
122import type {ViewerConfigInitial} from "../../src/ts/ts-rs/ViewerConfigInitial.d.ts";
123import type {WorkspaceConfig} from "../../src/ts/ts-rs/WorkspaceConfig.d.ts";
124import type {WorkspaceConfigUpdate} from "../../src/ts/ts-rs/WorkspaceConfigUpdate.d.ts";
125"#;
126
127/// Register a plugin globally.
128#[wasm_bindgen]
129#[allow(non_snake_case)]
130pub fn registerPlugin(name: &str) {
131    use crate::renderer::*;
132    PLUGIN_REGISTRY.register_plugin(name);
133}
134
135/// Register this crate's Custom Elements in the browser's current session.
136///
137/// This must occur before calling any public API methods on these Custom
138/// Elements from JavaScript, as the methods themselves won't be defined yet.
139/// By default, this crate does not register `PerspectiveViewerElement` (as to
140/// preserve backwards-compatible synchronous API).
141#[cfg(not(feature = "external-bootstrap"))]
142#[wasm_bindgen(js_name = "init")]
143pub fn js_init(module: js_sys::WebAssembly::Module, url: web_sys::Url) {
144    console_error_panic_hook::set_once();
145    perspective_js::utils::set_global_logging();
146    define_web_components!("export * as psp from '../../perspective-viewer.js'");
147    MODULE.with_borrow_mut(|f| {
148        *f = Some((module, url));
149    });
150
151    tracing::info!("Perspective initialized.");
152}
153
154thread_local! {
155    static MODULE: RefCell<Option<(js_sys::WebAssembly::Module, web_sys::Url)>> = RefCell::default();
156}
157
158#[cfg(not(feature = "external-bootstrap"))]
159#[wasm_bindgen(js_name = "get_wasm_module")]
160pub fn js_get_module() -> Result<js_sys::WebAssembly::Module, JsValue> {
161    MODULE
162        .with_borrow(|f| f.clone().map(|x| x.0))
163        .ok_or_else(|| "Uninited module".into())
164}
165
166#[cfg(not(feature = "external-bootstrap"))]
167#[wasm_bindgen(js_name = "get_worker_url")]
168pub fn js_get_worker_url() -> Result<web_sys::Url, JsValue> {
169    MODULE
170        .with_borrow(|f| f.clone().map(|x| x.1))
171        .ok_or_else(|| "Uninited module".into())
172}
173
174/// Register Web Components with the global registry, given a Perspective
175/// module.
176///
177/// This function shouldn't be called directly;  instead, use the
178/// `define_web_components!` macro to both call this method and hook the
179/// wasm_bindgen module object.
180pub fn bootstrap_web_components(psp: &JsValue) {
181    define_web_component::<PerspectiveViewerElement>(psp);
182    define_web_component::<PerspectiveDebugPluginElement>(psp);
183}
184
185/// Defining the web components needs an extern struct to reference the
186/// generated JavaSript glue. This is parameterized by an attribute macro which
187/// needs to be determined by the top-level compiled module - the JavaScript
188/// glue code emitted by `wasm-bindgen-cli`.
189#[macro_export]
190macro_rules! define_web_components {
191    ($x:expr) => {{
192        #[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
193        extern "C" {
194            #[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
195            #[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
196            pub static PSP: wasm_bindgen::prelude::JsValue;
197        }
198
199        PSP.with(|x| $crate::bootstrap_web_components(x));
200    }};
201}