Skip to content

Commit

Permalink
[fixup] cleanup proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
apapirovski committed Nov 19, 2017
1 parent 2f77aa6 commit e07abd5
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit e07abd5

Please sign in to comment.