Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/events.js

Issue 29729594: Noissue - Use Map to store EventEmitter listeners (Closed)
Patch Set: Avoid calling .get directly after .has Created April 3, 2018, 5:26 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | lib/filterNotifier.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/events.js
diff --git a/lib/events.js b/lib/events.js
index 3e9067a2b1bfe88ccace767b19ded6fdc42c5be1..cf6baf0dfd4e37b226bcdd6ed1c8925b4390accd 100644
--- a/lib/events.js
+++ b/lib/events.js
@@ -24,7 +24,7 @@
*/
exports.EventEmitter = function()
{
- this._listeners = Object.create(null);
+ this._listeners = new Map();
};
exports.EventEmitter.prototype = {
@@ -36,10 +36,11 @@ exports.EventEmitter.prototype = {
*/
on(name, listener)
{
- if (name in this._listeners)
- this._listeners[name].push(listener);
+ let listeners = this._listeners.get(name);
+ if (listeners)
+ listeners.push(listener);
else
- this._listeners[name] = [listener];
+ this._listeners.set(name, [listener]);
},
/**
@@ -50,7 +51,7 @@ exports.EventEmitter.prototype = {
*/
off(name, listener)
{
- let listeners = this._listeners[name];
+ let listeners = this._listeners.get(name);
if (listeners)
{
let idx = listeners.indexOf(listener);
@@ -87,7 +88,7 @@ exports.EventEmitter.prototype = {
*/
listeners(name)
{
- let listeners = this._listeners[name];
+ let listeners = this._listeners.get(name);
return listeners ? listeners.slice() : [];
},
« no previous file with comments | « no previous file | lib/filterNotifier.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld