Index: lib/events.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/events.js |
@@ -0,0 +1,102 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+"use strict"; |
+ |
+/** |
+ * Registers and emits names events. |
Wladimir Palant
2016/03/23 16:01:07
names => named
Sebastian Noack
2016/03/23 16:23:43
Done.
|
+ * |
+ * @constructor |
+ */ |
+exports.EventEmitter = function() |
+{ |
+ this._listeners = Object.create(null); |
+}; |
+ |
+exports.EventEmitter.prototype = { |
+ /** |
+ * Adds a listener for the specified event name. |
+ * |
+ * @param {string} name |
+ * @param {function} listener |
+ */ |
+ on: function(name, listener) |
+ { |
+ if (name in this._listeners) |
+ this._listeners[name].push(listener); |
+ else |
+ this._listeners[name] = [listener]; |
+ }, |
+ |
+ /** |
+ * Removes a listener for the specified event name. |
+ * |
+ * @param {string} name |
+ * @param {function} listener |
+ */ |
+ off: function(name, listener) |
+ { |
+ let listeners = this._listeners[name]; |
+ if (listeners) |
+ { |
+ let idx = listeners.indexOf(listener); |
+ if (idx != -1) |
+ listeners.splice(idx, 1); |
+ } |
+ }, |
+ |
+ /** |
+ * Adds a one time listener and returns a promise that |
+ * is resolved the next time the specified event is emitted. |
+ * |
+ * @return {Promise} |
+ */ |
+ once: function(name) |
+ { |
+ return new Promise(function(resolve) |
+ { |
+ let listener = function() |
Wladimir Palant
2016/03/23 16:01:07
What about arguments? For this to be usable these
Sebastian Noack
2016/03/23 16:23:43
I thought about passing arguments, but passing in
|
+ { |
+ this.off(name, listener); |
+ resolve(); |
+ }.bind(this); |
+ |
+ this.on(name, listener); |
+ }.bind(this)); |
Wladimir Palant
2016/03/23 16:01:07
Use an arrow function instead of bind(this)?
Sebastian Noack
2016/03/23 16:23:43
I agree that automatically bound arrow functions w
Wladimir Palant
2016/03/23 16:34:09
It does: https://hg.adblockplus.org/jshydra/file/t
Sebastian Noack
2016/03/23 17:07:16
Awesome! I didn't realize because it only does if
|
+ }, |
+ |
+ /** |
+ * Calls all previously added listeners for the given event name. |
+ * |
+ * @param {string} name |
+ * @param {...*} [arg] |
+ */ |
+ emit: function(name) |
Wladimir Palant
2016/03/23 16:01:07
Heh, I can see where you got this from but naming
Sebastian Noack
2016/03/23 16:23:43
Well, that is just the regular node.js name for th
|
+ { |
+ let listeners = this._listeners[name]; |
+ if (listeners) |
+ { |
+ let args = []; |
+ for (let i = 1; i < arguments.length; i++) |
+ args.push(arguments[i]); |
Wladimir Palant
2016/03/23 16:01:07
Just use Array functions:
let args = [].slice.c
Sebastian Noack
2016/03/23 16:23:44
Passing the arguments object to a different functi
Wladimir Palant
2016/03/23 16:34:09
And rest parameters aren't supported before Chrome
|
+ |
+ let currentListeners = listeners.slice(); |
+ for (let listener of currentListeners) |
+ listener.apply(null, args); |
Wladimir Palant
2016/03/23 16:01:07
Catch errors here so that remaining listeners are
Sebastian Noack
2016/03/23 16:23:43
We didn't do that before for FilterStorage, Prefs,
|
+ } |
+ } |
+}; |