Current section
Files
Jump to
Current section
Files
src/lightspeed/component.gleam
//// Component contract for Lightspeed applications.
import lightspeed/agent/isa.{type Instruction}
/// Rendered output from a component.
///
/// The first implementation stores HTML as a string. Later versions may
/// replace this with a renderer-neutral tree or static/dynamic segment model.
pub type Rendered {
Rendered(html: String, fingerprint: String)
}
/// Commands returned from component updates.
pub type Command(msg) {
NoCommand
Dispatch(msg)
Push(Instruction)
}
/// Minimal state/update/render component contract.
pub type Component(model, msg) {
Component(
init: fn() -> model,
update: fn(model, msg) -> #(model, List(Command(msg))),
render: fn(model) -> Rendered,
)
}
/// Construct rendered HTML with a development fingerprint.
pub fn html(markup: String) -> Rendered {
Rendered(html: markup, fingerprint: "dev")
}
/// Extract HTML from a rendered value.
pub fn to_html(rendered: Rendered) -> String {
rendered.html
}
/// Extract the static-shape fingerprint from a rendered value.
pub fn fingerprint(rendered: Rendered) -> String {
rendered.fingerprint
}