Skip to content

@busly/ngrx ​

npmweekly downloadsmonthly downloadstypesbundle size

NgRx SignalStore features for the bus. Mirrors withReducer / on / withEventHandlers from @ngrx/signals/events, without depending on it.

This page is the API reference. For a guided introduction, see Using with signalStore in the Angular guide.

Install ​

bash
pnpm add @busly/ngrx @busly/angular @busly/core @ngrx/signals

The peer dependencies are @angular/core, @ngrx/signals 22 (only the core entry point, not /events), @busly/core, @busly/angular, and rxjs.

on ​

ts
function on<State extends object, C extends IBusEventCreator[]>(
  ...args: [...events: C, reducer: CaseReducer<State, C>]
): CaseReducerResult<State, C>;

Creates a case reducer for withBusReducer, taking one or more creators followed by the reducer. It mirrors NgRx's on, with one difference: at least one creator is required, and calling it without any throws an error, because Busly has no "listen to everything" mode.

The reducer receives (event, state), where the event is typed as the union of the creators' events. It can return a partial state, a PartialStateUpdater, or an array of both.

ts
on(counterEvents.set, ({ payload }) => ({ count: payload }));
on(counterEvents.increment, () => (state) => ({ count: state.count + 1 }));
on(counterEvents.set, counterEvents.increment, () => [{ dirty: true }]);

withBusReducer ​

ts
function withBusReducer<State extends object>(
  ...caseReducers: CaseReducerResult<State, IBusEventCreator[]>[]
): SignalStoreFeature<{ state: State; props: {}; methods: {} }, EmptyFeatureResult>;

A SignalStore feature that applies case reducers to Busly events. It mirrors NgRx's withReducer, but listens on the bus instead of ReducerEvents.

Reducers listen through BusEvents.on, so they always run before any lazy listener, whatever the order of features in the store. The state is read untracked with getState, and the result is applied with patchState. When several case reducers handle the same type, they run in the order they were declared.

The feature never re-dispatches the events it reduces, and it stops listening when the store's injector is destroyed.

withBusEventHandlers ​

ts
function withBusEventHandlers<Input extends SignalStoreFeatureResult>(
  handlersFactory: (store: HandlersStore<Input>) => Record<string, Observable<unknown>> | Observable<unknown>[],
): SignalStoreFeature<Input, EmptyFeatureResult>;

A SignalStore feature for side effects. It mirrors NgRx's withEventHandlers, but dispatches on the bus.

The store argument exposes the state signals, props, and methods, and it can be written with patchState. The factory runs in onInit, inside an injection context, so using inject for default parameters works as expected.

What happens to each emitted value depends on what it is:

ValueResult
New event (isBusEvent and not isBusDelivered)Dispatched via BusDispatcher
Event received from BusEvents, passed throughIgnored
Spread copy of a received eventNew event → dispatched
Primitive, object without payload, [event, config] tupleIgnored

The feature never touches the NgRx Dispatcher. Be careful with spread copies: since they count as new events, a copy that keeps the original type will loop.

Each handler is subscribed on its own, so an error ends only the handler that failed, and RxJS reports it. NgRx, by contrast, ends every handler in the feature. All handlers are torn down when the store's injector is destroyed.

Types ​

ts
type CaseReducer<State extends object, C extends IBusEventCreator[]> = (
  event: ReturnType<C[number]>,
  state: State,
) => Partial<State> | PartialStateUpdater<State> | Array<Partial<State> | PartialStateUpdater<State>>;

type CaseReducerResult<State extends object, C extends IBusEventCreator[]> = {
  reducer: CaseReducer<State, C>;
  events: C;
};

These have the same shape as NgRx's CaseReducerResult.

Compared with NgRx ​

@ngrx/signals/events@busly/ngrx
withReducer(on(...))withBusReducer(on(...))
on(...), listen-all without creatorson(...), ≥1 creator required
withEventHandlers → NgRx DispatcherwithBusEventHandlers → BusDispatcher
[event, { scope }] tuplesNot supported
Handler error ends every handlerHandler error ends that handler

Avoid using NgRx's own withReducer and withEventHandlers with Busly events. withReducer never sees them, and withEventHandlers re-dispatches any Busly event that's passed through into NgRx, because Busly events lack NgRx's internal tag.

Public API ​

ExportKindRole
onfunctionCase reducer: creators plus reducer
withBusReducerfeatureApplies case reducers to bus events
withBusEventHandlersfeatureSide effects; new events go on the bus
CaseReducertypeReducer type
CaseReducerResulttypeon(...) result type