#可观察 #事件 #委托 #响应式 #无std

无std squeak

提供类型库,允许在值被广播或修改时执行回调

2个不稳定版本

0.2.0 2023年12月26日
0.1.0 2022年12月4日

#1156Rust模式

MIT/Apache

15KB
132

Squeak

build_badge crates.io_badge docs_badge

Squeak是一个无依赖的Rust库,用于简化事件驱动编程。

示例

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"

无运行时依赖