perspective_viewer/
lib.rs1#![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#[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#[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
135pub 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#[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}