dbuff 0.1.0

Double-buffered state with async command chains, streaming, and keyed task pools for ratatui applications
Documentation
use dbuff::*;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;

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

struct Add(i32);

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

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

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

struct Fail;

#[async_trait::async_trait]
impl Command<()> for Fail {
    type Output = ();
    type Error = FailError;

    async fn execute(self, _: ()) -> Result<Self::Output, Self::Error> {
        Err(FailError)
    }
}

#[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());

    let error_called = Arc::new(AtomicBool::new(false));
    let flag = error_called.clone();

    // Chain: Add(10) → Add(20) → Fail → Add(5)
    // Fail causes short-circuit, so Add(5) is skipped
    let handle = domain.bind((), rt.clone())
        .on_error(move |err, d| {
            flag.store(true, Ordering::SeqCst);
            d.error_log.push(format!("{err}"));
        })
        .exec(Add(10), |d, v: &i32| d.total += *v)
        .exec(Add(20), |d, v: &i32| d.total += *v)
        .exec_discard(Fail)
        .exec(Add(5), |d, v: &i32| d.total += *v)
        .go();

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

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

    assert!(error_called.load(Ordering::SeqCst));

    let guard = domain.read();
    // Only Add(10) + Add(20) applied; Add(5) was skipped
    assert_eq!(guard.total, 30);
    assert_eq!(guard.error_log.len(), 1);
    println!("total = {}, error_log = {:?}", guard.total, guard.error_log);
}