mirror of
https://github.com/koalyptus/TableFilter.git
synced 2024-06-02 14:02:31 +02:00
48 lines
1.1 KiB
JavaScript
48 lines
1.1 KiB
JavaScript
/**
|
|
* Event emitter class
|
|
*/
|
|
export class Emitter {
|
|
constructor() {
|
|
/**
|
|
* Events object
|
|
* @type {Object}
|
|
*/
|
|
this.events = {};
|
|
}
|
|
|
|
/**
|
|
* Subscribe to an event
|
|
* @param {String} evt Event name
|
|
* @param {Function} fn Function invoked when event is emitted
|
|
*/
|
|
on(evt, fn) {
|
|
this.events[evt] = this.events[evt] || [];
|
|
this.events[evt].push(fn);
|
|
}
|
|
|
|
/**
|
|
* Unsubscribe to an event
|
|
* @param {String} evt Event name
|
|
* @param {Function} fn Function invoked when event is emitted
|
|
*/
|
|
off(evt, fn) {
|
|
if(evt in this.events) {
|
|
this.events[evt].splice(this.events[evt].indexOf(fn), 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Emit an event
|
|
* @param {String} evt Event name followed by any other argument passed to
|
|
* the invoked function
|
|
*/
|
|
emit(evt /*, args...*/) {
|
|
if(evt in this.events === false) {
|
|
return;
|
|
}
|
|
for(var i = 0; i < this.events[evt].length; i++) {
|
|
this.events[evt][i].apply(this, [].slice.call(arguments, 1));
|
|
}
|
|
}
|
|
}
|