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: Use arrow functions Created March 23, 2016, 5:06 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 | « 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(),
+
/**
* 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);
}
};
« no previous file with comments | « lib/events.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld