openbet-eventbus v2.6.0-beta.0
openbet-eventbus
This repository provides an NPM module that can be used in your own application to both subscribe and publish events that you wish to be 'global'. These events can be heard by any other components that are in the same browser window and as such provides a sort of IPC solution.
Installation
This repository is published as an NPM module. Provided you are able to interact with the OpenBet NPM registry you can use it in your projects by running;
npm install --save openbet-eventbus
Standalone Usage
If you need to use this component in an environment where a build tool is not in use please get in contact with the package maintainers to understand options.
Examples
You use the library in your application as follows;
In one component;
import EventBus from 'openbet-eventbus'
EventBus.subscribe('an/event', payload => {
console.log('received a message - %o', payload)
})
and in another component;
import EventBus from 'openbet-eventbus'
EventBus.publish('an/event', 'hello from somewhere else!')
the result being that the first component will log the message published by the second.
Note |
---|
To avoid duplicate calls to the callback function be sure to pass the same references through on subsequent subscribe calls. Anonymous functions will be attached multiple times. |
API Reference
EventBus
The EventBus offers a consistent way to subscribe to and/or publish events between disparate modules in an asynchronous manner.
Kind: global class
eventBus.publishAsync(same)
Publish an event of a particular type with the given payload. Guarantees to be executed later in the event loop
Kind: instance method of EventBus
Param | Description |
---|---|
same | as publishSync() |
eventBus.publishSync(type, payload, eventInfo)
Publish an event of a particular type with the given payload. Synchronous version, meaning it behaves like a function call, invoking subscribed listeners immediately.
Kind: instance method of EventBus
Param | Type | Description |
---|---|---|
type | string | The type of event to publish, should be distinct |
payload | object | A payload of any sort that should be emitted |
eventInfo | object | An object containting various meta data about event itself and where it originated from. Client supplies this object when publishing and event, however NOTE that 'eventName' will be automatically added to by this library. |
eventBus.publish(type, payload, eventInfo)
Publish an event of a particular type with the given payload both over the event bus and also as a CustomEvent on the window (so it can be heard by window.addEventListener subscribers).
This can be useful if you want to send messages that are available for both internal code using the subscribe* family of methods in this library or third parties that are using standard DOM APIs.
A client using this method to dispatch a unique message will result in the following behaviour;
- a message sent over the internal eventbus and delivered exactly once to any subscribers using the subscribe() or subscribeLegacy() methods of this library
- a message delivered exactly once to any DOM subscribers using addEventListener on the window object
Kind: instance method of EventBus
Param | Type | Description |
---|---|---|
type | string | The type of event to publish, should be distinct |
payload | object | A payload of any sort that should be emitted |
eventInfo | object | An object containting various meta data about event itself and where it originated from. Client supplies this object when publishing and event, however NOTE that 'eventName' will be automatically added to by this library. |
eventBus.subscribe(type, handler)
Subscribe to a particular event that may occur asynchronously. Invoke the handler with the event payload when a message is received.
Kind: instance method of EventBus
Param | Type | Description |
---|---|---|
type | string | The type of event to subscribe to, should match one that is emitted through the use of the publish method |
handler | function | The handler method that should be invoked when an event arrives, the handler function should accept a single argument which will be the payload of the message originally published |
eventBus.subscribeLegacy(type, handler)
NOTE: Unless you need to handle messages delivered via both event bus AND window.dispatchEvent you shouldn't need to use this method!
This method provides a means of subscribing to messages that can be emitted either from the publish* family of methods in this library, or directly using window.dispatchEvent. This is useful for when you want to support an event based interface that is used not only by our own code, but also by third parties that can simply dispatch a CustomEvent on the window and not have to import the openbet-eventbus library.
A caveat for use is that there can only be one handler registered for a given type at any point in time (to avoid memory leaks due to how we offer unsubscribe). If you attempt to subscribe more than once using this method you will receive an error.
A client using this method will receive a unique message exactly once under the following conditions;
- when a call to publish(), publishAsync(), publishSync() is made
- when a call to window.dispatchEvent is made
Kind: instance method of EventBus
Param | Type | Description |
---|---|---|
type | string | The type of event to subscribe to, should match one that is emitted through the use of the publish method |
handler | function | The handler method that should be invoked when an event arrives, the handler function should accept a single argument which will be the payload of the message originally published |
eventBus.unsubscribe(type, handler)
Unsubscribe from receiving messages.
Kind: instance method of EventBus
Param | Type | Description |
---|---|---|
type | * | The type of event that should be unsubscribed from. |
handler | * | The handler method that was used when originally subscribing to the event type. Note if an arrow function was used initially then you will leak memory, each arrow declaration creates a new function and hence attempting to unsubscribe with an arrow function will simply not do anything. |
eventBus.unsubscribeLegacy(type, handler)
Unsubscribe from receiving messages.
Kind: instance method of EventBus
Param | Type | Description |
---|---|---|
type | * | The type of event that should be unsubscribed from. |
handler | * | The handler method that was used when originally subscribing to the event type. Note if an arrow function was used initially then you will leak memory, each arrow declaration creates a new function and hence attempting to unsubscribe with an arrow function will simply not do anything. |
Documented by jsdoc-to-markdown.
4 years ago