From f929fd8fe57f078b36857fe3c8ce6025896ae3d6 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 15 May 2024 14:50:01 +0200 Subject: [PATCH] lib: Encapsulate application setup --- src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ui.rs | 25 ++++++++++++++----------- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c49ae37a..43027b7e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,15 @@ pub mod task; pub mod terminal; pub mod ui; +use std::fmt::Debug; + use anyhow::Result; use serde::ser::{Serialize, SerializeStruct, Serializer}; +use store::State; +use task::Interrupted; +use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; +use ui::{widget::Widget, Frontend}; /// An optional return value. #[derive(Clone, Debug)] @@ -97,3 +103,45 @@ impl PageStack { } } } + +/// A multi-producer, single-consumer message channel. +pub struct Channel { + pub tx: UnboundedSender, + pub rx: UnboundedReceiver, +} + +impl Default for Channel { + fn default() -> Self { + let (tx, rx) = mpsc::unbounded_channel(); + Self { tx: tx.clone(), rx } + } +} + +/// Initialize a `Store` with the `State` given and a `Frontend` with the `Widget` given, +/// and run their main loops concurrently. Connect them to the `Channel` and also to +/// an interrupt broadcast channel also initialized in this function. +pub async fn run(channel: Channel, state: S, root: W) -> Result> +where + S: State + Clone + Debug + Send + Sync + 'static, + W: Widget, + P: Clone + Debug + Send + Sync + 'static, +{ + let (terminator, mut interrupt_rx) = task::create_termination(); + + let (store, state_rx) = store::Store::::new(); + let frontend = Frontend::::new(channel.tx.clone()); + + tokio::try_join!( + store.main_loop(state, terminator, channel.rx, interrupt_rx.resubscribe()), + frontend.main_loop(Some(root), state_rx, interrupt_rx.resubscribe()), + )?; + + if let Ok(reason) = interrupt_rx.recv().await { + match reason { + Interrupted::User { payload } => Ok(payload), + Interrupted::OsSignal => anyhow::bail!("exited because of an os sig int"), + } + } else { + anyhow::bail!("exited because of an unexpected error"); + } +} diff --git a/src/ui.rs b/src/ui.rs index b5cdbdb2..e7940daf 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -10,7 +10,8 @@ use std::fmt::Debug; use std::time::Duration; use tokio::sync::broadcast; -use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; +use tokio::sync::mpsc; +use tokio::sync::mpsc::UnboundedReceiver; use crate::ui::widget::RenderProps; @@ -34,16 +35,10 @@ pub struct Frontend { impl Frontend { /// Create a new `Frontend` storing the sending end of a message channel. - pub fn new() -> (Self, UnboundedSender, UnboundedReceiver) { - let (action_tx, action_rx) = mpsc::unbounded_channel(); - - ( - Self { - action_tx: action_tx.clone(), - }, - action_tx, - action_rx, - ) + pub fn new(action_tx: mpsc::UnboundedSender) -> Self { + Self { + action_tx: action_tx.clone(), + } } /// By calling `main_loop`, the `Frontend` will wait for new messages being sent @@ -115,4 +110,12 @@ impl Frontend { result } + + // pub fn action_tx(&self) -> UnboundedSender { + // self.action_tx.clone() + // } + + // pub fn action_rx(&self) -> UnboundedReceiver { + // self.action_rx.clone() + // } }