function EventEmitter () {
EventEmitter.init.call(this)
}
EventEmitter.prototype._events = undefined
EventEmitter.prototype._maxListeners = undefined
EventEmitter.defaultMaxListeners = 20
EventEmitter.init = function () {
if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
this._events = {}
this._eventsCount = 0
}
this._maxListeners = this._maxListeners || undefined
}
EventEmitter.prototype.setMaxListeners = function setMaxListeners (n) {
if (typeof n !== 'number' || n < 0 || isNaN(n)) { throw new TypeError('n must be a positive number') }
this._maxListeners = n
return this
}
function $getMaxListeners (that) {
if (that._maxListeners === undefined) { return EventEmitter.defaultMaxListeners }
return that._maxListeners
}
EventEmitter.prototype.getMaxListeners = function getMaxListeners () {
return $getMaxListeners(this)
}
function emitNone (handler, isFn, self) {
if (isFn) { handler.call(self) } else {
const len = handler.length
const listeners = arrayClone(handler, len)
for (let i = 0; i < len; ++i) { listeners[i].call(self) }
}
}
function emitOne (handler, isFn, self, arg1) {
if (isFn) { handler.call(self, arg1) } else {
const len = handler.length
const listeners = arrayClone(handler, len)
for (let i = 0; i < len; ++i) { listeners[i].call(self, arg1) }
}
}
function emitTwo (handler, isFn, self, arg1, arg2) {
if (isFn) { handler.call(self, arg1, arg2) } else {
const len = handler.length
const listeners = arrayClone(handler, len)
for (let i = 0; i < len; ++i) { listeners[i].call(self, arg1, arg2) }
}
}
function emitThree (handler, isFn, self, arg1, arg2, arg3) {
if (isFn) { handler.call(self, arg1, arg2, arg3) } else {
const len = handler.length
const listeners = arrayClone(handler, len)
for (let i = 0; i < len; ++i) { listeners[i].call(self, arg1, arg2, arg3) }
}
}
function emitMany (handler, isFn, self, args) {
if (isFn) { handler.apply(self, args) } else {
const len = handler.length
const listeners = arrayClone(handler, len)
for (let i = 0; i < len; ++i) { listeners[i].apply(self, args) }
}
}
EventEmitter.prototype.emit = function emit (type) {
let er, args, i
let doError = (type === 'error')
const events = this._events
if (events) { doError = (doError && events.error == null) } else if (!doError) { return false }
if (doError) {
er = arguments[1]
if (er instanceof Error) {
throw er
} else {
const err = new Error('Uncaught, unspecified "error" event. (' + er + ')')
err.context = er
throw err
}
}
const handler = events[type]
if (!handler) { return false }
const isFn = typeof handler === 'function'
const len = arguments.length
switch (len) {
case 1:
emitNone(handler, isFn, this)
break
case 2:
emitOne(handler, isFn, this, arguments[1])
break
case 3:
emitTwo(handler, isFn, this, arguments[1], arguments[2])
break
case 4:
emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3])
break
default:
args = new Array(len - 1)
for (i = 1; i < len; i++) { args[i - 1] = arguments[i] }
emitMany(handler, isFn, this, args)
}
return true
}
function checkListener (listener) {
if (typeof listener !== 'function') { throw new TypeError('listener must be a function') }
}
function _addListener (target, type, listener, prepend) {
let m
let events
let existing
checkListener(listener)
events = target._events
if (!events) {
events = target._events = {}
target._eventsCount = 0
} else {
if (events.newListener) {
target.emit('newListener', type,
listener.listener ? listener.listener : listener)
events = target._events
}
existing = events[type]
}
if (!existing) {
existing = events[type] = listener
++target._eventsCount
} else {
if (typeof existing === 'function') {
existing = events[type] = prepend ? [listener, existing] : [existing, listener]
} else if (prepend) {
existing.unshift(listener)
} else {
existing.push(listener)
}
if (!existing.warned) {
m = $getMaxListeners(target)
if (m && m > 0 && existing.length > m) {
existing.warned = true
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d %s listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
existing.length, type)
console.trace()
}
}
}
return target
}
EventEmitter.prototype.addListener = function addListener (type, listener) {
return _addListener(this, type, listener, false)
}
EventEmitter.prototype.on = EventEmitter.prototype.addListener
EventEmitter.prototype.prependListener =
function prependListener (type, listener) {
return _addListener(this, type, listener, true)
}
function onceWrapper () {
if (!this.fired) {
this.target.removeListener(this.type, this.wrapFn)
this.fired = true
if (arguments.length === 0) { return this.listener.call(this.target) }
return this.listener.apply(this.target, arguments)
}
}
function _onceWrap (target, type, listener) {
const state = { fired: false, wrapFn: undefined, target, type, listener }
const wrapped = onceWrapper.bind(state)
wrapped.listener = listener
state.wrapFn = wrapped
return wrapped
}
EventEmitter.prototype.once = function once (type, listener) {
checkListener(listener)
this.on(type, _onceWrap(this, type, listener))
return this
}
EventEmitter.prototype.prependOnceListener =
function prependOnceListener (type, listener) {
checkListener(listener)
this.prependListener(type, _onceWrap(this, type, listener))
return this
}
EventEmitter.prototype.removeListener =
function removeListener (type, listener) {
let position, i
if (typeof listener !== 'function') { throw new TypeError('listener must be a function') }
const events = this._events
if (!events) { return this }
const list = events[type]
if (!list) { return this }
if (list === listener || (list.listener && list.listener === listener)) {
if (--this._eventsCount === 0) { this._events = {} } else {
delete events[type]
if (events.removeListener) {
this.emit('removeListener', type, listener)
}
}
} else if (typeof list !== 'function') {
position = -1
for (i = list.length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i
break
}
}
if (position < 0) { return this }
if (list.length === 1) {
list[0] = undefined
if (--this._eventsCount === 0) {
this._events = {}
return this
} else {
delete events[type]
}
} else {
spliceOne(list, position)
}
if (events.removeListener) { this.emit('removeListener', type, listener) }
}
return this
}
EventEmitter.prototype.removeAllListeners =
function removeAllListeners (type) {
const events = this._events
if (!events) { return this }
if (!events.removeListener) {
if (arguments.length === 0) {
this._events = {}
this._eventsCount = 0
} else if (events[type]) {
if (--this._eventsCount === 0) { this._events = {} } else { delete events[type] }
}
return this
}
if (arguments.length === 0) {
const keys = Object.keys(events)
let i = 0; let key
for (; i < keys.length; ++i) {
key = keys[i]
if (key === 'removeListener') continue
this.removeAllListeners(key)
}
this.removeAllListeners('removeListener')
this._events = {}
this._eventsCount = 0
return this
}
const listeners = events[type]
if (typeof listeners === 'function') {
this.removeListener(type, listeners)
} else if (listeners) {
do {
this.removeListener(type, listeners[listeners.length - 1])
} while (listeners[0])
}
return this
}
EventEmitter.prototype.listeners = function listeners (type) {
let evlistener
let ret
const events = this._events
if (!events) { ret = [] } else {
evlistener = events[type]
if (!evlistener) { ret = [] } else if (typeof evlistener === 'function') { ret = [evlistener] } else { ret = arrayClone(evlistener, evlistener.length) }
}
return ret
}
EventEmitter.listenerCount = function (emitter, type) {
if (typeof emitter.listenerCount === 'function') {
return emitter.listenerCount(type)
} else {
return listenerCount.call(emitter, type)
}
}
EventEmitter.prototype.listenerCount = listenerCount
function listenerCount (type) {
const events = this._events
if (events) {
const evlistener = events[type]
if (typeof evlistener === 'function') {
return 1
} else if (evlistener) {
return evlistener.length
}
}
return 0
}
function spliceOne (list, index) {
let i = index; let k = i + 1
const n = list.length
for (; k < n; i += 1, k += 1) { list[i] = list[k] }
list.pop()
}
function arrayClone (arr, i) {
const copy = new Array(i)
while (i--) { copy[i] = arr[i] }
return copy
}
module.exports = EventEmitter