#state-management #reactive #async #signal #multi-threading

sigwake

A thread-safe signal-based state management library that integrates with Rust's async programming model

1 unstable release

new 0.0.1 Mar 8, 2025

#15 in #multithreading


Used in 2 crates (via jsoncall)

MIT/Apache

64KB
1.5K SLoC

sigwake

Crates.io Docs.rs Actions Status

A thread-safe state management library using signals

sigwake is a library that reinterprets signal-based state management (commonly used in modern web frameworks) through Rust's asynchronous programming model.

Features

  • Supports reactive programming with Signal-based dependency tracking from state access
  • Integration with Rust's asynchronous programming model (async/await, Future, Waker)
  • Thread-safe implementation
  • Runtime-agnostic implementation that can be used with any async runtime
  • Compact API

Example

use std::time::Duration;

use futures::StreamExt;
use sigwake::{StateContainer, state::Value};
use tokio::{spawn, time::sleep};

struct State {
    a: Value<i32>,
    b: Value<i32>,
    c: Value<i32>,
}

#[tokio::main]
async fn main() {
    let st = StateContainer::new(|cx| State {
        a: Value::new(0, cx),
        b: Value::new(0, cx),
        c: Value::new(0, cx),
    });

    // Stream that emits values whenever dependent values are updated
    let mut ac = st.subscribe(|st, cx| st.a.get(cx) + st.c.get(cx));
    spawn(async move {
        while let Some(value) = ac.next().await {
            println!("{value}");
        }
    });
    st.update(|st, cx| st.a.set(1, cx)); // Update a
    sleep(Duration::from_secs(1)).await;

    st.update(|st, cx| st.b.set(2, cx)); // Update b (ac is not recalculated)
    sleep(Duration::from_secs(1)).await;

    st.update(|st, cx| st.c.set(3, cx)); // Update c
    sleep(Duration::from_secs(1)).await;
}

Output:

1
4

License

This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1–1.6MB
~32K SLoC