-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprefwatcher.js
More file actions
39 lines (33 loc) · 1.13 KB
/
Copy pathprefwatcher.js
File metadata and controls
39 lines (33 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const {Cc, Ci, Cu, Cm} = require('chrome');
Cu.import('resource://gre/modules/Services.jsm', this);
// keyed on prefname, value is array of callbacks
let observers = {};
const Prefs = Services.prefs;
const Prefs2 = Prefs.QueryInterface(Ci.nsIPrefBranch2);
const PrefsJP = require('preferences-service');
exports.watch = function prefWatch(pref, callback) {
Prefs.addObserver(pref, prefObserver, false);
if (!observers[pref])
observers[pref] = [];
observers[pref].push(callback);
callback(PrefsJP.get(pref));
};
exports.unwatch = function prefUnwatch(pref, callback) {
if (observers[pref] && observers[pref].indexOf(callback) != -1)
observers[pref].splice(obervers[pref].indexOf(callback), 1);
};
let prefObserver = {
observe: function observe(s, t, pref) {
if (observers[pref]) {
let newVal = PrefsJP.get(pref);
console.log("updating ", pref, " to ", newVal);
observers[pref].forEach(function(callback) callback(newVal));
}
}
};
require('unload').when(function() {
for (let [pref, vars] in Iterator(observers)) {
Prefs2.removeObserver(pref, prefObserver);
observers[pref].splice(0);
}
});