Reduce duplication in the Dict.prototype.{get, getAsync, getArray} methods

These methods are all very similar, so let's introduce a private helper method to reduce unnecessary code duplication.
This commit is contained in:
Jonas Jenwald 2026-03-16 09:25:12 +01:00
parent bdc16f8999
commit f4aadea001

View File

@ -84,15 +84,14 @@ class Dict {
return this._map.size; return this._map.size;
} }
// Automatically dereferences Ref objects. #getValue(isAsync, key1, key2, key3) {
get(key1, key2, key3) {
let value = this._map.get(key1); let value = this._map.get(key1);
if (value === undefined && key2 !== undefined) { if (value === undefined && key2 !== undefined) {
if ( if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
key2.length < key1.length key2.length < key1.length
) { ) {
unreachable("Dict.get: Expected keys to be ordered by length."); unreachable("Dict.#getValue: Expected keys to be ordered by length.");
} }
value = this._map.get(key2); value = this._map.get(key2);
if (value === undefined && key3 !== undefined) { if (value === undefined && key3 !== undefined) {
@ -100,68 +99,32 @@ class Dict {
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
key3.length < key2.length key3.length < key2.length
) { ) {
unreachable("Dict.get: Expected keys to be ordered by length."); unreachable("Dict.#getValue: Expected keys to be ordered by length.");
} }
value = this._map.get(key3); value = this._map.get(key3);
} }
} }
if (value instanceof Ref && this.xref) { if (value instanceof Ref && this.xref) {
return this.xref.fetch(value, this.suppressEncryption); return isAsync
? this.xref.fetchAsync(value, this.suppressEncryption)
: this.xref.fetch(value, this.suppressEncryption);
} }
return value; return value;
} }
// Automatically dereferences Ref objects.
get(key1, key2, key3) {
return this.#getValue(/* isAsync = */ false, key1, key2, key3);
}
// Same as get(), but returns a promise and uses fetchIfRefAsync(). // Same as get(), but returns a promise and uses fetchIfRefAsync().
async getAsync(key1, key2, key3) { async getAsync(key1, key2, key3) {
let value = this._map.get(key1); return this.#getValue(/* isAsync = */ true, key1, key2, key3);
if (value === undefined && key2 !== undefined) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
key2.length < key1.length
) {
unreachable("Dict.getAsync: Expected keys to be ordered by length.");
}
value = this._map.get(key2);
if (value === undefined && key3 !== undefined) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
key3.length < key2.length
) {
unreachable("Dict.getAsync: Expected keys to be ordered by length.");
}
value = this._map.get(key3);
}
}
if (value instanceof Ref && this.xref) {
return this.xref.fetchAsync(value, this.suppressEncryption);
}
return value;
} }
// Same as get(), but dereferences all elements if the result is an Array. // Same as get(), but dereferences all elements if the result is an Array.
getArray(key1, key2, key3) { getArray(key1, key2, key3) {
let value = this._map.get(key1); let value = this.#getValue(/* isAsync = */ false, key1, key2, key3);
if (value === undefined && key2 !== undefined) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
key2.length < key1.length
) {
unreachable("Dict.getArray: Expected keys to be ordered by length.");
}
value = this._map.get(key2);
if (value === undefined && key3 !== undefined) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
key3.length < key2.length
) {
unreachable("Dict.getArray: Expected keys to be ordered by length.");
}
value = this._map.get(key3);
}
}
if (value instanceof Ref && this.xref) {
value = this.xref.fetch(value, this.suppressEncryption);
}
if (Array.isArray(value)) { if (Array.isArray(value)) {
value = value.slice(); // Ensure that we don't modify the Dict data. value = value.slice(); // Ensure that we don't modify the Dict data.