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/SaveWorkspaceOptions.d.ts";
97export type * from "../../src/ts/ts-rs/ClientOptions.d.ts";
98export type * from "../../src/ts/ts-rs/ExportOptions.d.ts";
99export type * from "../../src/ts/ts-rs/GetTableOptions.d.ts";
100export type * from "../../src/ts/ts-rs/GetClientOptions.d.ts";
101export type * from "../../src/ts/ts-rs/CustomNumberFormatConfig.d.ts";
102export type * from "../../src/ts/ts-rs/NumberFormatStyle.d.ts";
103export type * from "../../src/ts/ts-rs/Notation.d.ts";
104export type * from "../../src/ts/ts-rs/DatetimeFormatType.d.ts";
105export type * from "../../src/ts/ts-rs/StringColorMode.d.ts";
106export type * from "../../src/ts/ts-rs/DatetimeColorMode.d.ts";
107export type * from "../../src/ts/ts-rs/FormatMode.d.ts";
108
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 {SaveWorkspaceOptions} from "../../src/ts/ts-rs/SaveWorkspaceOptions.d.ts";
113import type {ExportOptions} from "../../src/ts/ts-rs/ExportOptions.d.ts";
114import type {GetClientOptions} from "../../src/ts/ts-rs/GetClientOptions.d.ts";
115import type {ClientOptions} from "../../src/ts/ts-rs/ClientOptions.d.ts";
116import type {ViewerConfig} from "../../src/ts/ts-rs/ViewerConfig.d.ts";
117import type {ViewerConfigUpdate} from "../../src/ts/ts-rs/ViewerConfigUpdate.d.ts";
118import type {ViewerConfigInitial} from "../../src/ts/ts-rs/ViewerConfigInitial.d.ts";
119import type {WorkspaceConfig} from "../../src/ts/ts-rs/WorkspaceConfig.d.ts";
120import type {WorkspaceConfigUpdate} from "../../src/ts/ts-rs/WorkspaceConfigUpdate.d.ts";
121"#;
122
123/// Register a plugin globally.
124#[wasm_bindgen]
125#[allow(non_snake_case)]
126pub fn registerPlugin(name: &str) {
127    use crate::renderer::*;
128    PLUGIN_REGISTRY.register_plugin(name);
129}
130
131/// Register this crate's Custom Elements in the browser's current session.
132///
133/// This must occur before calling any public API methods on these Custom
134/// Elements from JavaScript, as the methods themselves won't be defined yet.
135/// By default, this crate does not register `PerspectiveViewerElement` (as to
136/// preserve backwards-compatible synchronous API).
137#[cfg(not(feature = "external-bootstrap"))]
138#[wasm_bindgen(js_name = "init")]
139pub fn js_init(module: js_sys::WebAssembly::Module, url: web_sys::Url) {
140    console_error_panic_hook::set_once();
141    perspective_js::utils::set_global_logging();
142    define_web_components!("export * as psp from '../../perspective-viewer.js'");
143    MODULE.with_borrow_mut(|f| {
144        *f = Some((module, url));
145    });
146
147    tracing::info!("Perspective initialized.");
148}
149
150thread_local! {
151    static MODULE: RefCell<Option<(js_sys::WebAssembly::Module, web_sys::Url)>> = RefCell::default();
152}
153
154#[cfg(not(feature = "external-bootstrap"))]
155#[wasm_bindgen(js_name = "get_wasm_module")]
156pub fn js_get_module() -> Result<js_sys::WebAssembly::Module, JsValue> {
157    MODULE
158        .with_borrow(|f| f.clone().map(|x| x.0))
159        .ok_or_else(|| "Uninited module".into())
160}
161
162#[cfg(not(feature = "external-bootstrap"))]
163#[wasm_bindgen(js_name = "get_worker_url")]
164pub fn js_get_worker_url() -> Result<web_sys::Url, JsValue> {
165    MODULE
166        .with_borrow(|f| f.clone().map(|x| x.1))
167        .ok_or_else(|| "Uninited module".into())
168}
169
170/// Register Web Components with the global registry, given a Perspective
171/// module.
172///
173/// This function shouldn't be called directly;  instead, use the
174/// `define_web_components!` macro to both call this method and hook the
175/// wasm_bindgen module object.
176pub fn bootstrap_web_components(psp: &JsValue) {
177    define_web_component::<PerspectiveViewerElement>(psp);
178    define_web_component::<PerspectiveDebugPluginElement>(psp);
179}
180
181/// Defining the web components needs an extern struct to reference the
182/// generated JavaSript glue. This is parameterized by an attribute macro which
183/// needs to be determined by the top-level compiled module - the JavaScript
184/// glue code emitted by `wasm-bindgen-cli`.
185#[macro_export]
186macro_rules! define_web_components {
187    ($x:expr) => {{
188        #[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
189        extern "C" {
190            #[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
191            #[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
192            pub static PSP: wasm_bindgen::prelude::JsValue;
193        }
194
195        PSP.with(|x| $crate::bootstrap_web_components(x));
196    }};
197}