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:
parent
25d7420035
commit
3274972768
@ -21,8 +21,7 @@ import { GenericScripting } from "./generic_scripting.js";
|
|||||||
|
|
||||||
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
|
if (typeof PDFJSDev !== "undefined" && !PDFJSDev.test("GENERIC")) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Module "pdfjs-web/genericcom" shall not be used outside ' +
|
'Module "pdfjs-web/genericcom" shall not be used outside GENERIC build.'
|
||||||
"GENERIC build."
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,8 +84,14 @@ class BasePreferences {
|
|||||||
*/
|
*/
|
||||||
async reset() {
|
async reset() {
|
||||||
await this.#initializedPromise;
|
await this.#initializedPromise;
|
||||||
|
const prefs = this.#prefs;
|
||||||
|
|
||||||
this.#prefs = Object.create(null);
|
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) {
|
async set(name, value) {
|
||||||
await this.#initializedPromise;
|
await this.#initializedPromise;
|
||||||
const defaultValue = this.#defaults[name];
|
const defaultValue = this.#defaults[name],
|
||||||
|
prefs = this.#prefs;
|
||||||
|
|
||||||
if (defaultValue === undefined) {
|
if (defaultValue === undefined) {
|
||||||
throw new Error(`Set preference: "${name}" is undefined.`);
|
throw new Error(`Set preference: "${name}" is undefined.`);
|
||||||
} else if (value === undefined) {
|
} else if (value === undefined) {
|
||||||
throw new Error("Set preference: no value is specified.");
|
throw new Error("Set preference: no value is specified.");
|
||||||
}
|
}
|
||||||
const valueType = typeof value;
|
const valueType = typeof value,
|
||||||
const defaultType = typeof defaultValue;
|
defaultType = typeof defaultValue;
|
||||||
|
|
||||||
if (valueType !== defaultType) {
|
if (valueType !== defaultType) {
|
||||||
if (valueType === "number" && defaultType === "string") {
|
if (valueType === "number" && defaultType === "string") {
|
||||||
@ -120,8 +127,13 @@ class BasePreferences {
|
|||||||
throw new Error(`Set preference: "${value}" must be an integer.`);
|
throw new Error(`Set preference: "${value}" must be an integer.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.#prefs[name] = value;
|
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;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user