Skip to main content

App

Trait App 

Source
pub trait App: Sized + 'static {
    type Model: Send + 'static;
    type Msg: Clone + Send + 'static;

    // Required methods
    fn init() -> (Self::Model, Cmd<Self::Msg>);
    fn update(model: &mut Self::Model, msg: Self::Msg) -> Cmd<Self::Msg>;
    fn view(model: &Self::Model, ctx: &mut ViewCtx<'_, Self::Msg>);

    // Provided methods
    fn subscriptions(_model: &Self::Model) -> Sub<Self::Msg> { ... }
    fn on_framework_error(
        _model: &mut Self::Model,
        err: FrameworkError,
    ) -> Cmd<Self::Msg> { ... }
}
Expand description

The main application trait following TEA (The Elm Architecture)

§Type Parameters

  • Model: Application state
  • Msg: Message type for state updates

§Example

struct MyApp;

impl App for MyApp {
    type Model = AppModel;
    type Msg = AppMsg;

    fn init() -> (Self::Model, Cmd<Self::Msg>) {
        (AppModel::default(), Cmd::none())
    }

    fn update(model: &mut Self::Model, msg: Self::Msg) -> Cmd<Self::Msg> {
        match msg {
            AppMsg::Increment => model.count += 1,
            AppMsg::Decrement => model.count -= 1,
        }
        Cmd::none()
    }

    fn view(model: &Self::Model, ctx: &mut ViewCtx<Self::Msg>) {
        ctx.ui.label(format!("Count: {}", model.count));
    }
}

Required Associated Types§

Source

type Model: Send + 'static

Application state

Source

type Msg: Clone + Send + 'static

Message type for triggering state updates

Required Methods§

Source

fn init() -> (Self::Model, Cmd<Self::Msg>)

Initialize the application with initial model and optional commands

Source

fn update(model: &mut Self::Model, msg: Self::Msg) -> Cmd<Self::Msg>

Update the model based on a message, optionally returning commands

Source

fn view(model: &Self::Model, ctx: &mut ViewCtx<'_, Self::Msg>)

Render the view - use ctx.emit() to dispatch messages

Provided Methods§

Source

fn subscriptions(_model: &Self::Model) -> Sub<Self::Msg>

Declare subscriptions based on current model state

Called each frame. The runtime manages starting/stopping subscriptions as they appear or disappear.

§Example
fn subscriptions(model: &Model) -> Sub<Msg> {
    if model.auto_refresh {
        Sub::interval("refresh", Duration::from_secs(30), Msg::Refresh)
    } else {
        Sub::none()
    }
}
Source

fn on_framework_error( _model: &mut Self::Model, err: FrameworkError, ) -> Cmd<Self::Msg>

Handle framework errors

Called when the framework encounters an internal error (task panic, runtime failure, etc.). Override this to integrate with your app’s error handling.

Default implementation logs to tracing and continues.

§Example
fn on_framework_error(model: &mut Model, err: FrameworkError) -> Cmd<Msg> {
    // Add to your error console
    model.errors.push_with_level(&err.message, err.severity.into());

    // Or convert to your app's error type
    Cmd::msg(Msg::Error(err.format_message()))
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§