/**
 * Quick and dirty implementation of an EventEmitter
 *
 * This should work in any platform (node, browser, react native)
 */

'use strict';

let immediate = null;

if (typeof setImmediate === 'function') {
    immediate = setImmediate;
} else if (typeof Promise === 'function' && typeof Promise.resolve === 'function') {
    immediate = (fn) => Promise.resolve().then(fn);
} else {
    immediate = (fn) => setTimeout(fn, 0);
}

const EventEmitter = function () {
    this._listeners = {};
};

EventEmitter.prototype.emit = function (event, arg) {
    if (Array.isArray(this._listeners[event])) {
        this._listeners[event].forEach(fn => immediate(() => fn(arg)));
    }
};

EventEmitter.prototype.addListener = function (event, fn) {
    if (Array.isArray(this._listeners[event])) {
        if (!this._listeners[event].some(fn)) {
            this._listeners[event].push(fn);
        }
    } else {
        this._listeners[event] = [fn];
    }
};

EventEmitter.prototype.on = EventEmitter.prototype.addListener;

EventEmitter.prototype.removeListener = function (event, fn) {
    if (Array.isArray(this._listeners[event])) {
        this._listeners[event] = this._listeners[event].filter(f => f !== fn);
    }
};

module.exports = EventEmitter;

@@ -143,295 +143,8 @@
 ';%0A%0A
-let immediate = null;%0A%0Aif (typeof setImmediate === 'function') %7B%0A    immediate = setImmediate;%0A%7D else if (typeof Promise === 'function' && typeof Promise.resolve === 'function') %7B%0A    immediate = (fn) =%3E Promise.resolve().then(fn);%0A%7D else %7B%0A    immediate = (fn) =%3E setTimeout(fn, 0);%0A%7D%0A%0A
 cons
@@ -355,24 +355,8 @@
 n =%3E
- immediate(() =%3E
  fn(
@@ -360,17 +360,16 @@
 fn(arg))
-)
 ;%0A    %7D%0A

call handlers sync