4 releases
0.1.3 | Jun 24, 2024 |
---|---|
0.1.2 | Jun 20, 2024 |
0.1.1 | Jun 20, 2024 |
0.1.0 | Jun 18, 2024 |
#411 in GUI
36KB
471 lines
korhah
![docs.rs](https://img.shields.io/docsrs/korhah)
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.
Example Usage
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.
(Un)Sync
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.
MSRV
The minimum supported Rust version is 1.63.0.
Dependencies
~1.8–2.6MB
~45K SLoC