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

Unified Diff: lib/filterNotifier.js

Issue 29338969: Issue 3862 - Add EventEmitter class and implement in the FilterNotifier (Closed)
Patch Set: Created March 23, 2016, 2:34 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
« lib/events.js ('K') | « lib/events.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterNotifier.js
===================================================================
--- a/lib/filterNotifier.js
+++ b/lib/filterNotifier.js
@@ -20,37 +20,38 @@
* messages about filter changes.
*/
-/**
- * List of registered listeners
- * @type function[]
- */
-let listeners = [];
+let {EventEmitter} = require("events");
+
+const CATCH_ALL = "__all";
/**
* This class allows registering and triggering listeners for filter events.
* @class
*/
-let FilterNotifier = exports.FilterNotifier =
+exports.FilterNotifier =
{
+ __proto__: new EventEmitter(),
Wladimir Palant 2016/03/23 16:01:07 No need creating instances here, EventEmitter.prot
Sebastian Noack 2016/03/23 16:23:44 Nope, it won't. FilterNotifier isn't a class.
Wladimir Palant 2016/03/23 16:34:09 So the issue is apparently that EventEmitter const
+
/**
* Adds a listener
+ *
+ * @deprecated use FilterNotifier.on(action, callback)
*/
addListener: function(/**function(action, item, newValue, oldValue)*/ listener)
{
- if (listeners.indexOf(listener) >= 0)
- return;
-
- listeners.push(listener);
+ let listeners = this._listeners[CATCH_ALL];
+ if (!listeners || listeners.indexOf(listener) == -1)
+ this.on(CATCH_ALL, listener);
},
/**
* Removes a listener that was previosly added via addListener
+ *
+ * @deprecated use FilterNotifier.off(action, callback)
*/
removeListener: function(/**function(action, item, newValue, oldValue)*/ listener)
{
- let index = listeners.indexOf(listener);
- if (index >= 0)
- listeners.splice(index, 1);
+ this.off(CATCH_ALL, listener);
},
/**
@@ -63,11 +64,11 @@
* "filter.added", "filter.removed", "filter.moved",
* "filter.disabled", "filter.hitCount", "filter.lastHit")
* @param {Subscription|Filter} item item that the change applies to
+ * @deprecated use FilterNotifier.emit(action)
*/
triggerListeners: function(action, item, param1, param2, param3)
{
- let list = listeners.slice();
- for (let listener of list)
- listener(action, item, param1, param2, param3);
+ this.emit(action, item, param1, param2, param3);
+ this.emit(CATCH_ALL, action, item, param1, param2, param3);
}
};
« lib/events.js ('K') | « lib/events.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld