Home Reference Source

application/util/events.js

import {info} from './console';

const EventEmitter = require('eventemitter2').EventEmitter2;

const events = new EventEmitter();

/**
 * Internal event system, based on EventEmitter2.
 */
class Events {

    constructor(e) {
        this.events = e;
    }

    /**
     * Listen to event
     * @param  {string}   evt      Event
     * @param  {Function} callback Callback
     */
    listen(evt, callback) {
        return this.events.addListener(evt, callback);
    }

    /**
     * Stop listening to event callback
     * @param  {string}   evt      Event
     * @param  {Function} callback Callback
     */
    unlisten(evt, callback) {
        return this.events.removeListener(evt, callback);
    }

    /**
     * Emit event with given properties
     * @param  {string}    evt    Event
     * @param  {...mixed} params  Any necessary params
     */
    emit(evt, ...params) {
        info('EMIT: ' + evt, ...params);
        return this.events.emit(evt, ...params);
    }
}

export default (new Events(events));