Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert the PDFPageProxy.intentStates property from an Object to a Map #12017

Merged
merged 3 commits into from
Jun 21, 2020
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
2 changes: 1 addition & 1 deletion src/core/chunked_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ class ChunkedStreamManager {
if (this.pdfNetworkStream) {
this.pdfNetworkStream.cancelAllRequests(reason);
}
for (const [, capability] of this._promisesByRequest) {
for (const capability of this._promisesByRequest.values()) {
capability.reject(reason);
}
}
Expand Down
78 changes: 38 additions & 40 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ class PDFPageProxy {

this.cleanupAfterRender = false;
this.pendingCleanup = false;
this.intentStates = Object.create(null);
this._intentStates = new Map();
this.destroyed = false;
}

Expand Down Expand Up @@ -1003,10 +1003,11 @@ class PDFPageProxy {
// this call to render.
this.pendingCleanup = false;

if (!this.intentStates[renderingIntent]) {
this.intentStates[renderingIntent] = Object.create(null);
let intentState = this._intentStates.get(renderingIntent);
if (!intentState) {
intentState = Object.create(null);
this._intentStates.set(renderingIntent, intentState);
}
const intentState = this.intentStates[renderingIntent];

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

const renderingIntent = "oplist";
if (!this.intentStates[renderingIntent]) {
this.intentStates[renderingIntent] = Object.create(null);
let intentState = this._intentStates.get(renderingIntent);
if (!intentState) {
intentState = Object.create(null);
this._intentStates.set(renderingIntent, intentState);
}
const intentState = this.intentStates[renderingIntent];
let opListTask;

if (!intentState.opListReadCapability) {
opListTask = {};
opListTask = Object.create(null);
opListTask.operatorListChanged = operatorListChanged;
intentState.opListReadCapability = createPromiseCapability();
intentState.renderTasks = [];
Expand Down Expand Up @@ -1222,8 +1224,7 @@ class PDFPageProxy {
this._transport.pageCache[this._pageIndex] = null;

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

if (intent === "oplist") {
// Avoid errors below, since the renderTasks are just stubs.
return;
continue;
}
intentState.renderTasks.forEach(function (renderTask) {
const renderCompleted = renderTask.capability.promise.catch(
function () {}
); // ignoring failures
waitOn.push(renderCompleted);
renderTask.cancel();
});
});
for (const internalRenderTask of intentState.renderTasks) {
waitOn.push(internalRenderTask.completed);
internalRenderTask.cancel();
}
}
this.objs.clear();
this.annotationsPromise = null;
this.pendingCleanup = false;
Expand All @@ -1264,22 +1262,16 @@ class PDFPageProxy {
* @private
*/
_tryCleanup(resetStats = false) {
if (
!this.pendingCleanup ||
Object.keys(this.intentStates).some(intent => {
const intentState = this.intentStates[intent];
return (
intentState.renderTasks.length !== 0 ||
!intentState.operatorList.lastChunk
);
})
) {
if (!this.pendingCleanup) {
return false;
}
for (const { renderTasks, operatorList } of this._intentStates.values()) {
if (renderTasks.length !== 0 || !operatorList.lastChunk) {
return false;
}
}

Object.keys(this.intentStates).forEach(intent => {
delete this.intentStates[intent];
});
this._intentStates.clear();
this.objs.clear();
this.annotationsPromise = null;
if (resetStats && this._stats) {
Expand All @@ -1293,7 +1285,7 @@ class PDFPageProxy {
* @private
*/
_startRenderPage(transparency, intent) {
const intentState = this.intentStates[intent];
const intentState = this._intentStates.get(intent);
if (!intentState) {
return; // Rendering was cancelled.
}
Expand Down Expand Up @@ -1343,7 +1335,7 @@ class PDFPageProxy {
);
const reader = readableStream.getReader();

const intentState = this.intentStates[args.intent];
const intentState = this._intentStates.get(args.intent);
intentState.streamReader = reader;

const pump = () => {
Expand Down Expand Up @@ -1428,13 +1420,12 @@ class PDFPageProxy {
}
// Remove the current `intentState`, since a cancelled `getOperatorList`
// call on the worker-thread cannot be re-started...
Object.keys(this.intentStates).some(intent => {
if (this.intentStates[intent] === intentState) {
delete this.intentStates[intent];
return true;
for (const [intent, curIntentState] of this._intentStates) {
if (curIntentState === intentState) {
this._intentStates.delete(intent);
break;
}
return false;
});
}
// ... and force clean-up to ensure that any old state is always removed.
this.cleanup();
}
Expand Down Expand Up @@ -2650,6 +2641,13 @@ const InternalRenderTask = (function InternalRenderTaskClosure() {
this._canvas = params.canvasContext.canvas;
}

get completed() {
return this.capability.promise.catch(function () {
// Ignoring errors, since we only want to know when rendering is
// no longer pending.
});
}

initializeGraphics(transparency = false) {
if (this.cancelled) {
return;
Expand Down