Use Map.prototype.getOrInsertComputed() in the src/display/api.js file

This commit is contained in:
Jonas Jenwald 2026-02-22 18:15:22 +01:00
parent 7b451f6a89
commit e2c8fc6140

View File

@ -1502,12 +1502,9 @@ class PDFPageProxy {
optionalContentConfigPromise ||= optionalContentConfigPromise ||=
this._transport.getOptionalContentConfig(renderingIntent); this._transport.getOptionalContentConfig(renderingIntent);
let intentState = this._intentStates.get(cacheKey); const intentState = this._intentStates.getOrInsertComputed(cacheKey, () =>
if (!intentState) { Object.create(null)
intentState = Object.create(null); );
this._intentStates.set(cacheKey, intentState);
}
// Ensure that a pending `streamReader` cancel timeout is always aborted. // Ensure that a pending `streamReader` cancel timeout is always aborted.
if (intentState.streamReaderCancelTimeout) { if (intentState.streamReaderCancelTimeout) {
clearTimeout(intentState.streamReaderCancelTimeout); clearTimeout(intentState.streamReaderCancelTimeout);
@ -1676,11 +1673,10 @@ class PDFPageProxy {
isEditing, isEditing,
/* isOpList = */ true /* isOpList = */ true
); );
let intentState = this._intentStates.get(intentArgs.cacheKey); const intentState = this._intentStates.getOrInsertComputed(
if (!intentState) { intentArgs.cacheKey,
intentState = Object.create(null); () => Object.create(null)
this._intentStates.set(intentArgs.cacheKey, intentState); );
}
let opListTask; let opListTask;
if (!intentState.opListReadCapability) { if (!intentState.opListReadCapability) {
@ -2516,14 +2512,9 @@ class WorkerTransport {
} }
#cacheSimpleMethod(name, data = null) { #cacheSimpleMethod(name, data = null) {
const cachedPromise = this.#methodPromises.get(name); return this.#methodPromises.getOrInsertComputed(name, () =>
if (cachedPromise) { this.messageHandler.sendWithPromise(name, data)
return cachedPromise; );
}
const promise = this.messageHandler.sendWithPromise(name, data);
this.#methodPromises.set(name, promise);
return promise;
} }
#onProgress({ loaded, total }) { #onProgress({ loaded, total }) {
@ -3135,22 +3126,17 @@ class WorkerTransport {
} }
getMetadata() { getMetadata() {
const name = "GetMetadata", const name = "GetMetadata";
cachedPromise = this.#methodPromises.get(name);
if (cachedPromise) { return this.#methodPromises.getOrInsertComputed(name, () =>
return cachedPromise; this.messageHandler.sendWithPromise(name, null).then(results => ({
}
const promise = this.messageHandler
.sendWithPromise(name, null)
.then(results => ({
info: results[0], info: results[0],
metadata: results[1] ? new Metadata(results[1]) : null, metadata: results[1] ? new Metadata(results[1]) : null,
contentDispositionFilename: this.#fullReader?.filename ?? null, contentDispositionFilename: this.#fullReader?.filename ?? null,
contentLength: this.#fullReader?.contentLength ?? null, contentLength: this.#fullReader?.contentLength ?? null,
hasStructTree: results[2], hasStructTree: results[2],
})); }))
this.#methodPromises.set(name, promise); );
return promise;
} }
getMarkInfo() { getMarkInfo() {