| Crates.io | sigwake |
| lib.rs | sigwake |
| version | 0.0.1 |
| created_at | 2025-03-08 02:48:44.148038+00 |
| updated_at | 2025-03-08 02:48:44.148038+00 |
| description | A thread-safe signal-based state management library that integrates with Rust's async programming model |
| homepage | |
| repository | https://github.com/frozenlib/sigwake |
| max_upload_size | |
| id | 1584000 |
| size | 114,560 |
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.
async/await, Future, Waker)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
This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.
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.