brev v1.1.1
This is a event bus system that primarily sends and recives events. You are able to create new event busses and mix them into other objects. This script also works in a web page. While in a web page you have the capabillity to comunicate to the running service worker and the from the serviceworker to all active tabs not across browsers.
API
- brev.createBus()
- bus.on(topic, listener)
- bus.once(topic[, listener])
- bus.many(topic, max, listener)
- bus.observe(topic)
- bus.off(topic, listener)
- bus.emit(topic[, event][, onlyLocal])
- bus.mixin(obj)
exports
const brev = require("brev")
var globalBus = brev // The global brev bus
var bus = brev() // A new brev bus
bus.on(topic, listener)
Parameter | Type | Description |
---|---|---|
topic | String | The event to listen to. |
listener | Function | The actual listener to get fired. |
Returns: The instance.
Add a listener for the event given on this event bus.
function handler(e) {}
bus.on('connect', handler);
bus.once(topic, listener)
Parameter | Type | Description |
---|---|---|
topic | String | The event name |
listener | Function | The function handling the event |
Returns: Promise\ Promise with the result of the listener or the event.
Registers a handler to the given topic. It will only be called one time before it is unregistered.
It returns a promise containing the event if no listener was registered. Otherwise the promise contains the result of the listener.
function handler(e) {}
bus.once('connect', handler);
bus.many(topic, max, listener)
Parameter | Type | Description |
---|---|---|
topic | String | The event to listen to. |
max | Number | The maximum amount of times the listener can be called. |
listener | Function | The listener to get fired. |
Returns: The instance
Add a listener for the event given on this event bus. It will only be called x amount of times before it is automatically unregistered.
function handler(e) {}
bus.many('connect', 3, handler);
bus.observe(topic)
Parameter | Type | Description |
---|---|---|
topic | String | The event to listen to. |
Returns: A object with some methods.
Register a observer on a topic.
You can filter the events with the filter
method, transform the value with map
and
access the value with run
.
To unregister the observer call unobserve
.
bus.observe('connect')
.filter(e => typeof e === 'string')
.map(e => e.toLowerCase())
.run(e => {
console.log(e)
})
bus.off(topic, listener)
Parameter | Type | Description |
---|---|---|
topic | String | The event the listener is registered on. |
listener | Function | The listener to remove. |
Unregister a listener from the given event.
function handler(e) {}
bus.off('connect', handler)
bus.emit(topic[, event][, onlyLocal])
Parameter | Type | Description |
---|---|---|
topic | String | The event name to execute the event on. |
[event] | Any | The event to get passed to listeners. |
[onlyLocal] | Boolean=false | Whether or not the event should be broadcasted to the serviceworker / tabs. |
Emit a event to all listeners registered to the given topic
.
bus.emit('connect', { status: 'ok' })
bus.mixin(obj)
Parameter | Type | Description |
---|---|---|
obj | Any | The object to mix into. |
Mixin this eventbus into another object.
var mixedinObjext = bus.mixin({ hello: 'world' })