Merge pull request #13128 from Snuffleupagus/BasePreferences-loops
Re-factor how the `BasePreferences.prefs`-property is initialized; remove some *indirect* loops
This commit is contained in:
commit
9d0ce6e79f
@ -35,25 +35,16 @@ class BasePreferences {
|
|||||||
enumerable: true,
|
enumerable: true,
|
||||||
configurable: false,
|
configurable: false,
|
||||||
});
|
});
|
||||||
this.prefs = Object.assign(Object.create(null), this.defaults);
|
this.prefs = Object.create(null);
|
||||||
|
|
||||||
this._initializedPromise = this._readFromStorage(this.defaults).then(
|
this._initializedPromise = this._readFromStorage(this.defaults).then(
|
||||||
prefs => {
|
prefs => {
|
||||||
if (!prefs) {
|
for (const name in this.defaults) {
|
||||||
return;
|
const prefValue = prefs?.[name];
|
||||||
}
|
// Ignore preferences whose types don't match the default values.
|
||||||
for (const name in prefs) {
|
if (typeof prefValue === typeof this.defaults[name]) {
|
||||||
const defaultValue = this.defaults[name],
|
this.prefs[name] = prefValue;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
this.prefs[name] = prefValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@ -86,7 +77,7 @@ class BasePreferences {
|
|||||||
*/
|
*/
|
||||||
async reset() {
|
async reset() {
|
||||||
await this._initializedPromise;
|
await this._initializedPromise;
|
||||||
this.prefs = Object.assign(Object.create(null), this.defaults);
|
this.prefs = Object.create(null);
|
||||||
return this._writeToStorage(this.defaults);
|
return this._writeToStorage(this.defaults);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,8 +105,7 @@ class BasePreferences {
|
|||||||
value = value.toString();
|
value = value.toString();
|
||||||
} else {
|
} else {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Set preference: "${value}" is a ${valueType}, ` +
|
`Set preference: "${value}" is a ${valueType}, expected a ${defaultType}.`
|
||||||
`expected a ${defaultType}.`
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -135,18 +125,13 @@ class BasePreferences {
|
|||||||
*/
|
*/
|
||||||
async get(name) {
|
async get(name) {
|
||||||
await this._initializedPromise;
|
await this._initializedPromise;
|
||||||
const defaultValue = this.defaults[name];
|
const defaultValue = this.defaults[name],
|
||||||
|
prefValue = this.prefs[name];
|
||||||
|
|
||||||
if (defaultValue === undefined) {
|
if (defaultValue === undefined) {
|
||||||
throw new Error(`Get preference: "${name}" is undefined.`);
|
throw new Error(`Get preference: "${name}" is undefined.`);
|
||||||
} else {
|
|
||||||
const prefValue = this.prefs[name];
|
|
||||||
|
|
||||||
if (prefValue !== undefined) {
|
|
||||||
return prefValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return prefValue !== undefined ? prefValue : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -156,7 +141,13 @@ class BasePreferences {
|
|||||||
*/
|
*/
|
||||||
async getAll() {
|
async getAll() {
|
||||||
await this._initializedPromise;
|
await this._initializedPromise;
|
||||||
return Object.assign(Object.create(null), this.defaults, this.prefs);
|
const obj = Object.create(null);
|
||||||
|
|
||||||
|
for (const name in this.defaults) {
|
||||||
|
const prefValue = this.prefs[name];
|
||||||
|
obj[name] = prefValue !== undefined ? prefValue : this.defaults[name];
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user