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