Minimalist JS Event Emitter system
npm install tom32i-event-emitter.js
HTML:
<script src="tom32i-event-emitter.js"></script>
ES modules:
import EventEmitter from 'tom32i-event-emitter.js';
CommonJs modules:
const EventEmitter = require('tom32i-event-emitter.js');
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"
Clone the repository:
git clone [email protected]:Tom32i/event-emiter.js.git
Install dev dependencies:
npm install
make start
Go to http://localhost:8080
Linting:
make lint
Run tests:
make test