Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 12 additions & 16 deletions src/display/pdf_objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const INITIAL_DATA = Symbol("INITIAL_DATA");
* a worker. This class implements some basic methods to manage these objects.
*/
class PDFObjects {
#objs = Object.create(null);
#objs = new Map();

/**
* Ensures there is an object defined for `objId`.
Expand All @@ -30,10 +30,10 @@ class PDFObjects {
* @returns {Object}
*/
#ensureObj(objId) {
return (this.#objs[objId] ||= {
return this.#objs.getOrInsertComputed(objId, () => ({
...Promise.withResolvers(),
data: INITIAL_DATA,
});
}));
}

/**
Expand All @@ -58,7 +58,7 @@ class PDFObjects {
}
// If there isn't a callback, the user expects to get the resolved data
// directly.
const obj = this.#objs[objId];
const obj = this.#objs.get(objId);
// If there isn't an object yet or the object isn't resolved, then the
// data isn't ready yet!
if (!obj || obj.data === INITIAL_DATA) {
Expand All @@ -72,7 +72,7 @@ class PDFObjects {
* @returns {boolean}
*/
has(objId) {
const obj = this.#objs[objId];
const obj = this.#objs.get(objId);
return !!obj && obj.data !== INITIAL_DATA;
}

Expand All @@ -81,12 +81,12 @@ class PDFObjects {
* @returns {boolean}
*/
delete(objId) {
const obj = this.#objs[objId];
const obj = this.#objs.get(objId);
if (!obj || obj.data === INITIAL_DATA) {
// Only allow removing the object *after* it's been resolved.
return false;
}
delete this.#objs[objId];
this.#objs.delete(objId);
return true;
}

Expand All @@ -103,21 +103,17 @@ class PDFObjects {
}

clear() {
for (const objId in this.#objs) {
const { data } = this.#objs[objId];
for (const { data } of this.#objs.values()) {
data?.bitmap?.close(); // Release any `ImageBitmap` data.
}
this.#objs = Object.create(null);
this.#objs.clear();
}

*[Symbol.iterator]() {
for (const objId in this.#objs) {
const { data } = this.#objs[objId];

if (data === INITIAL_DATA) {
continue;
for (const [objId, { data }] of this.#objs) {
if (data !== INITIAL_DATA) {
yield [objId, data];
}
yield [objId, data];
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions web/base_download_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ class BaseDownloadManager {
const contentType = isPdfData ? "application/pdf" : "";

if (isPdfData) {
let blobUrl;
const blobUrl = this.#openBlobUrls.getOrInsertComputed(data, () =>
URL.createObjectURL(new Blob([data], { type: contentType }))
);
try {
blobUrl = this.#openBlobUrls.getOrInsertComputed(data, () =>
URL.createObjectURL(new Blob([data], { type: contentType }))
);
const viewerUrl = this._getOpenDataUrl(blobUrl, filename, dest);

window.open(viewerUrl);
Expand Down
Loading