1.1.1 • Published 6 years ago

brev v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

CircleCI NPM version NPM Downloads License

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

exports

const brev = require("brev")
var globalBus = brev // The global brev bus
var bus = brev() // A new brev bus

bus.on(topic, listener)

ParameterTypeDescription
topicStringThe event to listen to.
listenerFunctionThe 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)

ParameterTypeDescription
topicStringThe event name
listenerFunctionThe 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)

ParameterTypeDescription
topicStringThe event to listen to.
maxNumberThe maximum amount of times the listener can be called.
listenerFunctionThe 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)

ParameterTypeDescription
topicStringThe 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)

ParameterTypeDescription
topicStringThe event the listener is registered on.
listenerFunctionThe listener to remove.

Unregister a listener from the given event.

function handler(e) {}
bus.off('connect', handler)

bus.emit(topic[, event][, onlyLocal])

ParameterTypeDescription
topicStringThe event name to execute the event on.
[event]AnyThe event to get passed to listeners.
[onlyLocal]Boolean=falseWhether 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)

ParameterTypeDescription
objAnyThe object to mix into.

Mixin this eventbus into another object.

var mixedinObjext = bus.mixin({ hello: 'world' })