123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { AppOptions, OptionKind } from "./app_options.js";
- class BasePreferences {
- #defaults = Object.freeze(
- typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")
- ? AppOptions.getAll(OptionKind.PREFERENCE)
- : PDFJSDev.eval("DEFAULT_PREFERENCES")
- );
- #prefs = Object.create(null);
- #initializedPromise = null;
- constructor() {
- if (this.constructor === BasePreferences) {
- throw new Error("Cannot initialize BasePreferences.");
- }
- if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
- Object.defineProperty(this, "defaults", {
- get() {
- return this.#defaults;
- },
- });
- }
- this.#initializedPromise = this._readFromStorage(this.#defaults).then(
- prefs => {
- for (const name in this.#defaults) {
- const prefValue = prefs?.[name];
-
- if (typeof prefValue === typeof this.#defaults[name]) {
- this.#prefs[name] = prefValue;
- }
- }
- }
- );
- }
-
- async _writeToStorage(prefObj) {
- throw new Error("Not implemented: _writeToStorage");
- }
-
- async _readFromStorage(prefObj) {
- throw new Error("Not implemented: _readFromStorage");
- }
-
- async reset() {
- await this.#initializedPromise;
- const prefs = this.#prefs;
- this.#prefs = Object.create(null);
- return this._writeToStorage(this.#defaults).catch(reason => {
-
- this.#prefs = prefs;
- throw reason;
- });
- }
-
- async set(name, value) {
- await this.#initializedPromise;
- 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,
- 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;
- return this._writeToStorage(this.#prefs).catch(reason => {
-
- this.#prefs = prefs;
- throw reason;
- });
- }
-
- async get(name) {
- await this.#initializedPromise;
- const defaultValue = this.#defaults[name];
- if (defaultValue === undefined) {
- throw new Error(`Get preference: "${name}" is undefined.`);
- }
- return this.#prefs[name] ?? defaultValue;
- }
-
- async getAll() {
- await this.#initializedPromise;
- const obj = Object.create(null);
- for (const name in this.#defaults) {
- obj[name] = this.#prefs[name] ?? this.#defaults[name];
- }
- return obj;
- }
- }
- export { BasePreferences };
|