[Pkg-mozext-commits] [requestpolicy] 31/280: [refactoring] settings: simplify the observer(s)

David Prévot taffit at moszumanska.debian.org
Sat May 2 20:29:56 UTC 2015


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

taffit pushed a commit to branch master
in repository requestpolicy.

commit fbc365cd626b3e44bcc2d3997a01b72b951ae048
Author: Martin Kimmerle <dev at 256k.de>
Date:   Tue Nov 25 23:27:11 2014 +0100

    [refactoring] settings: simplify the observer(s)
---
 src/content/settings/advancedprefs.js           |  9 +---
 src/content/settings/basicprefs.js              |  9 +---
 src/content/settings/common.js                  | 27 ++---------
 src/content/settings/common.observer-manager.js | 62 +++++++++++++++++++++++++
 src/content/settings/defaultpolicy.js           |  9 +---
 src/content/settings/subscriptions.js           | 12 ++---
 src/content/settings/yourpolicy.js              | 18 ++++---
 7 files changed, 84 insertions(+), 62 deletions(-)

diff --git a/src/content/settings/advancedprefs.js b/src/content/settings/advancedprefs.js
index f154135..2773b23 100644
--- a/src/content/settings/advancedprefs.js
+++ b/src/content/settings/advancedprefs.js
@@ -131,11 +131,6 @@ function onload() {
       }
   );
 
-  prefsChangedObserver = new common.PrefsChangedObserver(
-      function(subject, topic, data) {
-        updateDisplay();
-      });
-  window.addEventListener("beforeunload", function(event) {
-    prefsChangedObserver.unregister();
-  });
+  // call updateDisplay() every time a preference gets changed
+  ObserverManager.observePrefChanges(updateDisplay);
 }
diff --git a/src/content/settings/basicprefs.js b/src/content/settings/basicprefs.js
index c730a13..738dfa2 100644
--- a/src/content/settings/basicprefs.js
+++ b/src/content/settings/basicprefs.js
@@ -74,11 +74,6 @@ function onload() {
       }
   );
 
-  prefsChangedObserver = new common.PrefsChangedObserver(
-      function(subject, topic, data) {
-        updateDisplay();
-      });
-  window.addEventListener("beforeunload", function(event) {
-    prefsChangedObserver.unregister();
-  });
+  // call updateDisplay() every time a preference gets changed
+  ObserverManager.observePrefChanges(updateDisplay);
 }
diff --git a/src/content/settings/common.js b/src/content/settings/common.js
index 772058d..ce29fc2 100644
--- a/src/content/settings/common.js
+++ b/src/content/settings/common.js
@@ -4,6 +4,8 @@ Components.utils.import("chrome://requestpolicy/content/lib/subscription.jsm");
 Components.utils.import("chrome://requestpolicy/content/lib/utils.jsm");
 Components.utils.import("chrome://requestpolicy/content/lib/policy-manager.jsm");
 
+Components.utils.import("resource://gre/modules/Services.jsm");
+
 var rpService = Components.classes["@requestpolicy.com/requestpolicy-service;1"]
     .getService().wrappedJSObject;
 
@@ -34,7 +36,7 @@ function _(msg, args) {
   }
 }
 
-common = {};
+var common = {};
 
 /*
  Based on the user's current default policy (allow or deny), swaps out which
@@ -217,28 +219,9 @@ common.localize = function(stringNames) {
 };
 
 
-common.Observer = function(functionToCall, aTopic) {
-  this.topic = aTopic;
-  this.observe = functionToCall;
-  this.register();
-}
-common.Observer.prototype.register = function() {
-  var observerService = Components.classes["@mozilla.org/observer-service;1"].
-      getService(Components.interfaces.nsIObserverService);
-  observerService.addObserver(this, this.topic, false);
-};
-common.Observer.prototype.unregister = function() {
-  var observerService = Components.classes["@mozilla.org/observer-service;1"].
-      getService(Components.interfaces.nsIObserverService);
-  observerService.removeObserver(this, this.topic);
-};
+Services.scriptloader.loadSubScript(
+    "chrome://requestpolicy/content/settings/common.observer-manager.js", this);
 
-common.PrefsChangedObserver = function(functionToCall)
-{
-  common.Observer.call(this, functionToCall, "requestpolicy-prefs-changed");
-}
-common.PrefsChangedObserver.prototype = Object.create(common.Observer.prototype);
-common.PrefsChangedObserver.prototype.constructor = common.PrefsChangedObserver;
 
 $(function() {
   common.localize(COMMON_STRINGS);
diff --git a/src/content/settings/common.observer-manager.js b/src/content/settings/common.observer-manager.js
new file mode 100644
index 0000000..4a177b8
--- /dev/null
+++ b/src/content/settings/common.observer-manager.js
@@ -0,0 +1,62 @@
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ *
+ * RequestPolicy - A Firefox extension for control over cross-site requests.
+ * Copyright (c) 2008-2012 Justin Samuel
+ * Copyright (c) 2014 Martin Kimmerle
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * This program 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
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * ***** END LICENSE BLOCK *****
+ */
+
+var ObserverManager = (function() {
+  var self = {};
+
+  // an object holding all observers for unregistering when unloading the page
+  var observers = [];
+
+  function Observer(aTopic, aFunctionToCall) {
+    this.topic = aTopic;
+    this.observe = aFunctionToCall;
+    this.register();
+    observers.push(this);
+  }
+  Observer.prototype.register = function() {
+    Services.obs.addObserver(this, this.topic, false);
+  };
+  Observer.prototype.unregister = function() {
+    Services.obs.removeObserver(this, this.topic);
+  };
+
+
+  // unregister all observers before the window is unloaded
+  window.addEventListener("beforeunload", function(event) {
+    while (observers.length > 0) {
+      var observer = observers.pop();
+      Logger.dump("Unregistering observer for topic " + observer.topic);
+      observer.unregister();
+    }
+  });
+
+
+  self.observe = function(aTopic, aFunctionToCall) {
+    return new Observer(aTopic, aFunctionToCall);
+  };
+
+  self.observePrefChanges = self.observe.bind(this,
+      "requestpolicy-prefs-changed");
+
+  return self;
+}());
diff --git a/src/content/settings/defaultpolicy.js b/src/content/settings/defaultpolicy.js
index a199595..0485549 100644
--- a/src/content/settings/defaultpolicy.js
+++ b/src/content/settings/defaultpolicy.js
@@ -72,11 +72,6 @@ function onload() {
       }
   );
 
-  prefsChangedObserver = new common.PrefsChangedObserver(
-      function(subject, topic, data) {
-        updateDisplay();
-      });
-  window.addEventListener("beforeunload", function(event) {
-    prefsChangedObserver.unregister();
-  });
+  // call updateDisplay() every time a preference gets changed
+  ObserverManager.observePrefChanges(updateDisplay);
 }
diff --git a/src/content/settings/subscriptions.js b/src/content/settings/subscriptions.js
index 37eb226..923ab16 100644
--- a/src/content/settings/subscriptions.js
+++ b/src/content/settings/subscriptions.js
@@ -126,13 +126,7 @@ function onload() {
     el.addEventListener('change', handleSubscriptionCheckboxChange);
   }
 
-  subAddedObserver = new common.Observer(updateDisplay,
-                                         SUBSCRIPTION_ADDED_TOPIC);
-  subRemovedObserver = new common.Observer(updateDisplay,
-                                           SUBSCRIPTION_REMOVED_TOPIC);
-
-  window.addEventListener("beforeunload", function(event) {
-    subAddedObserver.unregister();
-    subRemovedObserver.unregister();
-  });
+  // call updateDisplay() every time a subscription is added or removed
+  ObserverManager.observe(SUBSCRIPTION_ADDED_TOPIC, updateDisplay);
+  ObserverManager.observe(SUBSCRIPTION_REMOVED_TOPIC, updateDisplay);
 }
diff --git a/src/content/settings/yourpolicy.js b/src/content/settings/yourpolicy.js
index 5ddcf97..0099990 100644
--- a/src/content/settings/yourpolicy.js
+++ b/src/content/settings/yourpolicy.js
@@ -216,15 +216,7 @@ function addRuleHelper() {
   }
 }
 
-function RulesChangedObserver()
-{
-  common.Observer.call(this, function(subject, topic, data) {
-    var search = document.getElementById('rulesearch');
-    populateRuleTable(search.value);
-  }, "requestpolicy-rules-changed");
-}
-RulesChangedObserver.prototype = Object.create(common.Observer.prototype);
-RulesChangedObserver.prototype.constructor = RulesChangedObserver;
+
 
 function onload() {
   var search = document.getElementById('rulesearch');
@@ -241,7 +233,13 @@ function onload() {
     $('#oldrulesexist').show();
   }
 
-  rulesChangedObserver = new RulesChangedObserver();
+  // observe rule changes and update the table then
+  ObserverManager.observe("requestpolicy-rules-changed",
+      function(subject, topic, data) {
+        var search = document.getElementById('rulesearch');
+        populateRuleTable(search.value);
+      });
+
   window.addEventListener("beforeunload", function(event) {
     rulesChangedObserver.unregister();
   });

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



More information about the Pkg-mozext-commits mailing list