1
0
mirror of https://github.com/moparisthebest/FireTray synced 2024-11-16 05:45:01 -05:00
FireTray/src/modules/PrefListener.jsm

51 lines
1.5 KiB
JavaScript
Raw Normal View History

/* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
// https://developer.mozilla.org/en/Code_snippets/Preferences
var EXPORTED_SYMBOLS = [ "PrefListener" ];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
/**
* @constructor
*
* @param {string} branch_name
* @param {Function} callback must have the following arguments:
* branch, pref_leaf_name
*/
function PrefListener(branch_name, callback) {
// Keeping a reference to the observed preference branch or it will get
// garbage collected.
var prefService = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
this._branch = prefService.getBranch(branch_name);
this._branch.QueryInterface(Ci.nsIPrefBranch);
this._callback = callback;
}
PrefListener.prototype.observe = function(subject, topic, data) {
if (topic == 'nsPref:changed')
this._callback(this._branch, data);
};
/**
* @param {boolean=} trigger if true triggers the registered function
* on registration, that is, when this method is called.
*/
PrefListener.prototype.register = function(trigger) {
this._branch.addObserver('', this, false);
if (trigger) {
let that = this;
this._branch.getChildList('', {}).
forEach(function (pref_leaf_name)
{ that._callback(that._branch, pref_leaf_name); });
}
};
PrefListener.prototype.unregister = function() {
if (this._branch)
this._branch.removeObserver('', this);
};