Skip to content

Commit 3c81b5e

Browse files
Merge pull request #12017 from Snuffleupagus/api-intentStates-Map
Convert the `PDFPageProxy.intentStates` property from an `Object` to a `Map`
2 parents 8cfdfb2 + 4cb0c03 commit 3c81b5e

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

src/core/chunked_stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ class ChunkedStreamManager {
591591
if (this.pdfNetworkStream) {
592592
this.pdfNetworkStream.cancelAllRequests(reason);
593593
}
594-
for (const [, capability] of this._promisesByRequest) {
594+
for (const capability of this._promisesByRequest.values()) {
595595
capability.reject(reason);
596596
}
597597
}

src/display/api.js

+38-40
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ class PDFPageProxy {
898898

899899
this.cleanupAfterRender = false;
900900
this.pendingCleanup = false;
901-
this.intentStates = Object.create(null);
901+
this._intentStates = new Map();
902902
this.destroyed = false;
903903
}
904904

@@ -1003,10 +1003,11 @@ class PDFPageProxy {
10031003
// this call to render.
10041004
this.pendingCleanup = false;
10051005

1006-
if (!this.intentStates[renderingIntent]) {
1007-
this.intentStates[renderingIntent] = Object.create(null);
1006+
let intentState = this._intentStates.get(renderingIntent);
1007+
if (!intentState) {
1008+
intentState = Object.create(null);
1009+
this._intentStates.set(renderingIntent, intentState);
10081010
}
1009-
const intentState = this.intentStates[renderingIntent];
10101011

10111012
// Ensure that a pending `streamReader` cancel timeout is always aborted.
10121013
if (intentState.streamReaderCancelTimeout) {
@@ -1128,14 +1129,15 @@ class PDFPageProxy {
11281129
}
11291130

11301131
const renderingIntent = "oplist";
1131-
if (!this.intentStates[renderingIntent]) {
1132-
this.intentStates[renderingIntent] = Object.create(null);
1132+
let intentState = this._intentStates.get(renderingIntent);
1133+
if (!intentState) {
1134+
intentState = Object.create(null);
1135+
this._intentStates.set(renderingIntent, intentState);
11331136
}
1134-
const intentState = this.intentStates[renderingIntent];
11351137
let opListTask;
11361138

11371139
if (!intentState.opListReadCapability) {
1138-
opListTask = {};
1140+
opListTask = Object.create(null);
11391141
opListTask.operatorListChanged = operatorListChanged;
11401142
intentState.opListReadCapability = createPromiseCapability();
11411143
intentState.renderTasks = [];
@@ -1222,8 +1224,7 @@ class PDFPageProxy {
12221224
this._transport.pageCache[this._pageIndex] = null;
12231225

12241226
const waitOn = [];
1225-
Object.keys(this.intentStates).forEach(intent => {
1226-
const intentState = this.intentStates[intent];
1227+
for (const [intent, intentState] of this._intentStates) {
12271228
this._abortOperatorList({
12281229
intentState,
12291230
reason: new Error("Page was destroyed."),
@@ -1232,16 +1233,13 @@ class PDFPageProxy {
12321233

12331234
if (intent === "oplist") {
12341235
// Avoid errors below, since the renderTasks are just stubs.
1235-
return;
1236+
continue;
12361237
}
1237-
intentState.renderTasks.forEach(function (renderTask) {
1238-
const renderCompleted = renderTask.capability.promise.catch(
1239-
function () {}
1240-
); // ignoring failures
1241-
waitOn.push(renderCompleted);
1242-
renderTask.cancel();
1243-
});
1244-
});
1238+
for (const internalRenderTask of intentState.renderTasks) {
1239+
waitOn.push(internalRenderTask.completed);
1240+
internalRenderTask.cancel();
1241+
}
1242+
}
12451243
this.objs.clear();
12461244
this.annotationsPromise = null;
12471245
this.pendingCleanup = false;
@@ -1264,22 +1262,16 @@ class PDFPageProxy {
12641262
* @private
12651263
*/
12661264
_tryCleanup(resetStats = false) {
1267-
if (
1268-
!this.pendingCleanup ||
1269-
Object.keys(this.intentStates).some(intent => {
1270-
const intentState = this.intentStates[intent];
1271-
return (
1272-
intentState.renderTasks.length !== 0 ||
1273-
!intentState.operatorList.lastChunk
1274-
);
1275-
})
1276-
) {
1265+
if (!this.pendingCleanup) {
12771266
return false;
12781267
}
1268+
for (const { renderTasks, operatorList } of this._intentStates.values()) {
1269+
if (renderTasks.length !== 0 || !operatorList.lastChunk) {
1270+
return false;
1271+
}
1272+
}
12791273

1280-
Object.keys(this.intentStates).forEach(intent => {
1281-
delete this.intentStates[intent];
1282-
});
1274+
this._intentStates.clear();
12831275
this.objs.clear();
12841276
this.annotationsPromise = null;
12851277
if (resetStats && this._stats) {
@@ -1293,7 +1285,7 @@ class PDFPageProxy {
12931285
* @private
12941286
*/
12951287
_startRenderPage(transparency, intent) {
1296-
const intentState = this.intentStates[intent];
1288+
const intentState = this._intentStates.get(intent);
12971289
if (!intentState) {
12981290
return; // Rendering was cancelled.
12991291
}
@@ -1343,7 +1335,7 @@ class PDFPageProxy {
13431335
);
13441336
const reader = readableStream.getReader();
13451337

1346-
const intentState = this.intentStates[args.intent];
1338+
const intentState = this._intentStates.get(args.intent);
13471339
intentState.streamReader = reader;
13481340

13491341
const pump = () => {
@@ -1428,13 +1420,12 @@ class PDFPageProxy {
14281420
}
14291421
// Remove the current `intentState`, since a cancelled `getOperatorList`
14301422
// call on the worker-thread cannot be re-started...
1431-
Object.keys(this.intentStates).some(intent => {
1432-
if (this.intentStates[intent] === intentState) {
1433-
delete this.intentStates[intent];
1434-
return true;
1423+
for (const [intent, curIntentState] of this._intentStates) {
1424+
if (curIntentState === intentState) {
1425+
this._intentStates.delete(intent);
1426+
break;
14351427
}
1436-
return false;
1437-
});
1428+
}
14381429
// ... and force clean-up to ensure that any old state is always removed.
14391430
this.cleanup();
14401431
}
@@ -2650,6 +2641,13 @@ const InternalRenderTask = (function InternalRenderTaskClosure() {
26502641
this._canvas = params.canvasContext.canvas;
26512642
}
26522643

2644+
get completed() {
2645+
return this.capability.promise.catch(function () {
2646+
// Ignoring errors, since we only want to know when rendering is
2647+
// no longer pending.
2648+
});
2649+
}
2650+
26532651
initializeGraphics(transparency = false) {
26542652
if (this.cancelled) {
26552653
return;

0 commit comments

Comments
 (0)