Skip to content

Commit

Permalink
A couple of small tweaks of the BasePreferences class
Browse files Browse the repository at this point in the history
 - 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.
  • Loading branch information
Snuffleupagus committed Mar 26, 2024
1 parent 3d7ea60 commit 5e08396
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
});
}
}

/**
Expand All @@ -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.`);
Expand All @@ -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;
});
}
}

/**
Expand All @@ -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];

Expand Down

0 comments on commit 5e08396

Please sign in to comment.