Revert Preferences to their previous values, when writing to storage failed

This patch fixes an old inconsistency, when using `BasePreferences.{reset, set}`, where the internal Preference values would be kept even if writing them to storage failed.
This commit is contained in:
Jonas Jenwald 2022-03-12 14:40:18 +01:00
parent 25d7420035
commit 3274972768
2 changed files with 18 additions and 7 deletions

View File

@ -21,8 +21,7 @@ import { GenericScripting } from "./generic_scripting.js";
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
throw new Error(
'Module "pdfjs-web/genericcom" shall not be used outside ' +
"GENERIC build."
'Module "pdfjs-web/genericcom" shall not be used outside GENERIC build.'
);
}

View File

@ -84,8 +84,14 @@ class BasePreferences {
*/
async reset() {
await this.#initializedPromise;
const prefs = this.#prefs;
this.#prefs = Object.create(null);
return this._writeToStorage(this.#defaults);
return this._writeToStorage(this.#defaults).catch(reason => {
// Revert all preference values, since writing to storage failed.
this.#prefs = prefs;
throw reason;
});
}
/**
@ -97,15 +103,16 @@ class BasePreferences {
*/
async set(name, value) {
await this.#initializedPromise;
const defaultValue = this.#defaults[name];
const defaultValue = this.#defaults[name],
prefs = this.#prefs;
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;
const defaultType = typeof defaultValue;
const valueType = typeof value,
defaultType = typeof defaultValue;
if (valueType !== defaultType) {
if (valueType === "number" && defaultType === "string") {
@ -120,8 +127,13 @@ class BasePreferences {
throw new Error(`Set preference: "${value}" must be an integer.`);
}
}
this.#prefs[name] = value;
return this._writeToStorage(this.#prefs);
return this._writeToStorage(this.#prefs).catch(reason => {
// Revert all preference values, since writing to storage failed.
this.#prefs = prefs;
throw reason;
});
}
/**