Skip to content

Instantly share code, notes, and snippets.

@itzexor
Created April 17, 2018 03:48
Show Gist options
  • Save itzexor/9101e75d62ea992dcc2b9eacaac8be71 to your computer and use it in GitHub Desktop.
Save itzexor/9101e75d62ea992dcc2b9eacaac8be71 to your computer and use it in GitHub Desktop.
in-js gsettings bind/cache
class EffectSettings {
constructor() {
this._values = {};
}
get_string(key) {
return this._getValue(key, 'string');
}
get_int(key) {
return this._getValue(key, 'int');
}
get_boolean(key) {
return this._getValue(key, 'boolean');
}
_getValue(key, type) {
if (!this._values.hasOwnProperty(key))
this._bind(key, type);
return this._values[key];
}
_bind(key, type) {
if (!global.settings.list_keys().includes(key))
throw new Error("invalid key " + key);
// "Note that settings only emits this signal if you have read key at
// least once while a signal handler was already connected for key."
global.settings.connect("changed::" + key, (src, key) => {
this._values[key] = Gio[`_real_get_${type}`].call(global.settings, key);
});
this._values[key] = Gio[`_real_get_${type}`].call(global.settings, key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment