Skip to main content

oxide_mvu/
logic.rs

1//! Application logic trait defining the MVU contract.
2
3use crate::Event as EventTrait;
4use crate::{Effect, Emitter};
5
6/// Application logic trait defining the MVU contract.
7///
8/// Implementations must provide three pure functions:
9/// - [`init`](Self::init): Initialize the model and produce initial effects
10/// - [`update`](Self::update): Transform (Event, Model) → (Model, Effect)
11/// - [`view`](Self::view): Derive Props from Model with event emitter capability
12///
13/// See the [crate-level documentation](crate) for a complete example.
14pub trait MvuLogic<Event: EventTrait, Model, Props> {
15    /// Initialize the runtime from an initial model with effects and state changes as needed.
16    ///
17    /// This is called once when the runtime starts. Use it to set up initial
18    /// state and trigger any bootstrap events.
19    ///
20    /// # Arguments
21    ///
22    /// * `model` - The initial model state
23    ///
24    /// # Returns
25    ///
26    /// A tuple of `(Model, Effect<Event>)` containing the initialized model
27    /// and any effects to process during startup.
28    fn init(&self, model: Model) -> (Model, Effect<Event>);
29
30    /// Reduce an event to an updated model and side effects.
31    ///
32    /// This function takes an event and the current model, returning
33    /// the new model and any effects to process. All state changes must
34    /// happen through this function.
35    ///
36    /// # Arguments
37    ///
38    /// * `event` - The event to process
39    /// * `model` - The current model state
40    ///
41    /// # Returns
42    ///
43    /// A tuple of `(Model, Effect<Event>)` containing the updated model
44    /// and any effects to process.
45    fn update(&self, event: Event, model: &Model) -> (Model, Effect<Event>);
46
47    /// Reduce to Props from the current model.
48    ///
49    /// This function creates a renderable representation (Props) from
50    /// the model. The provided [`Emitter`] allows Props to contain callbacks
51    /// that can trigger new events.
52    ///
53    /// # Arguments
54    ///
55    /// * `model` - The current model state
56    /// * `emitter` - Event emitter for creating callbacks
57    ///
58    /// # Returns
59    ///
60    /// Props derived from the model, ready for rendering via [`Renderer::render`](crate::Renderer::render).
61    fn view(&self, model: &Model, emitter: &Emitter<Event>) -> Props;
62}