1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
//! Vertigo is a library for building reactive web components.
//!
//! It mainly consists of three parts:
//!
//! * **Reactive dependencies** - A graph of values and clients (micro-subscriptions)
//! that can automatically compute what to refresh after one or more change(s)
//! * **Real DOM operations** - No intermediate Virtual DOM mechanism is necessary
//! * **HTML/CSS macros** - Allows to construct Real DOM nodes using HTML and CSS
//!
//! ## Example 1
//!
//! ```rust
//! use vertigo::{dom, DomElement, Value, bind, start_app};
//!
//! pub fn render(count: Value<i32>) -> DomElement {
//! let increment = bind!(count, || {
//! count.change(|value| {
//! *value += 1;
//! });
//! });
//!
//! let decrement = bind!(count, || {
//! count.change(|value| {
//! *value -= 1;
//! });
//! });
//!
//! dom! {
//! <div>
//! <p>"Counter: " { count }</p>
//! <button on_click={decrement}>"-"</button>
//! <button on_click={increment}>"+"</button>
//! </div>
//! }
//! }
//!
//! #[no_mangle]
//! pub fn start_application() {
//! start_app(|| -> DomElement {
//! let count = Value::new(0);
//! render(count)
//! });
//! }
//! ```
//!
//! ## Example 2
//!
//! ```rust
//! use vertigo::{DomElement, Value, dom, css_fn};
//!
//! pub struct MyMessage {
//! pub message: Value<String>,
//! }
//!
//! impl MyMessage {
//! pub fn mount(self) -> DomElement {
//! dom! {
//! <p>
//! "Message to the world: "
//! { self.message }
//! </p>
//! }
//! }
//! }
//!
//! css_fn! { main_div, "
//! color: darkblue;
//! " }
//!
//! fn render() -> DomElement {
//! let message = Value::new("Hello world!".to_string());
//!
//! dom! {
//! <div css={main_div()}>
//! <MyMessage message={message} />
//! </div>
//! }
//! }
//! ```
//!
//! To get started you may consider looking at the
//! [Tutorial](https://github.com/vertigo-web/vertigo/blob/master/tutorial.md).
#![deny(rust_2018_idioms)]
#![feature(try_trait_v2)] // https://github.com/rust-lang/rust/issues/84277
mod computed;
mod css;
mod dom_list;
mod dom_value;
mod dom;
mod driver_module;
mod fetch;
mod future_box;
mod html_macro;
pub mod inspect;
mod instant;
pub mod router;
mod websocket;
pub use computed::{
AutoMap, Client, Computed, context::Context, Dependencies, DropResource, GraphId, struct_mut, Value
};
pub use dom::{
css::{Css, CssGroup},
dom_id::DomId,
dom_comment::DomComment,
dom_comment_create::DomCommentCreate,
dom_element::DomElement,
dom_node::{DomNode, DomNodeFragment},
dom_text::DomText,
types::{KeyDownEvent, DropFileEvent, DropFileItem},
};
pub use dom_list::ListRendered;
pub use driver_module::{
api::ApiImport,
driver::{Driver, FetchResult, FetchMethod},
js_value::js_value_struct::JsValue,
dom_command::DriverDomCommand,
};
pub use fetch::{
fetch_builder::FetchBuilder,
lazy_cache::{self, LazyCache},
pinboxfut::PinBoxFuture,
request_builder::{ListRequestTrait, RequestBuilder, RequestResponse, SingleRequestTrait},
resource::Resource,
};
pub use future_box::{FutureBoxSend, FutureBox};
pub use html_macro::{
EmbedDom
};
pub use instant::{
Instant, InstantType
};
pub use websocket::{WebsocketConnection, WebsocketMessage};
/// Allows to create an event handler based on provided arguments
pub use vertigo_macro::bind;
/// Allows to create an event handler based on provided arguments which is wrapped in Rc
pub use vertigo_macro::bind_rc;
/// Allows to create an event handler based on provided arguments which launches an asynchronous action
pub use vertigo_macro::bind_spawn;
#[cfg(feature = "serde_request")]
/// Implements [ListRequestTrait] using serde (needs `serde_request` feature).
pub use vertigo_macro::SerdeListRequest;
#[cfg(feature = "serde_request")]
/// Implements both [SingleRequestTrait] and [ListRequestTrait] using serde (needs `serde_request` feature).
pub use vertigo_macro::SerdeRequest;
#[cfg(feature = "serde_request")]
/// Implements [SingleRequestTrait] using serde (needs `serde_request` feature).
pub use vertigo_macro::SerdeSingleRequest;
#[cfg(feature = "serde_request")]
pub use serde_json;
// Export log module which can be used in vertigo plugins
pub use log;
/// Allows to create DomElement using HTML tags.
///
/// ```rust
/// use vertigo::dom;
///
/// let value = "world";
///
/// dom! {
/// <div>
/// <h3>"Hello " {value} "!"</h3>
/// <p>"Good morning!"</p>
/// </div>
/// };
/// ```
pub use vertigo_macro::dom;
/// Version of `dom!` macro that additionally emits compiler warning with generated code.
pub use vertigo_macro::dom_debug;
/// Allows to create Css styles for virtual DOM.
///
/// ```rust
/// use vertigo::{Css, css};
///
/// fn green_on_red() -> Css {
/// css! { "
/// color: green;
/// background-color: red;
/// " }
/// }
/// ```
pub use vertigo_macro::css;
/// Constructs a CSS block that can be manually pushed into existing Css styles instance.
///
/// ```rust
/// use vertigo::{css_fn, css_block};
///
/// css_fn! { green, "
/// color: green;
/// " }
///
/// let mut green_style = green();
///
/// green_style.push_str(
/// css_block! { "
/// font-style: italic;
/// " }
/// );
/// ```
pub use vertigo_macro::css_block;
/// Starting point of the app.
pub fn start_app(get_component: impl FnOnce() -> DomElement) {
get_driver_state("start_app", |state| {
state.driver.init_env();
let app = get_component();
let root = DomElement::create_with_id(DomId::root());
root.add_child(app);
state.set_root(root);
get_driver().inner.dom.flush_dom_changes();
});
}
/// Getter for Driver singleton.
pub fn get_driver() -> Driver {
DRIVER_BROWSER.with(|state| {
state.driver.clone()
})
}
/// Do bunch of operations without triggering anything in between.
pub fn transaction<R, F: FnOnce(&Context) -> R>(f: F) -> R {
get_driver().transaction(f)
}
//------------------------------------------------------------------------------------------------------------------
// Internals below
//------------------------------------------------------------------------------------------------------------------
mod external_api;
use external_api::{DRIVER_BROWSER, DriverConstruct};
fn get_driver_state<R: Default, F: FnOnce(&DriverConstruct) -> R>(label: &'static str, once: F) -> R {
match DRIVER_BROWSER.try_with(once) {
Ok(value) => value,
Err(_) => {
if label != "free" {
println!("error access {label}");
}
R::default()
}
}
}
// Methods for memory allocation
#[no_mangle]
pub fn alloc(size: u32) -> u32 {
get_driver_state("alloc", |state| {
state.driver.inner.api.arguments.alloc(size)
})
}
#[no_mangle]
pub fn free(pointer: u32) {
get_driver_state("free", |state| {
state.driver.inner.api.arguments.free(pointer);
})
}
// Callbacks gateways
#[no_mangle]
pub fn wasm_callback(callback_id: u64, value_ptr: u32) -> u64 {
get_driver_state("export_dom_callback", |state| {
let (ptr, size) = state.driver.wasm_callback(callback_id, value_ptr);
let ptr = ptr as u64;
let size = size as u64;
(ptr << 32) + size
})
}