@busly/zustand
Reduce Busly events into a Zustand store, as a middleware (
busly) or on an existing store (busReducer). MirrorswithBusReducer/onfrom@busly/ngrx.
This page is the API reference. For a guided introduction, see Using on Zustand projects in the React guide.
Install
pnpm add @busly/zustand @busly/js @busly/core zustandThe peer dependencies are @busly/js, @busly/core (installed once per page), and zustand 4 or newer. Only Zustand's types are used, so nothing from it ends up in the bundle. The package works with vanilla stores too, so React isn't required.
on
function on<State, C extends IBusEventCreator[]>(
...args: [...events: C, reducer: CaseReducer<State, C>]
): CaseReducerResult<State, C>;Creates a case reducer from one or more creators followed by the reducer. It mirrors on from @busly/ngrx, and like it, throws an error if no creator is given.
The reducer receives (event, state) and returns a partial state, which setState merges in. The event is a ReceivedEvent from @busly/js, meaning the creator's event plus the source, ts, and v metadata.
busly
const busly: <State, Mps, Mcs>(
initializer: StateCreator<State, Mps, Mcs>,
...cases: CaseReducerResult<State, IBusEventCreator[]>[]
) => StateCreator<State, Mps, Mcs>;A middleware that wires the cases when the store is created.
const useCounter = create<{ count: number }>()(
busly(() => ({ count: 0 }), on(counterEvents.set, ({ payload }) => ({ count: payload }))),
);It composes with other middleware, such as devtools, persist, or subscribeWithSelector. The wiring lives as long as the store does and can't be removed; if you need to remove it later, use busReducer instead.
busReducer
function busReducer<State>(store: StoreApi<State>, ...cases: CaseReducerResult<State, IBusEventCreator[]>[]): () => void;Applies cases to an existing store, whether it's a vanilla createStore or a hook from create. It returns a function that removes every case it registered.
const off = busReducer(useCounter, on(counterEvents.set, ({ payload }) => ({ count: payload })));
off();Behavior
busly and busReducer behave the same way. Both listen in the @busly/js on cohort, and because Zustand's setState is synchronous, every lazy handler from @busly/js, in any receiver, reads the reduced state.
Cases run in the order they were declared, so if two of them handle the same type, the second sees the result of the first.
The ordering holds within a single copy of @busly/js, so microfrontends need to share that copy for it to hold across them. On the server, both do nothing, using the same detection as createReceiver.
Types
type CaseReducer<State, C extends IBusEventCreator[]> = (
event: ReceivedEvent<C[number]>,
state: State,
) => Partial<State>;
interface CaseReducerResult<State, C extends IBusEventCreator[]> {
reducer: CaseReducer<State, C>;
events: C;
}Compared with @busly/ngrx
@busly/ngrx | @busly/zustand |
|---|---|
withBusReducer(on(...)) | busly(init, on(...)) / busReducer(store, on(...)) |
| Reducer returns partial, updater, or array | Reducer returns a partial (gets the state) |
withBusEventHandlers | @busly/js createReceiver().lazy + createDispatcher |
| Torn down with the injector | Store lifetime (busly) / dispose (busReducer) |
Public API
| Export | Kind | Role |
|---|---|---|
busly | middleware | Case reducers wired at store creation |
busReducer | function | Case reducers on an existing store; dispose |
on | function | Case reducer: creators plus reducer |
CaseReducer | type | Reducer type |
CaseReducerResult | type | on(...) result type |