Open Bug 563725 Opened 14 years ago Updated 2 years ago

Want a JSM to assist with syncing JS vars to preferences

Categories

(Firefox :: General, defect)

defect

Tracking

()

People

(Reporter: Dolske, Unassigned)

Details

Mossop mentioned the basic idea, and I hear zpao was already working on something like it!

In a nutshell, the observation is that we have a common annoying pattern of "var foo = Services.prefs.getBranch("bar").getBoolValue()" plus adding observer goop to keep the local |foo| in sync with the bar.foo pref as it changes.

It would be super awesome to have a JSM to assist with this. For example:

/* Prefs: browser.bacon.debug, browser.bacon.foo */
Components.utils.import("resource://gre/modules/Prefs.jsm");
let prefs = Prefs.initFromBranch("browser.bacon");
if (prefs.debug)
   dump("foo is " + prefs.foo);

Might be interesting to have a way to have a |prefs| that containts just a handful of prefs, perhaps from multiple branches, and perhaps with subbranches (prefs.foo.bar.baz) -- but this is the common case that would be super awesome to make more convenient.

Bonus points for making this work as above, where the preftype (int/bool/string) is automagically handled.

Note that this is mainly useful for _reading_ prefs; I'm assuming _setting_ prefs would still be done the old way, but perhaps there is some setter magic that could be done to make |prefs.foo = 123| actually work.
I don't see a reason why setters wouldn't work fine, the real question is how do you handle the case where you want a default value for unset prefs?
Hmm. You could just set it up initially as |prefs.secretFoo = 456|, which works for reading but would be misleading if you changed the value, expecting it to sync. We don't have wildcard getters/setters yet, unfortunately.

I suppose one answer would be something like:

Prefs.setHiddenPrefDefault(prefs, "secretFoo", 456), which would create the default value, the setter, and ensure observer stuff works.

Related, an earlier thought for this API was to do something like:

let prefs = {
  debug : false,
  foo   : 1
}
Prefs.init("browser.bacon", prefs);

You could easily add a "hiddenFoo: 456" there. I wasn't sure if this scheme was really useful, though. Mainly because you either tediously set default values which are then immediately overwritten by the values from the real prefs (which gets confusing, too), or have odd objects like |{ debug: null, foo: null }|.

This does work nicely for hidden prefs, and it also let you have just an explicit subset of available prefs, but I'm not really fond of the scheme.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Proxy ?

Although you may want to ask the JS team about it - I heard the spec is being changed or something.
I still think this would be handy, but I'm not working on it.
Assignee: paul → nobody
The JS team is currently implementing direct proxies. See bug 703537. I think this feature would be an excellent use case for direct proxies.
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.