use dbuff::*;
use std::time::Duration;
#[derive(Debug, Clone, Default)]
struct AppData {
counter: i32,
}
#[derive(Debug, Clone, wherror::Error)]
#[error(debug)]
struct IncrError;
struct Increment;
#[async_trait::async_trait]
impl Command<()> for Increment {
type Output = i32;
type Error = IncrError;
async fn execute(self, _: ()) -> Result<Self::Output, Self::Error> {
Ok(1)
}
}
#[tokio::main]
async fn main() {
let rt = tokio::runtime::Handle::current();
let (domain, write_handle) =
SharedDomainData::with_coalesce(AppData::default(), Duration::from_micros(500));
tokio::spawn(write_handle.run());
domain.bind((), rt.clone())
.exec(Increment, |d, v: &i32| d.counter += *v)
.go_detach();
tokio::time::sleep(Duration::from_millis(10)).await;
let guard = domain.read();
assert_eq!(guard.counter, 1);
println!("counter = {}", guard.counter);
}