Re-factor how the BasePreferences.prefs
-property is initialized
Looking at this now, I cannot understand why we'd need to initialize `this.prefs` with all of the values from `this.defaults`. Not only does this *indirectly* require one extra loop, via the `Object.assign`-call, but it also means that in GENERIC-builds changes to default-preference values might not be picked-up unless the the existing user-prefs are cleared (if the user had *manually* set prefs previously).
This commit is contained in:
parent
d426ffdad9
commit
4b27f58625
@ -35,25 +35,16 @@ class BasePreferences {
|
||||
enumerable: true,
|
||||
configurable: false,
|
||||
});
|
||||
this.prefs = Object.assign(Object.create(null), this.defaults);
|
||||
this.prefs = Object.create(null);
|
||||
|
||||
this._initializedPromise = this._readFromStorage(this.defaults).then(
|
||||
prefs => {
|
||||
if (!prefs) {
|
||||
return;
|
||||
}
|
||||
for (const name in prefs) {
|
||||
const defaultValue = this.defaults[name],
|
||||
prefValue = prefs[name];
|
||||
// Ignore preferences not present in, or whose types don't match,
|
||||
// the default values.
|
||||
if (
|
||||
defaultValue === undefined ||
|
||||
typeof prefValue !== typeof defaultValue
|
||||
) {
|
||||
continue;
|
||||
for (const name in this.defaults) {
|
||||
const prefValue = prefs?.[name];
|
||||
// Ignore preferences whose types don't match the default values.
|
||||
if (typeof prefValue === typeof this.defaults[name]) {
|
||||
this.prefs[name] = prefValue;
|
||||
}
|
||||
this.prefs[name] = prefValue;
|
||||
}
|
||||
}
|
||||
);
|
||||
@ -86,7 +77,7 @@ class BasePreferences {
|
||||
*/
|
||||
async reset() {
|
||||
await this._initializedPromise;
|
||||
this.prefs = Object.assign(Object.create(null), this.defaults);
|
||||
this.prefs = Object.create(null);
|
||||
return this._writeToStorage(this.defaults);
|
||||
}
|
||||
|
||||
@ -114,8 +105,7 @@ class BasePreferences {
|
||||
value = value.toString();
|
||||
} else {
|
||||
throw new Error(
|
||||
`Set preference: "${value}" is a ${valueType}, ` +
|
||||
`expected a ${defaultType}.`
|
||||
`Set preference: "${value}" is a ${valueType}, expected a ${defaultType}.`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user