From 7adcb90a2d7e22cb6b9d9e7ed470d6b72417ed82 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 16 Nov 2020 13:56:58 +0100 Subject: [PATCH] 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. --- web/preferences.js | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/web/preferences.js b/web/preferences.js index d21e02f749cfc..7edbd628f55c7 100644 --- a/web/preferences.js +++ b/web/preferences.js @@ -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; - - 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; } @@ -72,7 +55,8 @@ class BasePreferences { } this.prefs[name] = prefValue; } - }); + } + ); } /**