shelly-liveview 0.1.0

Core runtime primitives for Shelly LiveView.
Documentation
use crate::{ComponentRender, Context, Event, Html, LiveResult};

/// Trait implemented by server-owned interactive views.
///
/// A LiveView owns its state. Browser events are delivered through
/// `handle_event`, and the resulting HTML is returned by `render`.
pub trait LiveView: Send + 'static {
    /// Called before the first render for a given session.
    fn mount(&mut self, _ctx: &mut Context) -> LiveResult {
        Ok(())
    }

    /// Called after mount and after internal URL patches update route params.
    fn handle_params(&mut self, _ctx: &mut Context) -> LiveResult {
        Ok(())
    }

    /// Called when the browser sends an event.
    fn handle_event(&mut self, _event: Event, _ctx: &mut Context) -> LiveResult {
        Ok(())
    }

    /// Called when an event has a target that may identify a child component.
    ///
    /// Returning `Some(ComponentRender)` tells the session runtime to patch only
    /// that component id. Returning `None` falls back to normal view handling.
    fn handle_component_event(
        &mut self,
        _target: &str,
        _event: Event,
        _ctx: &mut Context,
    ) -> LiveResult<Option<ComponentRender>> {
        Ok(None)
    }

    /// Render this view into an HTML fragment.
    fn render(&self) -> Html;
}