Skip to main content

larust_live/
lib.rs

1//! Server-state-backed reactive components - Larust's Livewire equivalent.
2//! Unlike Livewire's client-held, HMAC-signed state snapshot (a workaround
3//! for PHP/Laravel's stateless-between-requests model), a component's state
4//! lives entirely server-side, keyed by the user's session: only an opaque
5//! component id ever crosses the wire. See `docs/ARCHITECTURE.md`'s
6//! "Reactive components" section for the full design rationale.
7//!
8//! The crate itself keeps its original name (`larust-live`) - only the
9//! user-facing directive/trait/route surface is `@wire`/`WireComponent`/
10//! `/__larust_wire/...`.
11//!
12//! App authors implement [`WireComponent`], register instances via
13//! [`components`]/[`LiveRegistry`], mount them from a template with
14//! `@wire('name', { prop: expr, ... })`, and wire the two routes this crate
15//! provides ([`update`], [`runtime_js`]) into their app's router - nothing
16//! here is auto-mounted, matching this framework's "explicit over magic"
17//! convention for routes/middleware/listeners elsewhere.
18//!
19//! [`push`] is a separate, unrelated feature living in the same crate:
20//! genuine server-*pushed* updates (`@live('channel') ... @endlive`,
21//! WebSocket-based) rather than `@wire`'s client-*initiated* AJAX sync -
22//! see that module's own doc comment for why these two are deliberately
23//! not the same mechanism.
24
25mod component;
26mod lock;
27mod mount;
28pub mod push;
29mod registry;
30mod routes;
31mod state;
32
33pub use component::WireComponent;
34pub use mount::mount;
35pub use registry::{components, LiveRegistry};
36pub use routes::{runtime_js, update, UpdatePayload};
37
38/// The two routes `@wire(...)` needs, bundled for [`larust_http::Router::plugin`] -
39/// sugar for the exact `.get`/`.post` pair this module's own doc comment
40/// shows an app writing by hand today. Registration itself is still the
41/// app's explicit choice (`.plugin(WirePlugin)` in `routes/web.rs`), this
42/// only removes the need to know or copy the two literal route strings.
43pub struct WirePlugin;
44
45impl larust_http::Plugin for WirePlugin {
46    fn routes(&self) -> larust_http::Router {
47        larust_http::Router::new()
48            .get("/__larust_wire/runtime.js", routes::runtime_js)
49            .post("/__larust_wire/{component_id}", routes::update)
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn wire_plugin_contributes_exactly_the_two_routes_an_app_used_to_hand_write() {
59        let routes = larust_http::Router::new().plugin(WirePlugin).routes();
60
61        assert_eq!(routes.len(), 2);
62        assert_eq!(routes[0].method, "GET");
63        assert_eq!(routes[0].path, "/__larust_wire/runtime.js");
64        assert_eq!(routes[1].method, "POST");
65        assert_eq!(routes[1].path, "/__larust_wire/{component_id}");
66    }
67}