diff --git a/lib/events.js b/lib/events.js index 2c6262205f7073..b47fadd64752e3 100644 --- a/lib/events.js +++ b/lib/events.js @@ -39,6 +39,7 @@ EventEmitter.usingDomains = false; EventEmitter.prototype.domain = undefined; EventEmitter.prototype[kEvents] = undefined; +EventEmitter.prototype[kEventsProxy] = undefined; EventEmitter.prototype._maxListeners = undefined; // By default EventEmitters will print a warning if more than 10 listeners are @@ -78,31 +79,42 @@ EventEmitter.init = function() { } } - if (this[kEvents] === Object.getPrototypeOf(this)[kEvents]) - this[kEvents] = undefined; - - this._maxListeners = this._maxListeners || undefined; + if (!this._maxListeners) + this._maxListeners = undefined; }; // Events proxy for compatibility with older user-land code const proxyEventsHandler = { deleteProperty({ emitter }, prop) { const events = emitter[kEvents]; - return events === undefined ? false : emitter[kEvents].delete(prop); + if (events === undefined) + return false; + if (!events.has(prop) && + typeof prop !== 'string' && + typeof prop !== 'symbol') + prop = String(prop); + return events.delete(prop); }, get({ emitter }, prop) { const events = emitter[kEvents]; if (events === undefined) return; - if (typeof prop !== 'string' && typeof prop !== 'symbol') - prop = String(prop); - return events.get(prop); + let value = events.get(prop); + if (value === undefined && + typeof prop !== 'string' && + typeof prop !== 'symbol') + value = events.get(String(prop)); + return value; }, getOwnPropertyDescriptor({ emitter }, prop) { const events = emitter[kEvents]; if (events === undefined) return undefined; - const value = events.get(prop); + let value = events.get(prop); + if (value === undefined && + typeof prop !== 'string' && + typeof prop !== 'symbol') + value = events.get(String(prop)); if (value === undefined) return undefined; return { @@ -119,22 +131,27 @@ const proxyEventsHandler = { const events = emitter[kEvents]; if (events === undefined) return false; - if (typeof prop !== 'string' && typeof prop !== 'symbol') - prop = String(prop); - return events.has(prop); + let has = events.has(prop); + if (!has && typeof prop !== 'string' && typeof prop !== 'symbol') + has = events.has(String(prop)); + return has; }, ownKeys({ emitter }) { const events = emitter[kEvents]; if (events === undefined) return []; - return [...events.keys()]; + const keys = new Array(events.size); + var i = 0; + for (var [key] of events) { + keys[i++] = typeof key !== 'string' && typeof key !== 'symbol' ? + String(key) : key; + } + return keys; }, set({ emitter }, prop, value) { let events = emitter[kEvents]; if (events === undefined) events = emitter[kEvents] = new Map(); - if (typeof prop !== 'string' && typeof prop !== 'symbol') - prop = String(prop); events.set(prop, value); return true; }