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