1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

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:
Jonas Jenwald 2024-03-26 13:31:27 +01:00
parent 3d7ea6076d
commit 5e08396696

View file

@ -54,18 +54,15 @@ class BasePreferences {
({ browserPrefs, prefs }) => {
const options = Object.create(null);
for (const [name, defaultVal] of Object.entries(
this.#browserDefaults
)) {
for (const [name, val] of Object.entries(this.#browserDefaults)) {
const prefVal = browserPrefs?.[name];
options[name] =
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
options[name] = typeof prefVal === typeof val ? prefVal : val;
}
for (const [name, defaultVal] of Object.entries(this.#defaults)) {
for (const [name, val] of Object.entries(this.#defaults)) {
const prefVal = prefs?.[name];
// Ignore preferences whose types don't match the default values.
options[name] = this.#prefs[name] =
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
typeof prefVal === typeof val ? prefVal : val;
}
AppOptions.setAll(options, /* init = */ true);
@ -128,14 +125,16 @@ class BasePreferences {
throw new Error("Please use `about:config` to change preferences.");
}
await this.#initializedPromise;
const prefs = this.#prefs;
const oldPrefs = structuredClone(this.#prefs);
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.
this.#prefs = prefs;
this.#prefs = oldPrefs;
throw reason;
});
}
}
/**
@ -151,7 +150,7 @@ class BasePreferences {
}
await this.#initializedPromise;
const defaultValue = this.#defaults[name],
prefs = this.#prefs;
oldPrefs = structuredClone(this.#prefs);
if (defaultValue === undefined) {
throw new Error(`Set preference: "${name}" is undefined.`);
@ -174,11 +173,13 @@ class BasePreferences {
}
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.
this.#prefs = prefs;
this.#prefs = oldPrefs;
throw reason;
});
}
}
/**
@ -188,6 +189,9 @@ class BasePreferences {
* containing the value of the preference.
*/
async get(name) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: get");
}
await this.#initializedPromise;
const defaultValue = this.#defaults[name];