Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize all user-options upfront in AppOptions #18501

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 38 additions & 39 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
};
}

const userOptions = new Map();

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// Apply any compatibility-values to the user-options.
for (const [name, value] of compatParams) {
userOptions.set(name, value);
}
}

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
// Ensure that the `defaultOptions` are correctly specified.
for (const name in defaultOptions) {
Expand Down Expand Up @@ -544,14 +535,44 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
class AppOptions {
static eventBus;

static #opts = new Map();

static {
// Initialize all the user-options.
for (const name in defaultOptions) {
this.#opts.set(name, defaultOptions[name].value);
}

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// Apply any compatibility-values to the user-options.
for (const [name, value] of compatParams) {
this.#opts.set(name, value);
}
this._hasInvokedSet = false;

this._checkDisablePreferences = () => {
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 true;
}
if (this._hasInvokedSet) {
console.warn(
"The Preferences may override manually set AppOptions; " +
'please use the "disablePreferences"-option to prevent that.'
);
}
return false;
};
}
}

constructor() {
throw new Error("Cannot initialize AppOptions.");
}

static get(name) {
return userOptions.has(name)
? userOptions.get(name)
: defaultOptions[name]?.value;
return this.#opts.get(name);
}

static getAll(kind = null, defaultOnly = false) {
Expand All @@ -562,10 +583,7 @@ class AppOptions {
if (kind && !(kind & defaultOpt.kind)) {
continue;
}
options[name] =
!defaultOnly && userOptions.has(name)
? userOptions.get(name)
: defaultOpt.value;
options[name] = !defaultOnly ? this.#opts.get(name) : defaultOpt.value;
}
return options;
}
Expand All @@ -575,6 +593,9 @@ class AppOptions {
}

static setAll(options, prefs = false) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this._hasInvokedSet ||= true;
}
let events;

for (const name in options) {
Expand All @@ -601,7 +622,7 @@ class AppOptions {
if (this.eventBus && kind & OptionKind.EVENT_DISPATCH) {
(events ||= new Map()).set(name, userOpt);
}
userOptions.set(name, userOpt);
this.#opts.set(name, userOpt);
}

if (events) {
Expand All @@ -612,26 +633,4 @@ class AppOptions {
}
}

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
AppOptions._checkDisablePreferences = () => {
if (AppOptions.get("disablePreferences")) {
// Give custom implementations of the default viewer a simpler way to
// opt-out of having the `Preferences` override existing `AppOptions`.
return true;
}
for (const [name] of userOptions) {
// Ignore any compatibility-values in the user-options.
if (compatParams.has(name)) {
continue;
}
console.warn(
"The Preferences may override manually set AppOptions; " +
'please use the "disablePreferences"-option to prevent that.'
);
break;
}
return false;
};
}

export { AppOptions, OptionKind };