Skip to content

Commit bda7456

Browse files
authored
Merge pull request #20892 from Snuffleupagus/Dict-get-helper
Reduce duplication in the `Dict.prototype.{get, getAsync, getArray}` methods
2 parents a984495 + f4aadea commit bda7456

1 file changed

Lines changed: 13 additions & 50 deletions

File tree

src/core/primitives.js

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -84,84 +84,47 @@ class Dict {
8484
return this._map.size;
8585
}
8686

87-
// Automatically dereferences Ref objects.
88-
get(key1, key2, key3) {
87+
#getValue(isAsync, key1, key2, key3) {
8988
let value = this._map.get(key1);
9089
if (value === undefined && key2 !== undefined) {
9190
if (
9291
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
9392
key2.length < key1.length
9493
) {
95-
unreachable("Dict.get: Expected keys to be ordered by length.");
94+
unreachable("Dict.#getValue: Expected keys to be ordered by length.");
9695
}
9796
value = this._map.get(key2);
9897
if (value === undefined && key3 !== undefined) {
9998
if (
10099
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
101100
key3.length < key2.length
102101
) {
103-
unreachable("Dict.get: Expected keys to be ordered by length.");
102+
unreachable("Dict.#getValue: Expected keys to be ordered by length.");
104103
}
105104
value = this._map.get(key3);
106105
}
107106
}
108107
if (value instanceof Ref && this.xref) {
109-
return this.xref.fetch(value, this.suppressEncryption);
108+
return isAsync
109+
? this.xref.fetchAsync(value, this.suppressEncryption)
110+
: this.xref.fetch(value, this.suppressEncryption);
110111
}
111112
return value;
112113
}
113114

115+
// Automatically dereferences Ref objects.
116+
get(key1, key2, key3) {
117+
return this.#getValue(/* isAsync = */ false, key1, key2, key3);
118+
}
119+
114120
// Same as get(), but returns a promise and uses fetchIfRefAsync().
115121
async getAsync(key1, key2, key3) {
116-
let value = this._map.get(key1);
117-
if (value === undefined && key2 !== undefined) {
118-
if (
119-
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
120-
key2.length < key1.length
121-
) {
122-
unreachable("Dict.getAsync: Expected keys to be ordered by length.");
123-
}
124-
value = this._map.get(key2);
125-
if (value === undefined && key3 !== undefined) {
126-
if (
127-
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
128-
key3.length < key2.length
129-
) {
130-
unreachable("Dict.getAsync: Expected keys to be ordered by length.");
131-
}
132-
value = this._map.get(key3);
133-
}
134-
}
135-
if (value instanceof Ref && this.xref) {
136-
return this.xref.fetchAsync(value, this.suppressEncryption);
137-
}
138-
return value;
122+
return this.#getValue(/* isAsync = */ true, key1, key2, key3);
139123
}
140124

141125
// Same as get(), but dereferences all elements if the result is an Array.
142126
getArray(key1, key2, key3) {
143-
let value = this._map.get(key1);
144-
if (value === undefined && key2 !== undefined) {
145-
if (
146-
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
147-
key2.length < key1.length
148-
) {
149-
unreachable("Dict.getArray: Expected keys to be ordered by length.");
150-
}
151-
value = this._map.get(key2);
152-
if (value === undefined && key3 !== undefined) {
153-
if (
154-
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
155-
key3.length < key2.length
156-
) {
157-
unreachable("Dict.getArray: Expected keys to be ordered by length.");
158-
}
159-
value = this._map.get(key3);
160-
}
161-
}
162-
if (value instanceof Ref && this.xref) {
163-
value = this.xref.fetch(value, this.suppressEncryption);
164-
}
127+
let value = this.#getValue(/* isAsync = */ false, key1, key2, key3);
165128

166129
if (Array.isArray(value)) {
167130
value = value.slice(); // Ensure that we don't modify the Dict data.

0 commit comments

Comments
 (0)