Expand description
The BLoC primitives behind rok_ui::bloc, without GPUI.
A bloc turns events into states; a cubit exposes methods that emit states. Both keep
their state in an Emitter, which notifies subscribers only when the state changes.
Handlers run on a tokio runtime (in rok-ui apps, rok_ui::runtime), and every handler is
cancelled when its bloc closes.
use rok_ui_bloc::{Bloc, BlocHandle, Emitter};
#[derive(Clone, Debug, PartialEq)]
struct Count(u32);
#[derive(Debug)]
enum CounterEvent {
Incremented,
}
struct CounterBloc;
impl Bloc for CounterBloc {
type Event = CounterEvent;
type State = Count;
fn initial_state(&self) -> Count {
Count(0)
}
async fn on(&self, event: CounterEvent, emit: &Emitter<Count>) {
match event {
CounterEvent::Incremented => {
emit.update(|count| count.0 += 1);
}
}
}
}
let states = rok_ui_bloc::test::run(CounterBloc, [CounterEvent::Incremented, CounterEvent::Incremented]);
assert_eq!(states, [Count(1), Count(2)]);Modules§
- test
- Test helpers: events (or cubit calls) in, states out, like
bloc_test.
Macros§
- bloc_
test - A test for a bloc: build it, add events, and compare the states it emitted (
bloc_test).
Structs§
- Bloc
Handle - A running bloc: add events, read the state, close it. Cheap to clone; views get one from
cx.bloc::<B>(). - Cubit
Handle - A running cubit. Derefs to the cubit, so views call its methods directly. Not
Send, likeBlocHandle. - Emitter
- The current state of a bloc or cubit. Handlers and cubit methods call
Emitter::emit; subscribers (views, listeners, tests) hear about each state that differs from the last. - LogObserver
- Prints every event, change and error to standard error: a quick way to see what the app
does.
set_observer(LogObserver). - Subscription
- A subscription to state changes; dropping it unsubscribes.
Enums§
- Concurrency
- How a bloc handles an event that arrives while others run (
bloc_concurrency’s transformers). Picked per event byBloc::concurrency.
Traits§
- Bloc
- Business logic that turns events into states (the BLoC pattern).
- Bloc
Observer - Sees what every bloc and cubit does. Set one for the process with
set_observer; every method has an empty default, so implement only what you need. - Cubit
- A bloc without events: methods change the state through the cubit’s
Emitter. Use it when the logic is a few direct actions (a counter, a toggle, a filter); use aBlocwhen events need concurrency control or a log. - Observable
- Something views can show and listen to: a
BlocHandleor aCubitHandle.
Functions§
- clear_
observer - Remove the observer.
- set_
observer - Make
observersee every bloc and cubit in the process, replacing the previous one. Set it once, at startup.
Type Aliases§
- BoxFuture
- A boxed, sendable future: what repository traits return, since
async fnin a trait used asdyn Traitis not object-safe.