Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce, now unnecessary, asynchronicity in the BasePreferences constructor #12630

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 15 additions & 31 deletions web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,6 @@

import { AppOptions, OptionKind } from "./app_options.js";

let defaultPreferences = null;
function getDefaultPreferences() {
if (!defaultPreferences) {
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
defaultPreferences = Promise.resolve(
AppOptions.getAll(OptionKind.PREFERENCE)
);
} else {
defaultPreferences = Promise.resolve(
PDFJSDev.json("$ROOT/build/default_preferences.json")
);
}
}
return defaultPreferences;
}

/**
* BasePreferences - Abstract base class for storing persistent settings.
* Used for settings that should be applied to all opened documents,
Expand All @@ -41,21 +25,20 @@ class BasePreferences {
if (this.constructor === BasePreferences) {
throw new Error("Cannot initialize BasePreferences.");
}
this.prefs = null;

this._initializedPromise = getDefaultPreferences()
.then(defaults => {
Object.defineProperty(this, "defaults", {
value: Object.freeze(defaults),
writable: false,
enumerable: true,
configurable: false,
});
Object.defineProperty(this, "defaults", {
value: Object.freeze(
typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")
? AppOptions.getAll(OptionKind.PREFERENCE)
: PDFJSDev.json("$ROOT/build/default_preferences.json")
),
writable: false,
enumerable: true,
configurable: false,
});
this.prefs = Object.assign(Object.create(null), this.defaults);

this.prefs = Object.assign(Object.create(null), defaults);
return this._readFromStorage(defaults);
})
.then(prefs => {
this._initializedPromise = this._readFromStorage(this.defaults).then(
prefs => {
if (!prefs) {
return;
}
Expand All @@ -72,7 +55,8 @@ class BasePreferences {
}
this.prefs[name] = prefValue;
}
});
}
);
}

/**
Expand Down