Convert the PDFPageProxy.intentStates
property from an Object
to a Map
As can be seen in the code there's a handful of places where this structure needs to be iterated, something that becomes cumbersome when dealing with `Object`s. Hence, by changing this to a `Map` instead we can both simplify the code and avoid creating unnecessary closures. Particularily the `PDFPageProxy._tryCleanup` method becomes a lot more readable, at least in my opinion. Finally, since this property is intended to be "private" the name is adjusted to reflect that.
This commit is contained in:
parent
cabc2cc4fc
commit
4cb0c032f3
@ -898,7 +898,7 @@ class PDFPageProxy {
|
|||||||
|
|
||||||
this.cleanupAfterRender = false;
|
this.cleanupAfterRender = false;
|
||||||
this.pendingCleanup = false;
|
this.pendingCleanup = false;
|
||||||
this.intentStates = Object.create(null);
|
this._intentStates = new Map();
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1003,10 +1003,11 @@ class PDFPageProxy {
|
|||||||
// this call to render.
|
// this call to render.
|
||||||
this.pendingCleanup = false;
|
this.pendingCleanup = false;
|
||||||
|
|
||||||
if (!this.intentStates[renderingIntent]) {
|
let intentState = this._intentStates.get(renderingIntent);
|
||||||
this.intentStates[renderingIntent] = Object.create(null);
|
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.
|
// Ensure that a pending `streamReader` cancel timeout is always aborted.
|
||||||
if (intentState.streamReaderCancelTimeout) {
|
if (intentState.streamReaderCancelTimeout) {
|
||||||
@ -1128,14 +1129,15 @@ class PDFPageProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const renderingIntent = "oplist";
|
const renderingIntent = "oplist";
|
||||||
if (!this.intentStates[renderingIntent]) {
|
let intentState = this._intentStates.get(renderingIntent);
|
||||||
this.intentStates[renderingIntent] = Object.create(null);
|
if (!intentState) {
|
||||||
|
intentState = Object.create(null);
|
||||||
|
this._intentStates.set(renderingIntent, intentState);
|
||||||
}
|
}
|
||||||
const intentState = this.intentStates[renderingIntent];
|
|
||||||
let opListTask;
|
let opListTask;
|
||||||
|
|
||||||
if (!intentState.opListReadCapability) {
|
if (!intentState.opListReadCapability) {
|
||||||
opListTask = {};
|
opListTask = Object.create(null);
|
||||||
opListTask.operatorListChanged = operatorListChanged;
|
opListTask.operatorListChanged = operatorListChanged;
|
||||||
intentState.opListReadCapability = createPromiseCapability();
|
intentState.opListReadCapability = createPromiseCapability();
|
||||||
intentState.renderTasks = [];
|
intentState.renderTasks = [];
|
||||||
@ -1222,8 +1224,7 @@ class PDFPageProxy {
|
|||||||
this._transport.pageCache[this._pageIndex] = null;
|
this._transport.pageCache[this._pageIndex] = null;
|
||||||
|
|
||||||
const waitOn = [];
|
const waitOn = [];
|
||||||
Object.keys(this.intentStates).forEach(intent => {
|
for (const [intent, intentState] of this._intentStates) {
|
||||||
const intentState = this.intentStates[intent];
|
|
||||||
this._abortOperatorList({
|
this._abortOperatorList({
|
||||||
intentState,
|
intentState,
|
||||||
reason: new Error("Page was destroyed."),
|
reason: new Error("Page was destroyed."),
|
||||||
@ -1232,13 +1233,13 @@ class PDFPageProxy {
|
|||||||
|
|
||||||
if (intent === "oplist") {
|
if (intent === "oplist") {
|
||||||
// Avoid errors below, since the renderTasks are just stubs.
|
// Avoid errors below, since the renderTasks are just stubs.
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
for (const internalRenderTask of intentState.renderTasks) {
|
for (const internalRenderTask of intentState.renderTasks) {
|
||||||
waitOn.push(internalRenderTask.completed);
|
waitOn.push(internalRenderTask.completed);
|
||||||
internalRenderTask.cancel();
|
internalRenderTask.cancel();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
this.objs.clear();
|
this.objs.clear();
|
||||||
this.annotationsPromise = null;
|
this.annotationsPromise = null;
|
||||||
this.pendingCleanup = false;
|
this.pendingCleanup = false;
|
||||||
@ -1261,22 +1262,16 @@ class PDFPageProxy {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_tryCleanup(resetStats = false) {
|
_tryCleanup(resetStats = false) {
|
||||||
if (
|
if (!this.pendingCleanup) {
|
||||||
!this.pendingCleanup ||
|
|
||||||
Object.keys(this.intentStates).some(intent => {
|
|
||||||
const intentState = this.intentStates[intent];
|
|
||||||
return (
|
|
||||||
intentState.renderTasks.length !== 0 ||
|
|
||||||
!intentState.operatorList.lastChunk
|
|
||||||
);
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
for (const { renderTasks, operatorList } of this._intentStates.values()) {
|
||||||
|
if (renderTasks.length !== 0 || !operatorList.lastChunk) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Object.keys(this.intentStates).forEach(intent => {
|
this._intentStates.clear();
|
||||||
delete this.intentStates[intent];
|
|
||||||
});
|
|
||||||
this.objs.clear();
|
this.objs.clear();
|
||||||
this.annotationsPromise = null;
|
this.annotationsPromise = null;
|
||||||
if (resetStats && this._stats) {
|
if (resetStats && this._stats) {
|
||||||
@ -1290,7 +1285,7 @@ class PDFPageProxy {
|
|||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_startRenderPage(transparency, intent) {
|
_startRenderPage(transparency, intent) {
|
||||||
const intentState = this.intentStates[intent];
|
const intentState = this._intentStates.get(intent);
|
||||||
if (!intentState) {
|
if (!intentState) {
|
||||||
return; // Rendering was cancelled.
|
return; // Rendering was cancelled.
|
||||||
}
|
}
|
||||||
@ -1340,7 +1335,7 @@ class PDFPageProxy {
|
|||||||
);
|
);
|
||||||
const reader = readableStream.getReader();
|
const reader = readableStream.getReader();
|
||||||
|
|
||||||
const intentState = this.intentStates[args.intent];
|
const intentState = this._intentStates.get(args.intent);
|
||||||
intentState.streamReader = reader;
|
intentState.streamReader = reader;
|
||||||
|
|
||||||
const pump = () => {
|
const pump = () => {
|
||||||
@ -1425,13 +1420,12 @@ class PDFPageProxy {
|
|||||||
}
|
}
|
||||||
// Remove the current `intentState`, since a cancelled `getOperatorList`
|
// Remove the current `intentState`, since a cancelled `getOperatorList`
|
||||||
// call on the worker-thread cannot be re-started...
|
// call on the worker-thread cannot be re-started...
|
||||||
Object.keys(this.intentStates).some(intent => {
|
for (const [intent, curIntentState] of this._intentStates) {
|
||||||
if (this.intentStates[intent] === intentState) {
|
if (curIntentState === intentState) {
|
||||||
delete this.intentStates[intent];
|
this._intentStates.delete(intent);
|
||||||
return true;
|
break;
|
||||||
}
|
}
|
||||||
return false;
|
}
|
||||||
});
|
|
||||||
// ... and force clean-up to ensure that any old state is always removed.
|
// ... and force clean-up to ensure that any old state is always removed.
|
||||||
this.cleanup();
|
this.cleanup();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user