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

Unified Diff: background.js

Issue 29338734: Issue 3839 - Adapt for Prefs event changes (Closed)
Patch Set: Cleaned up onFilterChange() Created March 22, 2016, 7:19 a.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 | messageResponder.js » ('j') | messageResponder.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: background.js
===================================================================
--- a/background.js
+++ b/background.js
@@ -17,32 +17,59 @@
(function(global)
{
+ function EventEmitter()
+ {
+ this._listeners = Object.create(null);
+ }
+ EventEmitter.prototype = {
+ on: function(name, listener)
+ {
+ if (name in this._listeners)
+ this._listeners[name].push(listener);
+ else
+ this._listeners[name] = [listener];
+ },
+ off: function(name, listener)
+ {
+ var listeners = this._listeners[name];
+ if (listeners)
+ {
+ var idx = listeners.indexOf(listener);
+ if (idx != -1)
+ listeners.splice(idx, 1);
+ }
+ },
+ emit: function(name)
+ {
+ var listeners = this._listeners[name];
+ if (listeners)
+ {
+ for (var i = 0; i < listeners.length; i++)
+ listeners[i].apply(null, Array.prototype.slice.call(arguments, 1));
+ }
+ }
+ };
+
function Notifier()
{
- this._listeners = [];
+ this._eventEmitter = new EventEmitter();
}
Notifier.prototype = {
- _listeners: null,
-
addListener: function(listener)
{
- if (this._listeners.indexOf(listener) < 0)
- this._listeners.push(listener);
+ var listeners = this._eventEmitter[""];
Thomas Greiner 2016/03/22 13:29:40 Detail: Shouldn't this be `var listeners = this._e
Sebastian Noack 2016/03/22 14:24:10 Well spotted, and actually it is a bug.
+ if (!listeners || listener.indexOf(listener) == -1)
+ this._eventEmitter.on("", listener);
},
-
removeListener: function(listener)
{
- var index = this._listeners.indexOf(listener);
- if (index >= 0)
- this._listeners.splice(index, 1);
+ this._eventEmitter.off("", listener);
},
-
triggerListeners: function()
{
var args = Array.prototype.slice.apply(arguments);
- var listeners = this._listeners.slice();
- for (var i = 0; i < listeners.length; i++)
- listeners[i].apply(null, args);
+ args.unshift("");
+ this._eventEmitter.emit.apply(this._eventEmitter, args);
}
};
@@ -91,11 +118,7 @@
}
};
- modules.prefs = {
- Prefs: {
- onChanged: new Notifier()
- }
- };
+ modules.prefs = {Prefs: new EventEmitter()};
var prefs = {
notifications_ignoredcategories: (params.showNotificationUI) ? ["*"] : [],
notifications_showui: params.showNotificationUI,
@@ -114,8 +137,7 @@
set: function(value)
{
prefs[key] = value;
- modules.prefs.Prefs.onChanged.triggerListeners(key);
- return prefs[key];
+ modules.prefs.Prefs.emit(key);
}
});
});
« no previous file with comments | « no previous file | messageResponder.js » ('j') | messageResponder.js » ('J')

Powered by Google App Engine
This is Rietveld