summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorKris Maglione <maglione.k@gmail.com>2008-12-20 14:36:11 -0500
committerKris Maglione <maglione.k@gmail.com>2008-12-20 14:36:11 -0500
commitef827139e4ce946b2fda88f3e28390f80ddbe5d7 (patch)
tree38c3f3ce143fedb12ac4cb19b013f8bfd6bfcbcf /common
parente1d5236261a32c9fe34a43b63034ce72e8d7be93 (diff)
downloadpentadactyl-ef827139e4ce946b2fda88f3e28390f80ddbe5d7.tar.gz
Tie storage observers to a weak reference of window so they're automatically destroyed.
Diffstat (limited to 'common')
-rw-r--r--common/content/options.js5
-rw-r--r--common/modules/storage.jsm24
2 files changed, 20 insertions, 9 deletions
diff --git a/common/content/options.js b/common/content/options.js
index 0a244ab3..0a1008ff 100644
--- a/common/content/options.js
+++ b/common/content/options.js
@@ -320,10 +320,7 @@ function Options() //{{{
}
storage.newMap("options", false);
- storage.addObserver("options", optionObserver);
- liberator.registerObserver("shutdown", function () {
- storage.removeObserver("options", optionObserver);
- });
+ storage.addObserver("options", optionObserver, window);
function storePreference(name, value)
{
diff --git a/common/modules/storage.jsm b/common/modules/storage.jsm
index ffb96d2d..61ebf01e 100644
--- a/common/modules/storage.jsm
+++ b/common/modules/storage.jsm
@@ -253,27 +253,41 @@ var storage = {
return this.newObject(key, ArrayStore, store, Array);
},
- addObserver: function addObserver(key, callback)
+ addObserver: function addObserver(key, callback, ref)
{
+ this.removeDeadObservers();
if (!(key in observers))
observers[key] = [];
if (observers[key].indexOf(callback) == -1)
- observers[key].push(callback);
+ observers[key].push({ ref: ref && Components.utils.getWeakReference(ref), callback: callback });
},
removeObserver: function (key, callback)
{
+ this.removeDeadObservers();
if (!(key in observers))
return;
- observers[key] = observers[key].filter(function (elem) elem != callback);
+ observers[key] = observers[key].filter(function (elem) elem.callback != callback);
if (observers[key].length == 0)
delete obsevers[key];
},
+ removeDeadObservers: function ()
+ {
+ for (let [key, ary] in Iterator(observers))
+ {
+ observers[key] = ary = ary.filter(function (o) !o.ref || o.ref.get());
+ if (!ary.length)
+ delete observers[key];
+ }
+ },
+
fireEvent: function fireEvent(key, event, arg)
{
- for each (callback in observers[key])
- callback(key, event, arg);
+ this.removeDeadObservers();
+ // Safe, since we have our own Array object here.
+ for each (let observer in observers[key])
+ observer.callback(key, event, arg);
timers[key].tell();
},