Event Bus
Simple typesafe cross-platform pubsub communication between different fragments or services. Purposefully built for a micro architecture service using for example versioned microfrontends.
Purpose
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.
Installation
Either add the Trutoo GitHub Package registry to your .npmrc
@trutoo:registry=https://npm.pkg.github.com/trutoo
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; eventBus;
or import the EventBus
class to create your own instance.
;// orconst EventBus = ; const myEventBus = ;myEventBus;
or using the UMD module and instance.
Usage
Fragment One
Fragment Two
;;
API
Register
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;
Parameters
Name | Type | Description |
---|---|---|
eventType | string |
name of event channel to register schema to |
schema | object |
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
Unregister the schema for the specified event type if channel exists.
unregistereventType: string: boolean;
Parameters
Name | Type | Description |
---|---|---|
eventType | string |
name of event channel to unregister schema from |
Returns - returns true if event channel existed and an existing schema was removed.
Subscribe
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>: ;
Parameters
Name | Type | Description |
---|---|---|
eventType | string |
name of event channel to receive data from |
replay | boolean=false |
flag indicating if initial description should return last event |
callback | function |
function executed on when event channel receives new data |
Returns - object containing an unsubscribe method
Publish
Publish to event channel with an optional payload triggering all subscription callbacks.
publisheventType: string, detail?: T: void;
Parameters
Name | Type | Description |
---|---|---|
eventType | string |
name of event channel to send payload on |
detail | any |
payload to be sent |
Returns - void