plushie_web
JavaScript/WASM entry point for plushie applications.
This is the JS-target equivalent of plushie.gleam (which uses
OTP supervisors). It provides the user-facing API for starting
and controlling a plushie app in the browser via the WASM renderer.
Supported app types
Both app.simple() (msg = Event) and app.application()
(custom msg type via on_event) are supported.
Quick start (browser)
import plushie_web
import plushie/app
import plushie/command
import plushie/event.{type Event, WidgetClick}
import plushie/ui
import plushie/widget/window
import gleam/int
type Model { Model(count: Int) }
pub fn main() {
let counter = app.simple(
fn(_) { #(Model(0), command.none()) },
fn(model, event) {
case event {
WidgetClick(id: "inc", ..) ->
#(Model(model.count + 1), command.none())
_ -> #(model, command.none())
}
},
fn(model) {
ui.window("main", [window.Title("Counter")], [
ui.text_("count", "Count: " <> int.to_string(model.count)),
ui.button_("inc", "+"),
])
},
)
let assert Ok(instance) =
plushie_web.start(counter, plushie_web.default_start_opts())
}
Types
A running plushie application on the JavaScript target.
Parameterized over the model type for type-safe get_model.
Created by start, controlled with get_model, get_tree,
dispatch_event, and stop.
Errors that can occur when starting a web application.
pub type WebStartError {
WasmInitFailed(reason: String)
}
Constructors
-
WasmInitFailed(reason: String)The WASM renderer failed to initialize.
Options for starting a plushie web application.
pub type WebStartOpts {
WebStartOpts(session: String, app_opts: dynamic.Dynamic)
}
Constructors
-
WebStartOpts(session: String, app_opts: dynamic.Dynamic)Arguments
- session
-
Session identifier. Default: “” (single-session).
- app_opts
-
Application options passed to init/1. Default: dynamic.nil().
Values
pub fn default_start_opts() -> WebStartOpts
Default start options for the web target.
pub fn start_error_to_string(err: WebStartError) -> String
Format a start error as a human-readable string.