Skip to content

Commit

Permalink
Reduce, now unnecessary, asynchronicity in the BasePreferences cons…
Browse files Browse the repository at this point in the history
…tructor

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 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.
  • Loading branch information
Snuffleupagus committed Nov 16, 2020
1 parent ed27fc6 commit 29cf1b2
Showing 1 changed file with 15 additions and 31 deletions.
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

0 comments on commit 29cf1b2

Please sign in to comment.