Skip to content

Commit

Permalink
Merge pull request #17130 from Snuffleupagus/AppOption-move-init
Browse files Browse the repository at this point in the history
Try to make the `Preferences`/`AppOptions` initialization slightly more efficient
  • Loading branch information
Snuffleupagus authored Oct 19, 2023
2 parents 929311f + 5ddc949 commit d6cfbd2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 58 deletions.
44 changes: 11 additions & 33 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ const PDFViewerApplication = {

// Called once when the document is loaded.
async initialize(appConfig) {
this.preferences = this.externalServices.createPreferences();
this.appConfig = appConfig;

if (
Expand All @@ -245,7 +244,16 @@ const PDFViewerApplication = {
this._nimbusDataPromise = this.externalServices.getNimbusExperimentData();
}

await this._initializeOptions();
// Ensure that `Preferences`, and indirectly `AppOptions`, have initialized
// before creating e.g. the various viewer components.
try {
await this.preferences.initializedPromise;
} catch (ex) {
console.error(`initialize: "${ex.message}".`);
}
if (AppOptions.get("pdfBugEnabled")) {
await this._parseHashParams();
}
this._forceCssTheme();
await this._initializeL10n();

Expand Down Expand Up @@ -275,37 +283,6 @@ const PDFViewerApplication = {
this._initializedCapability.resolve();
},

/**
* @private
*/
async _initializeOptions() {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
if (AppOptions.get("disablePreferences")) {
if (AppOptions.get("pdfBugEnabled")) {
await this._parseHashParams();
}
// Give custom implementations of the default viewer a simpler way to
// opt-out of having the `Preferences` override existing `AppOptions`.
return;
}
if (AppOptions._hasUserOptions()) {
console.warn(
"_initializeOptions: The Preferences may override manually set AppOptions; " +
'please use the "disablePreferences"-option in order to prevent that.'
);
}
}
try {
AppOptions.setAll(await this.preferences.getAll());
} catch (reason) {
console.error(`_initializeOptions: "${reason.message}".`);
}

if (AppOptions.get("pdfBugEnabled")) {
await this._parseHashParams();
}
},

/**
* Potentially parse special debugging flags in the hash section of the URL.
* @private
Expand Down Expand Up @@ -697,6 +674,7 @@ const PDFViewerApplication = {
},

async run(config) {
this.preferences = this.externalServices.createPreferences();
await this.initialize(config);

const { appConfig, eventBus } = this;
Expand Down
22 changes: 15 additions & 7 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,21 @@ class AppOptions {
userOptions[name] = value;
}

static setAll(options) {
static setAll(options, init = false) {
if ((typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && init) {
if (this.get("disablePreferences")) {
// Give custom implementations of the default viewer a simpler way to
// opt-out of having the `Preferences` override existing `AppOptions`.
return;
}
if (Object.keys(userOptions).length) {
console.warn(
"setAll: The Preferences may override manually set AppOptions; " +
'please use the "disablePreferences"-option in order to prevent that.'
);
}
}

for (const name in options) {
userOptions[name] = options[name];
}
Expand All @@ -413,10 +427,4 @@ class AppOptions {
}
}

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
AppOptions._hasUserOptions = function () {
return Object.keys(userOptions).length > 0;
};
}

export { AppOptions, compatibilityParams, OptionKind };
25 changes: 7 additions & 18 deletions web/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class BasePreferences {

this.#initializedPromise = this._readFromStorage(this.#defaults).then(
prefs => {
for (const name in this.#defaults) {
const prefValue = prefs?.[name];
for (const [name, defaultVal] of Object.entries(this.#defaults)) {
const prefVal = prefs?.[name];
// Ignore preferences whose types don't match the default values.
if (typeof prefValue === typeof this.#defaults[name]) {
this.#prefs[name] = prefValue;
}
this.#prefs[name] =
typeof prefVal === typeof defaultVal ? prefVal : defaultVal;
}
AppOptions.setAll(this.#prefs, /* init = */ true);
}
);
}
Expand Down Expand Up @@ -156,19 +156,8 @@ class BasePreferences {
return this.#prefs[name] ?? defaultValue;
}

/**
* Get the values of all preferences.
* @returns {Promise} A promise that is resolved with an {Object} containing
* the values of all preferences.
*/
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;
get initializedPromise() {
return this.#initializedPromise;
}
}

Expand Down

0 comments on commit d6cfbd2

Please sign in to comment.