@busly/js
Framework-agnostic adapter for the Busly event bus (
@busly/core).
This page is the API reference. For a guided introduction, see the TypeScript and Web Components and other frameworks guides.
Install
pnpm add @busly/js @busly/coreThe only peer dependency is @busly/core, which should be installed once per page. Since the package doesn't depend on any framework, it works in vanilla code, Vue, Svelte, Solid, Web Components, and workers alike.
createDispatcher
function createDispatcher<G extends IBusEventGroup>(
events: G,
options?: DispatchOptions,
): SelfDispatchingEvents<G>;Returns an object with one function per creator in the group. Calling one of those functions creates the event and dispatches it.
| Option | Type | Default | Notes |
|---|---|---|---|
source | string | 'busly-js' | Sender label |
server | boolean | detected | Overrides server detection. Resolved once |
The functions are plain closures, so they're safe to use unbound. Their payload types come from the creators, and events with a void payload take no argument. If a payload can't be cloned, the call throws a DataCloneError right away. On the server, the functions do nothing.
const counter = createDispatcher(counterPageEvents, { source: 'shell' });
counter.set(42);dispatch
function dispatch(event: IBusEvent, options?: DispatchOptions): void;Sends a single event. It takes the same options as createDispatcher, but when server isn't given, the environment is checked on every call.
dispatch(counterPageEvents.set(42), { source: 'shell' });createReceiver
function createReceiver(options?: EnvOptions): Receiver;
interface Receiver {
on<C extends IBusEventCreator>(events: C | readonly C[], handler: EventHandler<C>): () => void;
lazy<C extends IBusEventCreator>(events: C | readonly C[], handler: EventHandler<C>): () => void;
dispose(): void;
}| Option | Type | Default | Notes |
|---|---|---|---|
server | boolean | detected | Overrides server detection. Resolved once |
receiver.on / receiver.lazy
Both methods register handler for a single creator or an array of them, and return a function that unsubscribes it. on belongs to the first cohort, meant for state, and lazy to the second, meant for side effects.
For each event, every on handler runs before any lazy handler, regardless of the order in which they subscribed. This holds across all receivers created from the same copy of the package.
A few more details about how registration works:
- Passing an empty array throws an error.
- If the same type appears more than once, each event is still delivered once.
- Registering the same handler twice creates two independent subscriptions.
- Calling the unsubscribe function more than once is safe.
- A handler added while an event is being delivered waits for the next event; one removed during delivery is skipped.
- If a handler throws, the others still run, and the error is re-thrown in a microtask.
- On the server, nothing is registered and the returned function does nothing.
receiver.dispose
Removes every handler this receiver registered. Other receivers aren't affected, and the receiver remains usable afterwards.
Received event
type ReceivedEvent<C extends IBusEventCreator> = Envelope<ReturnType<C>['payload']> & ReturnType<C>;
type EventHandler<C extends IBusEventCreator> = (event: ReceivedEvent<C>) => void;Handlers receive the creator's event together with the envelope metadata:
| Property | Meaning |
|---|---|
type | Event type, typed from the creator |
payload | Payload, typed from the creator |
source | Sender's source |
ts | Dispatch time on the sender |
v | Envelope version |
All the handlers in an application share the same event object, so treat it as read-only.
Internals worth knowing
There's one core subscription per event type, shared by every receiver, and it's released once its last handler goes away. That shared subscription is what makes the cross-receiver ordering possible, but it also means the ordering only spans one instance of the module. Microfrontends that bundle their own copy of @busly/js order their handlers independently, so share a single copy when the order matters across them.
There's also one edge case to be aware of. If the last handler for a type unsubscribes and a new one subscribes within the same tick, an event already in flight for that type is dropped.
Server detection
The package treats the environment as a server when there is no window and the code isn't running in a worker. Electron renderers and workers both count as browser contexts.
Public API
| Export | Kind | Role |
|---|---|---|
createDispatcher | function | Self-dispatching functions for an event group |
dispatch | function | Sends one event |
createReceiver | function | Creates a receiver: on, lazy, dispose |
Receiver | type | Receiver interface |
EventHandler | type | Handler for a creator |
ReceivedEvent | type | Event a handler receives |
DispatchOptions | type | EventOptions & EnvOptions (source, server) |
The event types, isBusEvent, and EnvOptions come from @busly/core.
Out of scope
This package sends and receives events, but it doesn't store state. It also doesn't ship framework bindings: the callback API is meant to be wrapped in a few lines wherever you need it, as shown in Web Components and other frameworks. And there won't be a "listen to everything" form; you always name the events you want to hear.
Related packages
@busly/core: the bus@busly/events: event creators@busly/zustand: Zustand reducers, built on this package@busly/angular: the sameon/lazymodel, with DI and RxJS