1.0.1 • Published 5 years ago

cobu-eventbus v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

INACTIVE This library is no longer active. For an alternative take a look at RxJs.

cobu-eventbus

Build Status GitHub version NPM version

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

PerformanceScenario
10x - 100x fasterposting an event when using a lot of context and handlers
50% smallerfile size
25% slowerwhen adding / removing an event handler
1.0.1

5 years ago

1.0.0

5 years ago

0.14.0

6 years ago

0.13.0

10 years ago

0.12.0

10 years ago

0.11.4

10 years ago

0.11.3

10 years ago

0.11.2

10 years ago

0.11.1

10 years ago

0.11.0

10 years ago