[Pkg-mozext-commits] [adblock-plus] 54/87: Issue 3862 - Add EventEmitter class and implement in the FilterNotifier

David Prévot taffit at moszumanska.debian.org
Sat Apr 30 17:59:08 UTC 2016


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit 2559f2ff9cdd06fb83a1b47b3c4cc8ea1a9b4599
Author: Sebastian Noack <sebastian at adblockplus.org>
Date:   Wed Mar 23 13:00:17 2016 +0100

    Issue 3862 - Add EventEmitter class and implement in the FilterNotifier
---
 lib/events.js         | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/filterNotifier.js |  33 ++++++++--------
 2 files changed, 119 insertions(+), 16 deletions(-)

diff --git a/lib/events.js b/lib/events.js
new file mode 100644
index 0000000..a72575a
--- /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 named events.
+ *
+ * @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(resolve =>
+    {
+      let listener = () =>
+      {
+        this.off(name, listener);
+        resolve();
+      };
+
+      this.on(name, listener);
+    });
+  },
+
+  /**
+   * Calls all previously added listeners for the given event name.
+   *
+   * @param {string} name
+   * @param {...*}   [arg]
+   */
+  emit: function(name)
+  {
+    let listeners = this._listeners[name];
+    if (listeners)
+    {
+      let args = [];
+      for (let i = 1; i < arguments.length; i++)
+        args.push(arguments[i]);
+
+      let currentListeners = listeners.slice();
+      for (let listener of currentListeners)
+        listener.apply(null, args);
+    }
+  }
+};
diff --git a/lib/filterNotifier.js b/lib/filterNotifier.js
index 2d4ac19..8e24ff7 100644
--- 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 @@ let FilterNotifier = exports.FilterNotifier =
    *                 "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);
   }
 };

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list