cobu-eventbus
v1.0.1
Published
cobu-eventbus is a simple, lightweight and flexible JavaScript library to manage events.
Downloads
46
Readme
[INACTIVE] This library is no longer active. For an alternative take a look at RxJs.
cobu-eventbus
cobu-eventbus is a simple, lightweight and flexible JavaScript library to manage events. It works using strings or objects (even 'strong typed') as events. You define what an event is, cobu-eventbus will support it.
The purpose of an event bus is to decouple components. This pattern is very important for large scale JavaScript applications to be maintainable.
Getting started
npm install cobu-eventbus
Examples
Simple scenario using a string as an event.
// Create the EventBus instance.
var eventBus = new cobu.EventBus();
// Create an callback function that can handle the specific event.
function handleFooEvent(event) {
console.log('Handling a foo event');
}
// Register the callback function to an event type.
eventBus.on('foo', handleFooEvent);
// Post an event
eventBus.post('foo');
Using an object as an event
// Create the EventBus instance.
var eventBus = new cobu.EventBus();
// Define a constructor function to create the helloEvent object later.
function HelloEvent(name) {
this.type = 'helloEvent';
this.name = name;
}
// Create an callback function that can handle the specific event type.
function handleHelloEvent(event) {
console.log('Hello ' + event.name);
};
// Register the callback function to an event type.
eventBus.on(HelloEvent, handleAlertEvent);
// Post a new event.
eventBus.post(new HelloEvent('foo'));
API
Statistics for nerds
Performance difference between version 0.14.0 and 1.0.0
| Performance | Scenario | | --- | --- | | 10x - 100x faster | posting an event when using a lot of context and handlers | | 50% smaller | file size | | 25% slower | when adding / removing an event handler |