A couple of small tweaks of the BasePreferences class
- Use slightly shorter variable names when initializing the preferences. - Correctly copy the "old" preference-values before writing to storage, since Objects are passed by reference in JavaScript. (Only applies to the GENERIC viewer.) - Use `await` fully when writing new preference-values to storage. (Only applies to the GENERIC viewer.) - Stub the `get`-method in the Firefox PDF Viewer, since it's unused there nowadays.
This commit is contained in:
parent
3d7ea6076d
commit
5e08396696
@ -54,18 +54,15 @@ class BasePreferences {
|
|||||||
({ browserPrefs, prefs }) => {
|
({ browserPrefs, prefs }) => {
|
||||||
const options = Object.create(null);
|
const options = Object.create(null);
|
||||||
|
|
||||||
for (const [name, defaultVal] of Object.entries(
|
for (const [name, val] of Object.entries(this.#browserDefaults)) {
|
||||||
this.#browserDefaults
|
|
||||||
)) {
|
|
||||||
const prefVal = browserPrefs?.[name];
|
const prefVal = browserPrefs?.[name];
|
||||||
options[name] =
|
options[name] = typeof prefVal === typeof val ? prefVal : val;
|
||||||
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
|
|
||||||
}
|
}
|
||||||
for (const [name, defaultVal] of Object.entries(this.#defaults)) {
|
for (const [name, val] of Object.entries(this.#defaults)) {
|
||||||
const prefVal = prefs?.[name];
|
const prefVal = prefs?.[name];
|
||||||
// Ignore preferences whose types don't match the default values.
|
// Ignore preferences whose types don't match the default values.
|
||||||
options[name] = this.#prefs[name] =
|
options[name] = this.#prefs[name] =
|
||||||
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
|
typeof prefVal === typeof val ? prefVal : val;
|
||||||
}
|
}
|
||||||
AppOptions.setAll(options, /* init = */ true);
|
AppOptions.setAll(options, /* init = */ true);
|
||||||
|
|
||||||
@ -128,14 +125,16 @@ class BasePreferences {
|
|||||||
throw new Error("Please use `about:config` to change preferences.");
|
throw new Error("Please use `about:config` to change preferences.");
|
||||||
}
|
}
|
||||||
await this.#initializedPromise;
|
await this.#initializedPromise;
|
||||||
const prefs = this.#prefs;
|
const oldPrefs = structuredClone(this.#prefs);
|
||||||
|
|
||||||
this.#prefs = Object.create(null);
|
this.#prefs = Object.create(null);
|
||||||
return this._writeToStorage(this.#defaults).catch(reason => {
|
try {
|
||||||
|
await this._writeToStorage(this.#defaults);
|
||||||
|
} catch (reason) {
|
||||||
// Revert all preference values, since writing to storage failed.
|
// Revert all preference values, since writing to storage failed.
|
||||||
this.#prefs = prefs;
|
this.#prefs = oldPrefs;
|
||||||
throw reason;
|
throw reason;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,7 +150,7 @@ class BasePreferences {
|
|||||||
}
|
}
|
||||||
await this.#initializedPromise;
|
await this.#initializedPromise;
|
||||||
const defaultValue = this.#defaults[name],
|
const defaultValue = this.#defaults[name],
|
||||||
prefs = this.#prefs;
|
oldPrefs = structuredClone(this.#prefs);
|
||||||
|
|
||||||
if (defaultValue === undefined) {
|
if (defaultValue === undefined) {
|
||||||
throw new Error(`Set preference: "${name}" is undefined.`);
|
throw new Error(`Set preference: "${name}" is undefined.`);
|
||||||
@ -174,11 +173,13 @@ class BasePreferences {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.#prefs[name] = value;
|
this.#prefs[name] = value;
|
||||||
return this._writeToStorage(this.#prefs).catch(reason => {
|
try {
|
||||||
|
await this._writeToStorage(this.#prefs);
|
||||||
|
} catch (reason) {
|
||||||
// Revert all preference values, since writing to storage failed.
|
// Revert all preference values, since writing to storage failed.
|
||||||
this.#prefs = prefs;
|
this.#prefs = oldPrefs;
|
||||||
throw reason;
|
throw reason;
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -188,6 +189,9 @@ class BasePreferences {
|
|||||||
* containing the value of the preference.
|
* containing the value of the preference.
|
||||||
*/
|
*/
|
||||||
async get(name) {
|
async get(name) {
|
||||||
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||||
|
throw new Error("Not implemented: get");
|
||||||
|
}
|
||||||
await this.#initializedPromise;
|
await this.#initializedPromise;
|
||||||
const defaultValue = this.#defaults[name];
|
const defaultValue = this.#defaults[name];
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user