Skip to content

Commit 909a700

Browse files
Merge pull request #20717 from Snuffleupagus/api-getOrInsertComputed
Use `Map.prototype.getOrInsertComputed()` in the `src/display/api.js` file
2 parents c91de76 + e2c8fc6 commit 909a700

1 file changed

Lines changed: 16 additions & 30 deletions

File tree

src/display/api.js

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,12 +1502,9 @@ class PDFPageProxy {
15021502
optionalContentConfigPromise ||=
15031503
this._transport.getOptionalContentConfig(renderingIntent);
15041504

1505-
let intentState = this._intentStates.get(cacheKey);
1506-
if (!intentState) {
1507-
intentState = Object.create(null);
1508-
this._intentStates.set(cacheKey, intentState);
1509-
}
1510-
1505+
const intentState = this._intentStates.getOrInsertComputed(cacheKey, () =>
1506+
Object.create(null)
1507+
);
15111508
// Ensure that a pending `streamReader` cancel timeout is always aborted.
15121509
if (intentState.streamReaderCancelTimeout) {
15131510
clearTimeout(intentState.streamReaderCancelTimeout);
@@ -1676,11 +1673,10 @@ class PDFPageProxy {
16761673
isEditing,
16771674
/* isOpList = */ true
16781675
);
1679-
let intentState = this._intentStates.get(intentArgs.cacheKey);
1680-
if (!intentState) {
1681-
intentState = Object.create(null);
1682-
this._intentStates.set(intentArgs.cacheKey, intentState);
1683-
}
1676+
const intentState = this._intentStates.getOrInsertComputed(
1677+
intentArgs.cacheKey,
1678+
() => Object.create(null)
1679+
);
16841680
let opListTask;
16851681

16861682
if (!intentState.opListReadCapability) {
@@ -2516,14 +2512,9 @@ class WorkerTransport {
25162512
}
25172513

25182514
#cacheSimpleMethod(name, data = null) {
2519-
const cachedPromise = this.#methodPromises.get(name);
2520-
if (cachedPromise) {
2521-
return cachedPromise;
2522-
}
2523-
const promise = this.messageHandler.sendWithPromise(name, data);
2524-
2525-
this.#methodPromises.set(name, promise);
2526-
return promise;
2515+
return this.#methodPromises.getOrInsertComputed(name, () =>
2516+
this.messageHandler.sendWithPromise(name, data)
2517+
);
25272518
}
25282519

25292520
#onProgress({ loaded, total }) {
@@ -3135,22 +3126,17 @@ class WorkerTransport {
31353126
}
31363127

31373128
getMetadata() {
3138-
const name = "GetMetadata",
3139-
cachedPromise = this.#methodPromises.get(name);
3140-
if (cachedPromise) {
3141-
return cachedPromise;
3142-
}
3143-
const promise = this.messageHandler
3144-
.sendWithPromise(name, null)
3145-
.then(results => ({
3129+
const name = "GetMetadata";
3130+
3131+
return this.#methodPromises.getOrInsertComputed(name, () =>
3132+
this.messageHandler.sendWithPromise(name, null).then(results => ({
31463133
info: results[0],
31473134
metadata: results[1] ? new Metadata(results[1]) : null,
31483135
contentDispositionFilename: this.#fullReader?.filename ?? null,
31493136
contentLength: this.#fullReader?.contentLength ?? null,
31503137
hasStructTree: results[2],
3151-
}));
3152-
this.#methodPromises.set(name, promise);
3153-
return promise;
3138+
}))
3139+
);
31543140
}
31553141

31563142
getMarkInfo() {

0 commit comments

Comments
 (0)