lightspeed/component/stateful
Stateful component lifecycle and event routing compatibility helpers.
Types
One named event route for a component.
pub type EventRoute(msg) {
EventRoute(
name: String,
decode: fn(event.InboundEvent) -> Result(
msg,
event.DecodeError,
),
)
}
Constructors
-
EventRoute( name: String, decode: fn(event.InboundEvent) -> Result(msg, event.DecodeError), )
Instance
opaqueRunning stateful component instance.
pub opaque type Instance(model, assigns, msg)
Lifecycle phase at mount time.
pub type Lifecycle {
Disconnected
Connected
}
Constructors
-
Disconnected -
Connected
Stateful component contract comparable to LiveComponent lifecycle callbacks.
pub type LifecycleComponent(model, assigns, msg) {
LifecycleComponent(
mount: fn(MountContext, assigns) -> #(
model,
List(component.Command(msg)),
),
update: fn(model, assigns) -> #(
model,
List(component.Command(msg)),
),
handle: fn(model, msg) -> #(
model,
List(component.Command(msg)),
),
render: fn(model) -> component.Rendered,
routes: List(EventRoute(msg)),
)
}
Constructors
-
LifecycleComponent( mount: fn(MountContext, assigns) -> #( model, List(component.Command(msg)), ), update: fn(model, assigns) -> #( model, List(component.Command(msg)), ), handle: fn(model, msg) -> #(model, List(component.Command(msg))), render: fn(model) -> component.Rendered, routes: List(EventRoute(msg)), )
Values
pub fn context(
instance: Instance(model, assigns, msg),
) -> MountContext
Current mount context.
pub fn handle_event(
instance: Instance(model, assigns, msg),
inbound: event.InboundEvent,
) -> #(
Instance(model, assigns, msg),
Result(msg, event.DecodeError),
List(component.Command(msg)),
List(diff.Patch),
)
Route and handle one inbound event.
pub fn mount_context(
id: String,
route: String,
lifecycle: Lifecycle,
) -> MountContext
Build a mount context.
pub fn rendered(
instance: Instance(model, assigns, msg),
) -> component.Rendered
Last rendered value.
pub fn route(
name: String,
decode: fn(event.InboundEvent) -> Result(msg, event.DecodeError),
) -> EventRoute(msg)
Build one event route.
pub fn route_event(
inbound: event.InboundEvent,
routes: List(EventRoute(msg)),
) -> Result(msg, event.DecodeError)
Resolve an inbound event against named routes.
pub fn start(
definition: LifecycleComponent(model, assigns, msg),
context: MountContext,
assigns: assigns,
target: String,
) -> #(
Instance(model, assigns, msg),
List(component.Command(msg)),
List(diff.Patch),
)
Start a stateful component instance from mount assigns.
Returns initial commands and one deterministic replace patch.
pub fn update(
instance: Instance(model, assigns, msg),
assigns: assigns,
) -> #(
Instance(model, assigns, msg),
List(component.Command(msg)),
List(diff.Patch),
)
Apply parent assigns updates.