Expand description
Spark — Livewire-equivalent reactive components for Anvilforge.
Components are server-rendered structs whose state is HMAC-signed (or
AES-256-GCM-encrypted) and embedded in the DOM as a snapshot. The browser
ships the snapshot back on every interaction (spark:click, spark:model,
…); the server hydrates, dispatches the action, re-renders, and returns
refreshed HTML + a fresh snapshot. The runtime JS morphs the DOM in place.
Quickstart:
ⓘ
// app/Spark/Counter.rs
use anvilforge::prelude::*;
use spark::prelude::*;
#[spark_component(template = "spark/counter")]
pub struct Counter {
pub count: i32,
#[spark(model)] pub draft: String,
}
#[spark_actions]
impl Counter {
async fn increment(&mut self) -> Result<()> { self.count += 1; Ok(()) }
}ⓘ
// bootstrap/app.rs
Application::builder()
.web(spark::install(routes::web::register))
.build();Re-exports§
pub use broadcast::broadcast;pub use broadcast::SparkBroadcast;pub use component::Component;pub use component::Ctx;pub use component::MountProps;pub use component::PropertyWrite;pub use error::Error;pub use error::Result;pub use install::ensure_bellows_bound;pub use install::install;pub use install::install_routes;pub use registry::BoxedComponent;pub use registry::ComponentEntry;pub use registry::DynComponent;pub use render::boot_script;pub use render::render_mount;pub use ::async_trait;pub use ::futures;pub use ::inventory;pub use ::serde;pub use ::serde_json;
Modules§
- broadcast
- Server-side broadcasting helper that bridges into Bellows so Spark components
subscribed via
#[spark::on("event")]receive updates. - component
- Component trait + per-call context types.
- crypto
- Snapshot signing and encryption.
- error
- Spark error type.
- http
- HTTP handlers for Spark: the
/_spark/updateround-trip endpoint and the/_spark/spark.jsruntime asset. - install
- Install hook for Anvil’s
ApplicationBuilder. - middleware
- Per-request middleware: opens a Spark scope (mount buffer + CSRF binding)
around the handler future so
@sparkmounts in templates can be drained by@sparkScripts, and the boot script can include the request’s CSRF token. - morph
- Island slicing — extract
spark:island="name"subtrees from rendered HTML so the server can return only the changed region ineffects.islands. - prelude
- Spark prelude — items components and apps typically need.
- registry
- Component registry — inventory-driven lookup tables.
- render
- Server-side mount rendering: produces the
<div spark:id="..." spark:snapshot="...">…</div>wrapper that the JS runtime hydrates on page load. - request
- Wire types for
POST /_spark/updaterequest bodies. - response
- Wire types for
POST /_spark/updateresponse bodies. - snapshot
- Spark snapshot — the encoded, signed state envelope that lives on the page.
- template
- Runtime template engine for Spark component bodies.
Functions§
- const_
eq - Constant-time byte equality, used by snapshot::verify.