pub trait WireComponent:
Serialize
+ DeserializeOwned
+ Send
+ Sync
+ 'static {
const NAME: &'static str;
// Required methods
fn mount(
session: &Session,
props: &HashMap<String, Value>,
) -> impl Future<Output = Self> + Send;
fn render(&self) -> impl Future<Output = View> + Send;
// Provided method
fn call(
&mut self,
_session: &Session,
action: &str,
_args: &Value,
) -> impl Future<Output = Result<Option<String>, AppError>> + Send { ... }
}Expand description
A server-state-backed reactive component - Larust’s Livewire-equivalent.
Unlike Livewire, Self’s state never crosses the wire: it lives
server-side, keyed by the user’s session (see larust-live::state), and
only an opaque component id is ever sent to/from the browser.
Implementors are typically small #[derive(Serialize, Deserialize)]
structs holding just the fields a wire:model/wire:model.live field
binds to, plus whatever else render needs to recompute its output.
Prefer a struct with named fields - even a wire:click-only component
with no wire:model bindings at all - over a fieldless unit struct
(struct Foo;, which serde_json serializes as Value::Null rather
than a JSON object): a client sending a non-empty props object for
such a component (a bug, or a crafted request) is rejected with a
clean 422, whereas an entirely-empty prop sync (the normal case for a
wire:click-only component) works either way.
mount/render/call are async (spelled as -> impl Future<..> + Send rather than async fn, so the + Send bound is explicit rather
than relying on an external #[trait_variant]-style macro) because a
real component routinely needs to do real async work - querying the
database in render (a search box), or persisting a change to it from
an action wire:click dispatches to in call. A sync-only trait would
make that impossible to express here at all.
Required Associated Constants§
Sourceconst NAME: &'static str
const NAME: &'static str
A stable, app-chosen registration name - the string a
@wire('name', ...) call site names. Deliberately not
std::any::type_name::<Self>(): that string isn’t stable across a
rename, and every already-mounted, session-stored instance would
silently stop resolving the moment it changed.
Required Methods§
Sourcefn mount(
session: &Session,
props: &HashMap<String, Value>,
) -> impl Future<Output = Self> + Send
fn mount( session: &Session, props: &HashMap<String, Value>, ) -> impl Future<Output = Self> + Send
Builds this component’s initial state from the props its
@wire(...) call site passed. A prop name this component doesn’t
recognize is simply ignored - the same “unset becomes nothing”
tolerance @global/@stack already have. session is the same
real session call’s own session param is - available here so a
component can capture per-viewer identity once, at mount time (e.g.
“does the current user own this record, so should they see edit/
delete controls” - demo’s PostList does exactly this), rather
than needing to be told it again on every subsequent render.
Sourcefn render(&self) -> impl Future<Output = View> + Send
fn render(&self) -> impl Future<Output = View> + Send
Renders the current state. Typically ends with a
larust_support::view!("components.name", { .. }) call - use
View::into_html(), not into_response(), since this is a fragment,
not a full page (a fragment must never get the dev-reload script
spliced into it).
Provided Methods§
Sourcefn call(
&mut self,
_session: &Session,
action: &str,
_args: &Value,
) -> impl Future<Output = Result<Option<String>, AppError>> + Send
fn call( &mut self, _session: &Session, action: &str, _args: &Value, ) -> impl Future<Output = Result<Option<String>, AppError>> + Send
Dispatches a wire:click="action_name"/wire:submit="action_name"-
style call, mutating self in place. session is the requesting
user’s real session - the same one mount()’s surrounding
@wire(...) call site has, threaded through here so an action can
look up the logged-in user (larust_support::auth::id(session)) to
do real, per-user work (creating a record as its author, say), not
just mutate local component state. The default rejects every action
name - a display-only, wire:model-only component needs zero
action boilerplate; there’s no silent-security-gap risk in
defaulting to “reject everything” the way Policy<U>’s
deliberately-defaultless abilities avoid a different risk
(accidentally-permissive authorization).
Returning Ok(Some(path)) tells the client to navigate the browser
to path instead of patching the current fragment in place -
Livewire’s own redirect(), for the common case of an action that
finishes by taking the user somewhere else entirely (a form
wire:submit that creates a record and sends the browser to it).
Ok(None) is the normal case: re-render this component in place
with whatever self mutation the action made (including, e.g.,
setting a validation-errors field for the next render to display).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".