Skip to content

Commit

Permalink
Merge pull request mozilla#18501 from Snuffleupagus/AppOptions-init
Browse files Browse the repository at this point in the history
Initialize all user-options upfront in AppOptions
  • Loading branch information
timvandermeij authored Jul 28, 2024
2 parents 0ed4521 + 935d904 commit d3384c0
Showing 1 changed file with 38 additions and 39 deletions.
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 };

0 comments on commit d3384c0

Please sign in to comment.