Crate squeak

Source
Expand description

This library provides types allowing execution of callbacks in response to values being broadcast or mutated.


§Manually broadcast values

Delegates can be used to manually broadcast values to subscribers:

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"

§Automatically broadcast when a variable is mutated

Observables own a value and execute callbacks whenever the value is mutated:


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"

Structs§

Delegate
Maintains a list of callbacks that can be explicitely triggered by calling Delegate::broadcast.
Observable
Wrapper type which owns a value and executes callbacks every time a call is made to mutate the value.
Subscription
Represents a subscription created via Delegate::subscribe or Observable::subscribe.

Enums§

Response
Returned by Delegate and Observable subscription callbacks. Depending on the value returned, the subscription will stay active or be cancelled.