diff --git a/ratatui-elm/src/effects.rs b/ratatui-elm/src/effects.rs index a788f92..3f3d326 100644 --- a/ratatui-elm/src/effects.rs +++ b/ratatui-elm/src/effects.rs @@ -1,9 +1,13 @@ -// Actor responsible of processing side effects sent by the update actor. +//! Actor responsible of processing side effects sent by the update actor. use color_eyre::Result; use std::sync::mpsc::Receiver; use std::sync::mpsc::Sender; -pub fn run(effects_fn: F, rx: Receiver<(M, Eff)>, tx: Sender) -> Result<()> +pub(crate) fn run( + effects_fn: F, + rx: Receiver<(M, Eff)>, + tx: Sender, +) -> Result<()> where Msg: Send + Sync + 'static, F: Fn(&M, Eff) -> Result>, diff --git a/ratatui-elm/src/events.rs b/ratatui-elm/src/events.rs index ba6e12c..f11c872 100644 --- a/ratatui-elm/src/events.rs +++ b/ratatui-elm/src/events.rs @@ -3,7 +3,7 @@ use color_eyre::Result; use crossterm::event; use std::sync::mpsc::Sender; -pub fn run(tx: Sender) -> Result<()> +pub(crate) fn run(tx: Sender) -> Result<()> where M: From + Sync + Send + 'static, { diff --git a/ratatui-elm/src/lib.rs b/ratatui-elm/src/lib.rs index 14b6f44..160157a 100644 --- a/ratatui-elm/src/lib.rs +++ b/ratatui-elm/src/lib.rs @@ -1,4 +1,4 @@ -//! Elm-like framework based on Ratatui. +//! Elm-like framework implemented on top of [Ratatui](https://github.com/ratatui/ratatui). //! //! The state of your application is represented by a single type called the Model. //! @@ -22,15 +22,22 @@ //! The users of this framework only need to provide: //! //! - An update function that given a model and a message return an `Update` instance. +//! //! - A view function that given a reference to the model, returns a `View` +//! //! - An effects function that given a reference to the model and an effect, //! might perform any side effects and optionally return a message to update the state of the application +//! +//! ### Examples +//! +//! You can find a folder with example projects in the [examples](https://github.com/JasterV/ratatui-elm/tree/main/examples) folder. use color_eyre::Report; use color_eyre::Result; use std::{ sync::mpsc::{Sender, channel}, thread, }; + pub use update::Update; pub use view::View; @@ -39,6 +46,18 @@ mod events; mod update; mod view; +/// Starts the runtime which manages all the internal +/// processes and message passing. +/// +/// The user needs to provide: +/// +/// - The initial model +/// +/// - An `update` function, responsible for updating the model based on messages. +/// +/// - A `view` function, responsible for constructing the view from the model. +/// +/// - An `effects` function responsible for handling side effects. pub fn start( model: M, update_fn: UF, diff --git a/ratatui-elm/src/update.rs b/ratatui-elm/src/update.rs index 5b6381c..e6a0a7b 100644 --- a/ratatui-elm/src/update.rs +++ b/ratatui-elm/src/update.rs @@ -2,13 +2,20 @@ use color_eyre::{Report, Result}; use std::sync::mpsc::{Receiver, Sender}; +/// Tells the runtime what to do with the previous message. +/// +/// If `Update::Exit` is returned, the program will exit. +/// +/// If `Update::Next(M)` is returned, the view will be rendered with the new model. +/// +/// If `Update::NextWithEffect` is returned, the view will be rendered with the new model and a side effect will be executed. pub enum Update { Exit, Next(M), NextWithEffect(M, E), } -pub fn run( +pub(crate) fn run( mut model: M, update_fn: F, rx: Receiver, @@ -33,13 +40,15 @@ where Update::NextWithEffect(new_model, effect) => (new_model, Some(effect)), }; + // Send the new model to the view + view_tx.send(new_model.clone())?; + + // After the view is notified of the new model, + // execute side effects if any if let Some(effect) = effect { effects_tx.send((new_model.clone(), effect))?; } - // Send the updated version of the model - view_tx.send(new_model.clone())?; - model = new_model; } } diff --git a/ratatui-elm/src/view.rs b/ratatui-elm/src/view.rs index d057d4e..3beda34 100644 --- a/ratatui-elm/src/view.rs +++ b/ratatui-elm/src/view.rs @@ -1,10 +1,13 @@ -// Actor responsible of rendering the model into the terminal. +//! Actor responsible of rendering the model into the terminal. use color_eyre::Result; use ratatui::DefaultTerminal; use ratatui::widgets::Widget; use ratatui::widgets::WidgetRef; use std::sync::mpsc::Receiver; +/// A thin wrapper around a `ratatui` WidgetRef. +/// It is guaranteed that it will always be possible +/// to construct it from a Widget. pub struct View(Box); impl View { @@ -19,7 +22,7 @@ impl Widget for View { } } -pub fn run( +pub(crate) fn run( mut model: M, mut terminal: DefaultTerminal, view_fn: F,