From 86bd06f568dff0c46fc470e17880ecba4f9f4ef5 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 14 Jul 2026 12:48:25 -0400 Subject: [PATCH] Tokio app structure --- Cargo.lock | 18 ++++++++++++++++++ pulse-ui/Cargo.toml | 1 + pulse-ui/src/lib.rs | 43 +++++++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 61f686d..93fa691 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + [[package]] name = "pulse-trader" version = "0.1.0-alpha.0" @@ -12,3 +18,15 @@ dependencies = [ [[package]] name = "pulse-ui" version = "0.1.0" +dependencies = [ + "tokio", +] + +[[package]] +name = "tokio" +version = "1.52.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc7f01b389ac15039e4dc9531aa973a135d7a4135281b12d7c1bc79fd57fffe" +dependencies = [ + "pin-project-lite", +] diff --git a/pulse-ui/Cargo.toml b/pulse-ui/Cargo.toml index 96d7e5d..9d098b2 100644 --- a/pulse-ui/Cargo.toml +++ b/pulse-ui/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +tokio = { version = "1.52.3", features = ["sync"] } diff --git a/pulse-ui/src/lib.rs b/pulse-ui/src/lib.rs index 14e8bc5..5725779 100644 --- a/pulse-ui/src/lib.rs +++ b/pulse-ui/src/lib.rs @@ -1,31 +1,46 @@ +use std::any::Any; + +use tokio::sync::mpsc; + pub mod layout; pub mod render; pub mod unit; pub mod widget; +#[derive(Debug, Clone)] +pub struct Context { + tx: mpsc::Sender>, +} + +pub struct Refresh; + pub trait App { // State - fn init(&mut self); - fn update(&mut self); - fn tick(&self); + #[allow(async_fn_in_trait)] + async fn init(&mut self, ctx: Context); + #[allow(async_fn_in_trait)] + async fn update(&mut self, event: Box); // Rendering fn layout(&self) -> layout::Allocation; fn render(&mut self, layout: layout::Allocation); +} - fn refresh(&mut self) { - self.render(self.layout()); - } +pub struct AppHandler { + pub app: A, +} - // Running - fn run(&mut self) { - self.init(); - self.refresh(); +impl AppHandler { + pub async fn run(&mut self) { + let (tx, mut rx) = mpsc::channel(100); - loop { - self.tick(); - self.update(); - self.refresh(); + self.app.init(Context { tx }).await; + + self.app.render(self.app.layout()); + + while let Some(event) = rx.recv().await { + self.app.update(event).await; + self.app.render(self.app.layout()); } } }