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
//! Two-way bridge between wasm-bindgen Rust and a mounted Phoenix LiveView.
//!
//! Outbound, this crate wraps the [`Phoenix.LiveView.JS`] command set so
//! Rust/wasm code can fire LV events, navigate, dispatch DOM events, run
//! transitions, and manage focus -- without trampolining through hidden
//! `phx-*` trigger elements. Inbound, it lets Rust subscribe to server-pushed
//! events from `Phoenix.LiveView.push_event/3`.
//!
//! Written for game code that renders in wasm but wants the server to own
//! state, routing, and persistence.
//
//! # Payload types
//!
//! Commands that carry a payload ([`push_event`], [`push_event_to`],
//! [`dispatch_with`]) accept any `T: serde::Serialize`. Use
//! [`serde_json::json!`] for ad-hoc payloads, or define a
//! `#[derive(Serialize)]` struct for typed, compile-time-checked ones. Both
//! styles are shown on each function's page.
//!
//! # Outbound example
//!
//! ```no_run
//! use wasm_liveview as lv;
//!
//! #[derive(serde::Serialize)]
//! struct Submit<'a> { word: &'a str, route: &'a [usize] }
//!
//! lv::push_event("submit_word", &Submit { word: "TRY", route: &[0, 1, 2] })?;
//!
//! // Client-side routing.
//! lv::navigate("/room/42", false)?;
//!
//! // Run a CSS transition.
//! lv::transition(
//! lv::TransitionClasses {
//! transition: &["fade-in"],
//! start: &["opacity-0"],
//! end: &["opacity-100"],
//! },
//! Some("#board"),
//! Some(150),
//! )?;
//! # Ok::<(), lv::Error>(())
//! ```
//!
//! # Inbound example
//!
//! ```no_run
//! use wasm_liveview as lv;
//!
//! #[derive(serde::Deserialize)]
//! struct Score { value: u32 }
//!
//! let sub = lv::subscribe::<Score, _>("score_update", |s| {
//! // handler runs once per server push
//! let _ = s.value;
//! })?;
//!
//! // Drop the subscription to unsubscribe, or:
//! sub.forget();
//! # Ok::<(), lv::Error>(())
//! ```
//!
//! # Target behavior
//!
//! On `wasm32-*` targets the crate pulls in `wasm-bindgen`, `js-sys`, and
//! `web-sys` and calls `window.liveSocket.execJS` for real. On any other
//! target every outbound function stubs to `Ok(())` and [`subscribe`] returns
//! an inert handle, so the JSON wire-format encoders can be unit-tested
//! without a browser.
//!
//! # Error model
//!
//! Every fallible call returns `Result<_, `[`Error`]`>`. See the [`Error`]
//! enum for the failure modes (missing `window`, uninitialized `liveSocket`,
//! JSON serialization failures, and JS exceptions thrown by `execJS`).
//!
//! [`Phoenix.LiveView.JS`]: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.JS.html
pub use Bridge;
pub use ;
pub use exec_attr;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Error;
pub use ;