diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/content/options.js | 5 | ||||
-rw-r--r-- | common/modules/storage.jsm | 24 |
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(); }, |