@neomasterr/eventer
v1.0.1
Published
Liteweight EventEmitter JavaScript Class
Downloads
9
Maintainers
Readme
Eventer
Simple lightweight inheritable dependency free event emitter with minimal footprint
Example
import Eventer from '@neomasterr/eventer';
const ev = new Eventer();
ev.on('init', (foo, bar) => {
console.log(foo, bar);
});
ev.emit('init', 'foo', 'bar');
Inheritance (mixin)
///////////////////////////
// my-super-component.js //
///////////////////////////
import {EventerMixin} from '@neomasterr/eventer';
function MySuperComponent(options = {on: {}}) {
EventerMixin.call(this, options.on);
// typeof this.eventer == 'Eventer'
this.emit('init'); // or this.eventer.emit('init');
setTimeout(() => {
this.emit('init:after');
}, 1000);
}
// mixin
Object.assign(MySuperComponent.prototype, EventerMixin.prototype);
// prototype chaining
Object.defineProperty(MySuperComponent.prototype, 'constructor', {
value: MySuperComponent,
writable: true,
enumerable: false,
});
/////////////
// page.js //
/////////////
import MySuperComponent from './my-super-component';
const component = new MySuperComponent({
on: {
init: () => {
console.log('Hello world!');
},
},
});
component.on('init:after', () => {
console.log('After init');
});