Crates.io | korhah |
lib.rs | korhah |
version | 0.1.3 |
source | src |
created_at | 2024-06-18 20:11:58.346518 |
updated_at | 2024-06-24 18:45:30.607201 |
description | A minimal & extensible reactive event system |
homepage | |
repository | https://github.com/konall/korhah |
max_upload_size | |
id | 1275953 |
size | 71,677 |
korhah
is a minimal & extensible reactive event system.
At its most basic, it allows users to register callback functions and emit arbitrary custom events which will trigger those functions.
It can also act as an object store, whose contents are accessible from within registered callback functions as well as from outside the system.
To keep it minimal, only basic CRUD operations are provided along with signal-y behaviour via automatic dependency-tracking.
To make it extensible, the built-in CRUD operations emit events that can be hooked into.
let mut system = korhah::System::default();
// create a simple variable in the reactive system
let a = system.create(|_, _| 0).expect("no cancelling listeners registered");
// create a signal-y variable that depends on `a`
let b = system.create(move |s, _| {
// the dependency on `a` is automatically tracked here
let a = s.read(a, |v| *v)
.expect("no cancelling listeners registered")
.expect("`a` exists");
// subsequent updates to `a` will recompute `b` according to this formula
a + 1
}).expect("no cancelling listeners registered");
// we can emit *any* 'static type we like as an event
struct CustomEvent {
n: i32,
}
// listen for our custom event being emitted in a "global" scope
// (the "global" scope being due to specifying a `None` target)
system.listen(None, move |s, e: &CustomEvent, _, _| {
// we'll update `a` to the associated event info
// (note that this should automatically update `b` too)
_ = s.update(a, |v| *v = e.n);
});
assert_eq!(Ok(Some(0)), system.read(a, |v| *v));
assert_eq!(Ok(Some(1)), system.read(b, |v| *v));
// emit our custom event
_ = system.emit(None, &CustomEvent { n: 42 });
assert_eq!(Ok(Some(42)), system.read(a, |v| *v));
assert_eq!(Ok(Some(43)), system.read(b, |v| *v));
no_std
This crate is compatible with no_std
environments, requiring only the alloc
crate.
This crate can be used in both single-threaded and multi-threaded environments.
In the spirit of cargo features being additive, the stricter Send
+ Sync
bounds of a multi-threaded environment are assumed by default for variable types and callback types, and these bounds can be relaxed for single-threaded environments via the unsync
feature.
The minimum supported Rust version is 1.63.0.