Reduce, now unnecessary, asynchronicity in the BasePreferences
constructor
Originally the default preferences were defined in a JSON-file checked into the repository, which was loaded using SystemJS in development mode. Over the years a number of changes have been made to this code, most notably: - The preferences JSON-file is now generated automatically, during building, from the `AppOptions` abstraction. - All SystemJS usage has been removed from the development viewer. Hence the default preferences are now available *synchronously* even in the development viewer, and it's thus no longer necessary to defer to the microtask queue (since `getDefaultPreferences` is async) just to get the default preferences. While the effect of these changes is quite small, it *does* reduces the time it takes for the preferences to be fully initialized. Given the amount of asynchronous code during viewer initialization, every bit of time that we can save should thus help.
This commit is contained in:
parent
ed27fc6c52
commit
7adcb90a2d
@ -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,
|
||||
@ -41,21 +25,20 @@ class BasePreferences {
|
||||
if (this.constructor === BasePreferences) {
|
||||
throw new Error("Cannot initialize BasePreferences.");
|
||||
}
|
||||
this.prefs = null;
|
||||
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._initializedPromise = getDefaultPreferences()
|
||||
.then(defaults => {
|
||||
Object.defineProperty(this, "defaults", {
|
||||
value: Object.freeze(defaults),
|
||||
writable: false,
|
||||
enumerable: true,
|
||||
configurable: false,
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
@ -72,7 +55,8 @@ class BasePreferences {
|
||||
}
|
||||
this.prefs[name] = prefValue;
|
||||
}
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user