esp-js
The esp-js package holds the EventBus: you register stores on it, publish events to them, and observe the models they produce.
A counter
import { EventBus } from 'esp-js';
type CounterModel = { count: number };
const bus = new EventBus();
bus.storeBuilder<CounterModel>('counter', { count: 0 })
.withEventHandler('Increment', (draft, event: { by: number }) => {
draft.count += event.by;
})
.build();
bus.getStoreModelObservable<CounterModel>('counter')
.subscribe(model => console.log(model.count));
bus.publishEvent('counter', 'Increment', { by: 1 });
Output:
0
1
storeBuilderregisters a store with thestoreId'counter'and the initial model{ count: 0 }.withEventHandleradds a handler that changes the model when anIncrementevent arrives.subscribeprints0straight away, because the observable sends the current model to each new observer.publishEventsendsIncrementto the store. The handler runs, and the observer prints the new model,1.
Pages in this section
- Event bus: publish events, read models, and handle errors.
- Store builder: register a store and its handlers.
- Effects: do async work and talk to services.
- State change workflow: the order in which the bus runs handlers, effects, and observers.
- Talking to other stores: publish events from one store to another.
- Logging: the logger, levels, and sinks.