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

Introduce some Map-usage in the AppOptions #18469

Merged
merged 3 commits into from
Jul 21, 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
52 changes: 30 additions & 22 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// eslint-disable-next-line no-var
var compatibilityParams = Object.create(null);
var compatParams = new Map();
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("LIB") &&
Expand All @@ -34,9 +34,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {

// Limit canvas size to 5 mega-pixels on mobile.
// Support: Android, iOS
(function checkCanvasSizeLimitation() {
(function () {
if (isIOS || isAndroid) {
compatibilityParams.maxCanvasPixels = 5242880;
compatParams.set("maxCanvasPixels", 5242880);
}
})();
}
Expand Down Expand Up @@ -442,13 +442,13 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
};
}

const userOptions = Object.create(null);
const userOptions = new Map();

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

Expand All @@ -464,10 +464,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
if (kind & OptionKind.BROWSER) {
throw new Error(`Cannot mix "PREFERENCE" and "BROWSER" kind: ${name}`);
}
if (
typeof compatibilityParams === "object" &&
compatibilityParams[name] !== undefined
) {
if (typeof compatParams === "object" && compatParams.has(name)) {
throw new Error(
`Should not have compatibility-value for "PREFERENCE" kind: ${name}`
);
Expand All @@ -480,6 +477,15 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
) {
throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`);
}
} else if (kind & OptionKind.BROWSER) {
if (typeof compatParams === "object" && compatParams.has(name)) {
throw new Error(
`Should not have compatibility-value for "BROWSER" kind: ${name}`
);
}
if (value === undefined) {
throw new Error(`Invalid value for "BROWSER" kind: ${name}`);
}
}
}
}
Expand All @@ -492,7 +498,9 @@ class AppOptions {
}

static get(name) {
return userOptions[name] ?? defaultOptions[name]?.value ?? undefined;
return userOptions.has(name)
timvandermeij marked this conversation as resolved.
Show resolved Hide resolved
? userOptions.get(name)
: defaultOptions[name]?.value;
}

static getAll(kind = null, defaultOnly = false) {
Expand All @@ -503,9 +511,10 @@ class AppOptions {
if (kind && !(kind & defaultOption.kind)) {
continue;
}
options[name] = defaultOnly
? defaultOption.value
: (userOptions[name] ?? defaultOption.value);
options[name] =
!defaultOnly && userOptions.has(name)
? userOptions.get(name)
: defaultOption.value;
}
return options;
}
Expand All @@ -516,7 +525,7 @@ class AppOptions {
if (!defaultOption || typeof value !== typeof defaultOption.value) {
return;
}
userOptions[name] = value;
userOptions.set(name, value);
}

static setAll(options, prefs = false) {
Expand All @@ -539,7 +548,7 @@ class AppOptions {
(events ||= new Map()).set(name, userOption);
}
}
userOptions[name] = userOption;
userOptions.set(name, userOption);
}

if (events) {
Expand All @@ -550,13 +559,12 @@ class AppOptions {
}

static remove(name) {
delete userOptions[name];
userOptions.delete(name);

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// Re-apply a compatibility-value, if it exists, to the user-options.
const val = compatibilityParams[name];
if (val !== undefined) {
userOptions[name] = val;
if (compatParams.has(name)) {
userOptions.set(name, compatParams.get(name));
}
}
}
Expand All @@ -569,9 +577,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// opt-out of having the `Preferences` override existing `AppOptions`.
return true;
}
for (const name in userOptions) {
for (const [name] of userOptions) {
// Ignore any compatibility-values in the user-options.
if (compatibilityParams[name] !== undefined) {
if (compatParams.has(name)) {
continue;
}
console.warn(
Expand Down