Crates.io | hash_events |
lib.rs | hash_events |
version | |
source | src |
created_at | 2024-04-15 17:03:13.985052 |
updated_at | 2024-12-03 09:25:46.943274 |
description | An MPMC event system that allows for custom events. Events are emitted with a payload, and subscribers to that event are stream of payloads. For each event, all subscribers are triggered. |
homepage | |
repository | https://gitlab.com/CreativeStoic/hash_events.git |
max_upload_size | |
id | 1209454 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
An MPMC event system that allows for custom events. Events are emitted with a payload, and subscribers to that event are stream of payloads. For each event, all subscribers are triggered.
use hash_events::EventManager;
use futures_lite::StreamExt;
use futures::executor::block_on;
// First, create an `EventManager` using a custom event, or the built-in `StringEvent`.
#[derive(Debug, PartialEq, Eq, Hash)]
struct SimpleEvent(String);
let mut manager: EventManager<SimpleEvent, i32> = EventManager::new();
// Then subscribe to an event. You can listen for all events, or just one. Subscriber streams can be cloned.
let mut subscriber = manager.subscribe(SimpleEvent("Event".into()));
let mut subscriber_once = manager.once(SimpleEvent("Event2".into()));
let mut subscriber2 = subscriber.clone();
// Then events can be emitted from the event manger or an event emitter.
manager.emit(SimpleEvent("Event".into()), 57);
let mut emitter = manager.emitter(SimpleEvent("Event2".into()));
emitter.emit(56);
// The subscribers will receive payloads for matching events.
assert_eq!(block_on(subscriber.next()), Some(57));
assert_eq!(block_on(subscriber2.next()), Some(57));
assert_eq!(block_on(subscriber_once), Ok(56));