1.0.4 • Published 6 years ago

pegboard v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

Pegboard

What is this?

An event emitter where the event handlers are async, chained, and can return data to the caller.

Usage

const events = new Pegboard();

events.on('test', event => {
    console.log(event.name);
    console.log(event.data);
});

events.emit('test', {
    foo: 'bar'
});

Event handlers can be assigned a priority, where the highest priority value is ran first:

events.on('chained', event => {
    console.log('Will run first');
}, 100);

events.on('chained', event => {
    console.log('Will run last');
}, -100);

events.on('chained', event => {
    console.log('In the middle, with default priority of 0');
});

The data passed in the initial emit() call will be passed to each handler, but each handler MAY alter the data. At the end, the data object is returnd to the emit() caller.

events.on('alter', event => {
    event.data.list.push('Bar');
}, 5);

events.on('alter', event => {
    event.data.list.push('Foo');
}, 10);

const result = await events.emit('alter', { list: [] });
console.log(result);
// { list: [ 'Foo', 'Bar' ] }

License

MIT

Created By

If you like this, follow @lancestout on twitter.

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago