chore: update docs

This commit is contained in:
JasterV 2025-08-19 22:33:11 +02:00
parent d439efd2a3
commit 520afe7183
5 changed files with 45 additions and 10 deletions

View file

@ -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 color_eyre::Result;
use std::sync::mpsc::Receiver; use std::sync::mpsc::Receiver;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
pub fn run<M, Msg, Eff, F>(effects_fn: F, rx: Receiver<(M, Eff)>, tx: Sender<Msg>) -> Result<()> pub(crate) fn run<M, Msg, Eff, F>(
effects_fn: F,
rx: Receiver<(M, Eff)>,
tx: Sender<Msg>,
) -> Result<()>
where where
Msg: Send + Sync + 'static, Msg: Send + Sync + 'static,
F: Fn(&M, Eff) -> Result<Option<Msg>>, F: Fn(&M, Eff) -> Result<Option<Msg>>,

View file

@ -3,7 +3,7 @@ use color_eyre::Result;
use crossterm::event; use crossterm::event;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
pub fn run<M>(tx: Sender<M>) -> Result<()> pub(crate) fn run<M>(tx: Sender<M>) -> Result<()>
where where
M: From<crossterm::event::Event> + Sync + Send + 'static, M: From<crossterm::event::Event> + Sync + Send + 'static,
{ {

View file

@ -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. //! 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: //! The users of this framework only need to provide:
//! //!
//! - An update function that given a model and a message return an `Update` instance. //! - 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` //! - 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, //! - 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 //! 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::Report;
use color_eyre::Result; use color_eyre::Result;
use std::{ use std::{
sync::mpsc::{Sender, channel}, sync::mpsc::{Sender, channel},
thread, thread,
}; };
pub use update::Update; pub use update::Update;
pub use view::View; pub use view::View;
@ -39,6 +46,18 @@ mod events;
mod update; mod update;
mod view; 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<M, Msg, Eff, UF, VF, EF>( pub fn start<M, Msg, Eff, UF, VF, EF>(
model: M, model: M,
update_fn: UF, update_fn: UF,

View file

@ -2,13 +2,20 @@
use color_eyre::{Report, Result}; use color_eyre::{Report, Result};
use std::sync::mpsc::{Receiver, Sender}; 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<M, E> { pub enum Update<M, E> {
Exit, Exit,
Next(M), Next(M),
NextWithEffect(M, E), NextWithEffect(M, E),
} }
pub fn run<M, Msg, Eff, F>( pub(crate) fn run<M, Msg, Eff, F>(
mut model: M, mut model: M,
update_fn: F, update_fn: F,
rx: Receiver<Msg>, rx: Receiver<Msg>,
@ -33,13 +40,15 @@ where
Update::NextWithEffect(new_model, effect) => (new_model, Some(effect)), 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 { if let Some(effect) = effect {
effects_tx.send((new_model.clone(), effect))?; effects_tx.send((new_model.clone(), effect))?;
} }
// Send the updated version of the model
view_tx.send(new_model.clone())?;
model = new_model; model = new_model;
} }
} }

View file

@ -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 color_eyre::Result;
use ratatui::DefaultTerminal; use ratatui::DefaultTerminal;
use ratatui::widgets::Widget; use ratatui::widgets::Widget;
use ratatui::widgets::WidgetRef; use ratatui::widgets::WidgetRef;
use std::sync::mpsc::Receiver; 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<dyn WidgetRef>); pub struct View(Box<dyn WidgetRef>);
impl View { impl View {
@ -19,7 +22,7 @@ impl Widget for View {
} }
} }
pub fn run<M, F>( pub(crate) fn run<M, F>(
mut model: M, mut model: M,
mut terminal: DefaultTerminal, mut terminal: DefaultTerminal,
view_fn: F, view_fn: F,