Skip to content

Commit 00e3a4d

Browse files
Merge pull request #19674 from Snuffleupagus/core-document-more-async
Introduce more `async` code in the `src/core/document.js` file
2 parents 6243afa + d004823 commit 00e3a4d

1 file changed

Lines changed: 117 additions & 127 deletions

File tree

src/core/document.js

Lines changed: 117 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,20 @@ class Page {
251251
/**
252252
* @returns {Promise<BaseStream>}
253253
*/
254-
getContentStream() {
255-
return this.pdfManager.ensure(this, "content").then(content => {
256-
if (content instanceof BaseStream) {
257-
return content;
258-
}
259-
if (Array.isArray(content)) {
260-
return new StreamsSequenceStream(
261-
content,
262-
this._onSubStreamError.bind(this)
263-
);
264-
}
265-
// Replace non-existent page content with empty content.
266-
return new NullStream();
267-
});
254+
async getContentStream() {
255+
const content = await this.pdfManager.ensure(this, "content");
256+
257+
if (content instanceof BaseStream) {
258+
return content;
259+
}
260+
if (Array.isArray(content)) {
261+
return new StreamsSequenceStream(
262+
content,
263+
this._onSubStreamError.bind(this)
264+
);
265+
}
266+
// Replace non-existent page content with empty content.
267+
return new NullStream();
268268
}
269269

270270
get xfaData() {
@@ -375,7 +375,7 @@ class Page {
375375
}
376376
}
377377

378-
save(handler, task, annotationStorage, changes) {
378+
async save(handler, task, annotationStorage, changes) {
379379
const partialEvaluator = new PartialEvaluator({
380380
xref: this.xref,
381381
handler,
@@ -392,37 +392,34 @@ class Page {
392392

393393
// Fetch the page's annotations and save the content
394394
// in case of interactive form fields.
395-
return this._parsedAnnotations.then(function (annotations) {
396-
const promises = [];
397-
for (const annotation of annotations) {
398-
promises.push(
399-
annotation
400-
.save(partialEvaluator, task, annotationStorage, changes)
401-
.catch(function (reason) {
402-
warn(
403-
"save - ignoring annotation data during " +
404-
`"${task.name}" task: "${reason}".`
405-
);
406-
return null;
407-
})
408-
);
409-
}
395+
const annotations = await this._parsedAnnotations;
410396

411-
return Promise.all(promises);
412-
});
397+
const promises = [];
398+
for (const annotation of annotations) {
399+
promises.push(
400+
annotation
401+
.save(partialEvaluator, task, annotationStorage, changes)
402+
.catch(function (reason) {
403+
warn(
404+
"save - ignoring annotation data during " +
405+
`"${task.name}" task: "${reason}".`
406+
);
407+
return null;
408+
})
409+
);
410+
}
411+
return Promise.all(promises);
413412
}
414413

415-
loadResources(keys) {
414+
async loadResources(keys) {
416415
// TODO: add async `_getInheritableProperty` and remove this.
417-
this.resourcesPromise ||= this.pdfManager.ensure(this, "resources");
416+
await (this.resourcesPromise ??= this.pdfManager.ensure(this, "resources"));
418417

419-
return this.resourcesPromise.then(() => {
420-
const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
421-
return objectLoader.load();
422-
});
418+
const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
419+
await objectLoader.load();
423420
}
424421

425-
getOperatorList({
422+
async getOperatorList({
426423
handler,
427424
sink,
428425
task,
@@ -527,7 +524,7 @@ class Page {
527524
const pageListPromise = Promise.all([
528525
contentStreamPromise,
529526
resourcesPromise,
530-
]).then(([contentStream]) => {
527+
]).then(async ([contentStream]) => {
531528
const opList = new OperatorList(intent, sink);
532529

533530
handler.send("StartRenderPage", {
@@ -539,109 +536,102 @@ class Page {
539536
cacheKey,
540537
});
541538

542-
return partialEvaluator
543-
.getOperatorList({
544-
stream: contentStream,
545-
task,
546-
resources: this.resources,
547-
operatorList: opList,
548-
})
549-
.then(() => opList);
539+
await partialEvaluator.getOperatorList({
540+
stream: contentStream,
541+
task,
542+
resources: this.resources,
543+
operatorList: opList,
544+
});
545+
return opList;
550546
});
551547

552548
// Fetch the page's annotations and add their operator lists to the
553549
// page's operator list to render them.
554-
return Promise.all([
550+
// eslint-disable-next-line prefer-const
551+
let [pageOpList, annotations, newAnnotations] = await Promise.all([
555552
pageListPromise,
556553
this._parsedAnnotations,
557554
newAnnotationsPromise,
558-
]).then(function ([pageOpList, annotations, newAnnotations]) {
559-
if (newAnnotations) {
560-
// Some annotations can already exist (if it has the refToReplace
561-
// property). In this case, we replace the old annotation by the new
562-
// one.
563-
annotations = annotations.filter(
564-
a => !(a.ref && deletedAnnotations.has(a.ref))
565-
);
566-
for (let i = 0, ii = newAnnotations.length; i < ii; i++) {
567-
const newAnnotation = newAnnotations[i];
568-
if (newAnnotation.refToReplace) {
569-
const j = annotations.findIndex(
570-
a => a.ref && isRefsEqual(a.ref, newAnnotation.refToReplace)
571-
);
572-
if (j >= 0) {
573-
annotations.splice(j, 1, newAnnotation);
574-
newAnnotations.splice(i--, 1);
575-
ii--;
576-
}
555+
]);
556+
557+
if (newAnnotations) {
558+
// Some annotations can already exist (if it has the refToReplace
559+
// property). In this case, we replace the old annotation by the new one.
560+
annotations = annotations.filter(
561+
a => !(a.ref && deletedAnnotations.has(a.ref))
562+
);
563+
for (let i = 0, ii = newAnnotations.length; i < ii; i++) {
564+
const newAnnotation = newAnnotations[i];
565+
if (newAnnotation.refToReplace) {
566+
const j = annotations.findIndex(
567+
a => a.ref && isRefsEqual(a.ref, newAnnotation.refToReplace)
568+
);
569+
if (j >= 0) {
570+
annotations.splice(j, 1, newAnnotation);
571+
newAnnotations.splice(i--, 1);
572+
ii--;
577573
}
578574
}
579-
annotations = annotations.concat(newAnnotations);
580575
}
576+
annotations = annotations.concat(newAnnotations);
577+
}
578+
if (
579+
annotations.length === 0 ||
580+
intent & RenderingIntentFlag.ANNOTATIONS_DISABLE
581+
) {
582+
pageOpList.flush(/* lastChunk = */ true);
583+
return { length: pageOpList.totalLength };
584+
}
585+
const renderForms = !!(intent & RenderingIntentFlag.ANNOTATIONS_FORMS),
586+
isEditing = !!(intent & RenderingIntentFlag.IS_EDITING),
587+
intentAny = !!(intent & RenderingIntentFlag.ANY),
588+
intentDisplay = !!(intent & RenderingIntentFlag.DISPLAY),
589+
intentPrint = !!(intent & RenderingIntentFlag.PRINT);
590+
591+
// Collect the operator list promises for the annotations. Each promise
592+
// is resolved with the complete operator list for a single annotation.
593+
const opListPromises = [];
594+
for (const annotation of annotations) {
581595
if (
582-
annotations.length === 0 ||
583-
intent & RenderingIntentFlag.ANNOTATIONS_DISABLE
596+
intentAny ||
597+
(intentDisplay &&
598+
annotation.mustBeViewed(annotationStorage, renderForms) &&
599+
annotation.mustBeViewedWhenEditing(isEditing, modifiedIds)) ||
600+
(intentPrint && annotation.mustBePrinted(annotationStorage))
584601
) {
585-
pageOpList.flush(/* lastChunk = */ true);
586-
return { length: pageOpList.totalLength };
587-
}
588-
const renderForms = !!(intent & RenderingIntentFlag.ANNOTATIONS_FORMS),
589-
isEditing = !!(intent & RenderingIntentFlag.IS_EDITING),
590-
intentAny = !!(intent & RenderingIntentFlag.ANY),
591-
intentDisplay = !!(intent & RenderingIntentFlag.DISPLAY),
592-
intentPrint = !!(intent & RenderingIntentFlag.PRINT);
593-
594-
// Collect the operator list promises for the annotations. Each promise
595-
// is resolved with the complete operator list for a single annotation.
596-
const opListPromises = [];
597-
for (const annotation of annotations) {
598-
if (
599-
intentAny ||
600-
(intentDisplay &&
601-
annotation.mustBeViewed(annotationStorage, renderForms) &&
602-
annotation.mustBeViewedWhenEditing(isEditing, modifiedIds)) ||
603-
(intentPrint && annotation.mustBePrinted(annotationStorage))
604-
) {
605-
opListPromises.push(
606-
annotation
607-
.getOperatorList(
608-
partialEvaluator,
609-
task,
610-
intent,
611-
annotationStorage
612-
)
613-
.catch(function (reason) {
614-
warn(
615-
"getOperatorList - ignoring annotation data during " +
616-
`"${task.name}" task: "${reason}".`
617-
);
618-
return {
619-
opList: null,
620-
separateForm: false,
621-
separateCanvas: false,
622-
};
623-
})
624-
);
625-
}
602+
opListPromises.push(
603+
annotation
604+
.getOperatorList(partialEvaluator, task, intent, annotationStorage)
605+
.catch(function (reason) {
606+
warn(
607+
"getOperatorList - ignoring annotation data during " +
608+
`"${task.name}" task: "${reason}".`
609+
);
610+
return {
611+
opList: null,
612+
separateForm: false,
613+
separateCanvas: false,
614+
};
615+
})
616+
);
626617
}
618+
}
627619

628-
return Promise.all(opListPromises).then(function (opLists) {
629-
let form = false,
630-
canvas = false;
620+
const opLists = await Promise.all(opListPromises);
621+
let form = false,
622+
canvas = false;
631623

632-
for (const { opList, separateForm, separateCanvas } of opLists) {
633-
pageOpList.addOpList(opList);
624+
for (const { opList, separateForm, separateCanvas } of opLists) {
625+
pageOpList.addOpList(opList);
634626

635-
form ||= separateForm;
636-
canvas ||= separateCanvas;
637-
}
638-
pageOpList.flush(
639-
/* lastChunk = */ true,
640-
/* separateAnnots = */ { form, canvas }
641-
);
642-
return { length: pageOpList.totalLength };
643-
});
644-
});
627+
form ||= separateForm;
628+
canvas ||= separateCanvas;
629+
}
630+
pageOpList.flush(
631+
/* lastChunk = */ true,
632+
/* separateAnnots = */ { form, canvas }
633+
);
634+
return { length: pageOpList.totalLength };
645635
}
646636

647637
async extractTextContent({

0 commit comments

Comments
 (0)