Crates.io | squeak |
lib.rs | squeak |
version | 0.2.0 |
source | src |
created_at | 2022-12-04 01:17:52.392867 |
updated_at | 2023-12-26 20:58:46.457219 |
description | Library providing types allowing execution of callbacks in response to values being broadcast or mutated. |
homepage | |
repository | https://github.com/agersant/squeak |
max_upload_size | |
id | 729350 |
size | 32,821 |
Squeak is a zero-dependency Rust library to facilitate event-driven programming.
use squeak::{Delegate, Response};
let on_damage_received = Delegate::new();
on_damage_received.subscribe(|amount| {
println!("Received {amount} damage");
Response::StaySubscribed
});
on_damage_received.broadcast(16); // Prints "Received 16 damage"
on_damage_received.broadcast(14); // Prints "Received 14 damage"
on_damage_received.broadcast(28); // Prints "Received 28 damage"
use squeak::{Observable, Response};
let mut health = Observable::new(100);
health.subscribe(|updated_health| {
println!("Health is now {updated_health}");
Response::StaySubscribed
});
health.mutate(|h| *h -= 10); // Prints "Health is now 90"
health.mutate(|h| *h -= 5); // Prints "Health is now 85"
health.mutate(|h| *h += 25); // Prints "Health is now 110"