Simple typesafe cross-platform pubsub communication between different fragments or services. Purposefully built for a micro architecture service using for example versioned microfrontends.
This project was created to improve upon some of the deficits CustomEvents has in relation to event communication between separate fragments, which often is the preferred way of communication. Below points are some of the benefits of using this pubsub package.
Each fragment can register on an event channel name to ensure all traffic on that channel follow a specified schema. This means incompatibilities are reported before any payload is sent and every payload will be typesafe.
The individual event channels stores the last payload allowing new fragments or asynchronous subscriptions to ask for a replay of the last payloads data as a first callback.
CustomEvents require a polyfill to work in older browsers.
Either add the Trutoo GitHub Package registry to your
or install using the registry flag
npm install @trutoo/event-bus --registry=https://npm.pkg.github.com/trutoo
or install from the npm registry @trutoo/event-bus
npm install @trutoo/event-bus
Then either import the side effects only exposing a
eventBus global instance.
or import the
EventBus class to create your own instance.
;// orconst EventBus = ;const myEventBus = ;myEventBus;
or using the UMD module and instance.
Register a schema for the specified event type and equality checking on subsequent registers. Subsequent registers must use an equal schema or an error will be thrown.
registereventType: string, schema: object: boolean;
||name of event channel to register schema to|
||all communication on channel must follow this schema|
Returns - returns true if event channel already existed of false if a new one was created.
Unregister the schema for the specified event type if channel exists.
unregistereventType: string: boolean;
||name of event channel to unregister schema from|
Returns - returns true if event channel existed and an existing schema was removed.
Subscribe to an event channel triggering callback on received event matching type, with an optional replay of last event at initial subscription.
subscribeeventType: string, callback: Callback<T>: ;subscribeeventType: string, replay: boolean, callback: Callback<T>: ;
||name of event channel to receive data from|
||flag indicating if initial description should return last event|
||function executed on when event channel receives new data|
Returns - object containing an unsubscribe method
Publish to event channel with an optional payload triggering all subscription callbacks.
publisheventType: string, detail?: T: void;
||name of event channel to send payload on|
||payload to be sent|
Returns - void