@busly/angular
Angular adapter for the Busly event bus (
@busly/core).
This page is the API reference. For a guided introduction, see the Angular guide. SignalStore features live in a separate package, @busly/ngrx.
Install
pnpm add @busly/angular @busly/coreThe peer dependencies are @angular/core and @angular/common (version 22) and @busly/core. The package also uses rxjs, which every Angular application already has. It doesn't depend on NgRx.
BusDispatcher
@Service() // root
class BusDispatcher {
dispatch(event: IBusEvent): void;
}Sends event using the configured options, such as the source. The service is root-injectable, so it works without any provider.
If a payload can't be cloned, dispatch throws a DataCloneError right away. On the server platform, it does nothing.
injectBusDispatch
function injectBusDispatch<G extends IBusEventGroup>(
events: G,
config?: { injector?: Injector },
): SelfDispatchingEvents<G>;Returns one function per creator in the group, each dispatching through BusDispatcher. It mirrors NgRx's injectDispatch.
It must be called in an injection context, unless you pass config.injector. Either way, it uses the BusDispatcher of that injector, and therefore its source.
provideBusDispatcher
function provideBusDispatcher(options?: EventOptions): Provider[];Configures BusDispatcher for an injector. Without it, the source is 'busly-angular'. Since it also provides BusDispatcher itself, you can use it at the route or component level to give part of your application its own source.
bootstrapApplication(App, {
providers: [provideBusDispatcher({ source: 'shell' })],
});BusEvents
@Service() // root
class BusEvents {
on<C extends IBusEventCreator[]>(...events: C): Observable<ReturnType<C[number]>>;
lazy<C extends IBusEventCreator[]>(...events: C): Observable<ReturnType<C[number]>>;
}Both methods listen to one or more creators and return a plain Observable (not a Subject). When you pass several creators, their streams are merged.
| Method | Cohort | NgRx equivalent | Use for |
|---|---|---|---|
on | first | ReducerEvents.on(...) | State |
lazy | second | Events.on(...) | Side effects |
For each event, every on subscriber runs before any lazy subscriber, regardless of the order in which they subscribed. This guarantee holds within one BusEvents instance.
Here's how the service behaves in more detail:
- Calling either method without arguments throws an error.
- If the same type appears more than once, each event is still delivered once.
- For each type,
onandlazyshare a single core subscription with a reference count. The last unsubscribe releases it, and resubscribing to an Observable you held onto reconnects it. - If a subscriber throws, RxJS reports the error and the other subscribers still run. An error inside an operator ends only that stream.
- Emitted objects carry a delivery mark (see
isBusDelivered). Readtype,payload,source,ts, andv, and don't rely on any other properties. - All subscribers share the same event object, so treat it as read-only.
- If the last subscriber for a type unsubscribes and a new one subscribes within the same tick, an event already in flight for that type is dropped.
- On the server platform, the Observable completes immediately.
Tearing down is regular RxJS: takeUntilDestroyed, the async pipe, toSignal, or unsubscribe.
isBusDelivered
function isBusDelivered(value: unknown): boolean;Returns true for event objects handed out by BusEvents.on or lazy. Integrations use it to tell an event that's being passed through from a brand-new one, which is how they avoid re-dispatch loops. Note that a spread copy ({ ...e }) loses the mark, and that the mark is local: it doesn't travel to other contexts.
Zone.js
Zoneless applications, the default in Angular 22, need nothing special. With zone.js, events dispatched by non-Angular code on the same page arrive outside the Angular zone, so either write to signals or wrap the work in NgZone.run.
Public API
| Export | Kind | Role |
|---|---|---|
BusDispatcher | service | dispatch(event) |
injectBusDispatch | function | Self-dispatching functions for an event group |
provideBusDispatcher | function | Sets source, provides BusDispatcher |
BusEvents | service | on(...) / lazy(...) Observables |
isBusDelivered | function | true for event objects handed out by BusEvents |
The event types (IBusEvent, IBusEventCreator, IBusEventGroup, SelfDispatchingEvents), isBusEvent, and EventOptions come from @busly/core.
Out of scope
This package sends and receives events, but it doesn't store state; the SignalStore features live in @busly/ngrx. There won't be a "listen to everything" form either.
Parent scopes are also left out on purpose. The main use case is events coming from other browsing contexts, and boundaries inside a single application are already well covered by existing libraries.
Related packages
@busly/core: the bus@busly/events: event creators@busly/ngrx:withBusReducer,on,withBusEventHandlers