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