tom32i-event-emitter.js
v2.0.5
Published
Minimalist Event Emitter
Downloads
2,734
Maintainers
Readme
tom32i-event-emitter.js
Minimalist JS Event Emitter system
Install:
npm install tom32i-event-emitter.js
Import
HTML:
<script src="tom32i-event-emitter.js"></script>
ES6:
import EventEmitter from 'tom32i-event-emitter.js';
Node:
const EventEmitter = require('tom32i-event-emitter.js');
Usage
Make your prototype extend the EventEmitter.
Emit event with the emit
method.
/**
* Player
*/
class Player extends EventEmitter {
constructor() {
super();
this.alive = true;
}
/**
* Emit and event on death
*/
die() {
this.alive = false;
// Emitting an event:
this.emit('die', { player: this, foo: 'bar' });
}
}
Listening for events with on
/ off
:
var player = new Player();
function onDie(event) {
const { type, detail } = event;
const { foo, player } = detail;
// ...
}
// Adding a listener
player.on('die', onDie); // "on" is an alias of "addEventListener"
// Removing a listener
player.off('die', onDie); // "off" is an alias of "removeEventListener"
Contribute
Clone the repository:
git clone [email protected]:Tom32i/event-emiter.js.git
Install dev dependencies:
npm install
Build dist:
npm build