Re-factor BasePreferences to essentially be a wrapper around AppOptions

In the MOZCENTRAL build the `BasePreferences` class is almost unused, since it's only being used to initialize and update the `AppOptions` which means that theoretically we could move the relevant code there.
However for the other build targets (e.g. GENERIC and CHROME) we still need to keep the `BasePreferences` class, although we can re-factor things to move the necessary validation inside of `AppOptions` and thus simplify the code and reduce duplication.

The patch also moves the event dispatching, for changed preference values, into `AppOptions` instead.
This commit is contained in:
Jonas Jenwald 2024-07-09 13:22:14 +02:00
parent 1bdd6920ff
commit d9f0ec0b87
4 changed files with 76 additions and 142 deletions

View file

@ -21,24 +21,14 @@ import { AppOptions, OptionKind } from "./app_options.js";
* or every time the viewer is loaded.
*/
class BasePreferences {
#browserDefaults = Object.freeze(
typeof PDFJSDev === "undefined"
? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true)
: PDFJSDev.eval("BROWSER_PREFERENCES")
);
#defaults = Object.freeze(
typeof PDFJSDev === "undefined"
? AppOptions.getAll(OptionKind.PREFERENCE, /* defaultOnly = */ true)
: PDFJSDev.eval("DEFAULT_PREFERENCES")
);
#prefs = Object.create(null);
#initializedPromise = null;
static #eventToDispatch = new Set(["toolbarDensity"]);
constructor() {
if (this.constructor === BasePreferences) {
throw new Error("Cannot initialize BasePreferences.");
@ -54,27 +44,24 @@ class BasePreferences {
this.#initializedPromise = this._readFromStorage(this.#defaults).then(
({ browserPrefs, prefs }) => {
const options = Object.create(null);
for (const [name, val] of Object.entries(this.#browserDefaults)) {
const prefVal = browserPrefs?.[name];
options[name] = typeof prefVal === typeof val ? prefVal : val;
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
AppOptions._checkDisablePreferences()
) {
return;
}
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 val ? prefVal : val;
}
AppOptions.setAll(options, /* init = */ true);
AppOptions.setAll({ ...browserPrefs, ...prefs }, /* prefs = */ true);
}
);
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
window.addEventListener("updatedPreference", evt => {
this.#updatePref(evt.detail);
});
this.eventBus = null;
window.addEventListener(
"updatedPreference",
async ({ detail: { name, value } }) => {
await this.#initializedPromise;
AppOptions.setAll({ [name]: value }, /* prefs = */ true);
}
);
}
}
@ -98,31 +85,6 @@ class BasePreferences {
throw new Error("Not implemented: _readFromStorage");
}
async #updatePref({ name, value }) {
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: #updatePref");
}
await this.#initializedPromise;
if (name in this.#browserDefaults) {
if (typeof value !== typeof this.#browserDefaults[name]) {
return; // Invalid preference value.
}
} else if (name in this.#defaults) {
if (typeof value !== typeof this.#defaults[name]) {
return; // Invalid preference value.
}
this.#prefs[name] = value;
} else {
return; // Invalid preference.
}
AppOptions.set(name, value);
if (BasePreferences.#eventToDispatch.has(name)) {
this.eventBus?.dispatch(name.toLowerCase(), { source: this, value });
}
}
/**
* Reset the preferences to their default values and update storage.
* @returns {Promise} A promise that is resolved when the preference values
@ -133,16 +95,9 @@ class BasePreferences {
throw new Error("Please use `about:config` to change preferences.");
}
await this.#initializedPromise;
const oldPrefs = structuredClone(this.#prefs);
AppOptions.setAll(this.#defaults, /* prefs = */ true);
this.#prefs = Object.create(null);
try {
await this._writeToStorage(this.#defaults);
} catch (reason) {
// Revert all preference values, since writing to storage failed.
this.#prefs = oldPrefs;
throw reason;
}
await this._writeToStorage(this.#defaults);
}
/**
@ -157,37 +112,10 @@ class BasePreferences {
throw new Error("Please use `about:config` to change preferences.");
}
await this.#initializedPromise;
const defaultValue = this.#defaults[name],
oldPrefs = structuredClone(this.#prefs);
AppOptions.setAll({ [name]: value }, /* prefs = */ true);
if (defaultValue === undefined) {
throw new Error(`Set preference: "${name}" is undefined.`);
} else if (value === undefined) {
throw new Error("Set preference: no value is specified.");
}
const valueType = typeof value,
defaultType = typeof defaultValue;
if (valueType !== defaultType) {
if (valueType === "number" && defaultType === "string") {
value = value.toString();
} else {
throw new Error(
`Set preference: "${value}" is a ${valueType}, expected a ${defaultType}.`
);
}
} else if (valueType === "number" && !Number.isInteger(value)) {
throw new Error(`Set preference: "${value}" must be an integer.`);
}
this.#prefs[name] = value;
try {
await this._writeToStorage(this.#prefs);
} catch (reason) {
// Revert all preference values, since writing to storage failed.
this.#prefs = oldPrefs;
throw reason;
}
const prefs = AppOptions.getAll(OptionKind.PREFERENCE);
await this._writeToStorage(prefs);
}
/**
@ -201,12 +129,7 @@ class BasePreferences {
throw new Error("Not implemented: get");
}
await this.#initializedPromise;
const defaultValue = this.#defaults[name];
if (defaultValue === undefined) {
throw new Error(`Get preference: "${name}" is undefined.`);
}
return this.#prefs[name] ?? defaultValue;
return AppOptions.get(name);
}
get initializedPromise() {