Skip to content

Commit fe44bac

Browse files
Merge pull request #20673 from Snuffleupagus/PDFObjects-Map
Convert the `PDFObjects` class to use a `Map` internally
2 parents 62ac1b8 + 6323afa commit fe44bac

2 files changed

Lines changed: 15 additions & 20 deletions

File tree

src/display/pdf_objects.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const INITIAL_DATA = Symbol("INITIAL_DATA");
2121
* a worker. This class implements some basic methods to manage these objects.
2222
*/
2323
class PDFObjects {
24-
#objs = Object.create(null);
24+
#objs = new Map();
2525

2626
/**
2727
* Ensures there is an object defined for `objId`.
@@ -30,10 +30,10 @@ class PDFObjects {
3030
* @returns {Object}
3131
*/
3232
#ensureObj(objId) {
33-
return (this.#objs[objId] ||= {
33+
return this.#objs.getOrInsertComputed(objId, () => ({
3434
...Promise.withResolvers(),
3535
data: INITIAL_DATA,
36-
});
36+
}));
3737
}
3838

3939
/**
@@ -58,7 +58,7 @@ class PDFObjects {
5858
}
5959
// If there isn't a callback, the user expects to get the resolved data
6060
// directly.
61-
const obj = this.#objs[objId];
61+
const obj = this.#objs.get(objId);
6262
// If there isn't an object yet or the object isn't resolved, then the
6363
// data isn't ready yet!
6464
if (!obj || obj.data === INITIAL_DATA) {
@@ -72,7 +72,7 @@ class PDFObjects {
7272
* @returns {boolean}
7373
*/
7474
has(objId) {
75-
const obj = this.#objs[objId];
75+
const obj = this.#objs.get(objId);
7676
return !!obj && obj.data !== INITIAL_DATA;
7777
}
7878

@@ -81,12 +81,12 @@ class PDFObjects {
8181
* @returns {boolean}
8282
*/
8383
delete(objId) {
84-
const obj = this.#objs[objId];
84+
const obj = this.#objs.get(objId);
8585
if (!obj || obj.data === INITIAL_DATA) {
8686
// Only allow removing the object *after* it's been resolved.
8787
return false;
8888
}
89-
delete this.#objs[objId];
89+
this.#objs.delete(objId);
9090
return true;
9191
}
9292

@@ -103,21 +103,17 @@ class PDFObjects {
103103
}
104104

105105
clear() {
106-
for (const objId in this.#objs) {
107-
const { data } = this.#objs[objId];
106+
for (const { data } of this.#objs.values()) {
108107
data?.bitmap?.close(); // Release any `ImageBitmap` data.
109108
}
110-
this.#objs = Object.create(null);
109+
this.#objs.clear();
111110
}
112111

113112
*[Symbol.iterator]() {
114-
for (const objId in this.#objs) {
115-
const { data } = this.#objs[objId];
116-
117-
if (data === INITIAL_DATA) {
118-
continue;
113+
for (const [objId, { data }] of this.#objs) {
114+
if (data !== INITIAL_DATA) {
115+
yield [objId, data];
119116
}
120-
yield [objId, data];
121117
}
122118
}
123119
}

web/base_download_manager.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ class BaseDownloadManager {
6464
const contentType = isPdfData ? "application/pdf" : "";
6565

6666
if (isPdfData) {
67-
let blobUrl;
67+
const blobUrl = this.#openBlobUrls.getOrInsertComputed(data, () =>
68+
URL.createObjectURL(new Blob([data], { type: contentType }))
69+
);
6870
try {
69-
blobUrl = this.#openBlobUrls.getOrInsertComputed(data, () =>
70-
URL.createObjectURL(new Blob([data], { type: contentType }))
71-
);
7271
const viewerUrl = this._getOpenDataUrl(blobUrl, filename, dest);
7372

7473
window.open(viewerUrl);

0 commit comments

Comments
 (0)