dbuff 0.1.0

Double-buffered state with async command chains, streaming, and keyed task pools for ratatui applications
Documentation
use dbuff::*;
use std::time::Duration;

#[derive(Debug, Clone, Default)]
struct AppData {
    total: i32,
    log: Vec<String>,
}

#[derive(Debug, Clone, wherror::Error)]
#[error(debug)]
struct CmdError;

struct Add(i32);

#[async_trait::async_trait]
impl Command<()> for Add {
    type Output = i32;
    type Error = CmdError;

    async fn execute(self, _: ()) -> Result<Self::Output, Self::Error> {
        Ok(self.0)
    }
}

struct Log(String);

#[async_trait::async_trait]
impl Command<()> for Log {
    type Output = String;
    type Error = CmdError;

    async fn execute(self, _: ()) -> Result<Self::Output, Self::Error> {
        Ok(self.0)
    }
}

#[tokio::main]
async fn main() {
    let rt = tokio::runtime::Handle::current();
    let (domain, write_handle) = SharedDomainData::with_coalesce(
        AppData::default(),
        Duration::from_millis(1),
    );
    tokio::spawn(write_handle.run());

    // Chain: Add(10) → Add(20) → Log("done") → Add(5)
    // All four updates arrive within the coalesce window → single batch
    let handle = domain.bind((), rt.clone())
        .exec(Add(10), |d, v: &i32| d.total += *v)
        .exec(Add(20), |d, v: &i32| d.total += *v)
        .exec(Log("done".into()), |d, msg: &String| d.log.push(msg.clone()))
        .exec(Add(5), |d, v: &i32| d.total += *v)
        .go();

    let flow = handle.await.unwrap();
    assert_eq!(flow, ControlFlow::Continue);

    tokio::time::sleep(Duration::from_millis(10)).await;

    let guard = domain.read();
    assert_eq!(guard.total, 35);
    assert_eq!(guard.log, vec!["done"]);
    println!("total = {}, log = {:?}", guard.total, guard.log);
}