From 5e083966965804741ac091a3cc500f75821db2e7 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 26 Mar 2024 13:31:27 +0100 Subject: [PATCH] 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. --- web/preferences.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/web/preferences.js b/web/preferences.js index 9b764d90867cd..15da2cc584440 100644 --- a/web/preferences.js +++ b/web/preferences.js @@ -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];