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#![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#[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#[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
134pub 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#[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}