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