Merge pull request #17130 from Snuffleupagus/AppOption-move-init

Try to make the `Preferences`/`AppOptions` initialization slightly more efficient
This commit is contained in:
Jonas Jenwald 2023-10-19 15:12:30 +02:00 committed by GitHub
commit d6cfbd2839
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 58 deletions

View File

@ -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 (
@ -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();
@ -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
@ -697,6 +674,7 @@ const PDFViewerApplication = {
},
async run(config) {
this.preferences = this.externalServices.createPreferences();
await this.initialize(config);
const { appConfig, eventBus } = this;

View File

@ -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];
}
@ -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 };

View File

@ -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);
}
);
}
@ -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;
}
}