Skip to main content

Crate livewire

Crate livewire 

Source
Expand description

§Livewire for Rust

Livewire renders a component on the server, sends the browser a signed snapshot of its state, and answers the browser’s updates with new HTML and effects. This crate ports the server half of Livewire 4.x to Laravel for Rust, and reuses Livewire’s own browser runtime unchanged.

A component is a Rust struct whose fields cross the wire, plus an inherent implementation that publishes its actions:

ⓘ
use livewire::{LivewireComponent, component};

#[derive(Default, LivewireComponent)]
#[livewire(view = "livewire.counter")]
struct Counter {
    count: i32,
    #[livewire(locked)]
    id: String,
}

#[component]
impl Counter {
    fn mount(&mut self, count: Option<i32>) {
        self.count = count.unwrap_or(0);
        self.id = livewire::str::random(20);
    }

    fn increment(&mut self) {
        self.count += 1;
    }
}

The application registers its components once, in the provider:

ⓘ
use livewire::LivewireServiceProvider;

laravel::Application::configure().with_providers(|providers| {
    providers.add(LivewireServiceProvider::new(|components| {
        components.register::<Counter>("counter");
    }));
});

A page renders a component with livewire; a component that should render the whole document is mounted by its route with page::render_page.

ModuleHolds
componentThe authoring contract: Component, LivewireState, and the call envelope
snapshotThe signed wire envelope and its checksum
contextOne request’s mutable Livewire state, kept out of statics
engineThe .livewire template engine
registryThe components, synthesizers, and hooks an application registered

Re-exports§

pub use self::component::Call;
pub use self::component::Component;
pub use self::component::ComponentContext;
pub use self::component::ComponentView;
pub use self::component::LivewireState;
pub use self::component::Params;
pub use self::config::LivewireConfig;
pub use self::provider::LivewireServiceProvider;
pub use self::registry::ComponentRegistry;
pub use self::component::Result;
pub use self::error::LivewireError;

Modules§

assets
The browser runtime an application serves: its bundle bytes, the tags that load them, and the inline CSS they need.
checksum
The checksum that proves a snapshot reached the server as the server sent it, and the rate limit that stops a client which keeps sending corrupt ones.
commands
The console commands the package registers, starting with make:livewire.
compile
Turn a component’s .livewire source into the Jinja template the engine renders.
component
The authoring contract: what a component is, what it renders, and how the browser’s calls reach it.
config
The livewire config section and its defaults, which mirror config/livewire.php in upstream Livewire 4.x.
context
The request’s Livewire state, reached through the framework’s context instead of a static.
engine
The .livewire template engine: MiniJinja over the component’s compiled source.
error
The errors the Livewire protocol reports, with the status and message upstream Livewire 4.x answers with.
frontend
The application-specific paths under which every Livewire endpoint is served.
handlers
The endpoints the browser calls: the update handler, and the built runtime it loads.
hooks
Lifecycle hooks: one place to observe every mount, update, and action.
html
Injecting Livewire’s wire attributes into rendered component HTML.
jinja_helpers
The framework’s template helpers, added to the MiniJinja environment that renders .livewire templates.
page
Page components: a component whose view renders the whole document.
persistent
Guards that run when the browser drives a component a page mounted.
provider
The service provider an application adds to use Livewire.
registry
The components, synthesizers, and template hooks an application registers once, frozen into one shared value before the application boots.
render
Mounting, rendering, and updating a component.
routes
The routes the package registers: the browser’s update endpoint, and the built runtime it loads.
session
The session access the server half needs synchronously, and the keys session-backed properties persist under.
snapshot
One component’s signed wire envelope: the state it carries between requests and the checksum that proves the browser did not change it.
str
The string helpers the component IDs use, from the framework. String helpers, random strings, and identifiers.
synthesizers
The synthesizers that read and write a field whose wire form differs from its Rust form.
testing
A harness that drives a component through the same mount and update pipeline the endpoint uses, without HTTP.
uploads
Local file uploads: a signed URL, a temporary directory, and the calls the browser makes.

Attribute Macros§

component
Publishes an implementation’s lifecycle hooks and actions to the browser.

Derive Macros§

LivewireComponent
Makes a struct a Livewire component’s state: its wire-public fields, their rules, and the view it renders by default.