Merge pull request #19674 from Snuffleupagus/core-document-more-async

Introduce more `async` code in the `src/core/document.js` file
This commit is contained in:
Jonas Jenwald 2025-03-17 21:40:16 +01:00 committed by GitHub
commit 00e3a4d87a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -251,8 +251,9 @@ class Page {
/** /**
* @returns {Promise<BaseStream>} * @returns {Promise<BaseStream>}
*/ */
getContentStream() { async getContentStream() {
return this.pdfManager.ensure(this, "content").then(content => { const content = await this.pdfManager.ensure(this, "content");
if (content instanceof BaseStream) { if (content instanceof BaseStream) {
return content; return content;
} }
@ -264,7 +265,6 @@ class Page {
} }
// Replace non-existent page content with empty content. // Replace non-existent page content with empty content.
return new NullStream(); return new NullStream();
});
} }
get xfaData() { get xfaData() {
@ -375,7 +375,7 @@ class Page {
} }
} }
save(handler, task, annotationStorage, changes) { async save(handler, task, annotationStorage, changes) {
const partialEvaluator = new PartialEvaluator({ const partialEvaluator = new PartialEvaluator({
xref: this.xref, xref: this.xref,
handler, handler,
@ -392,7 +392,8 @@ class Page {
// Fetch the page's annotations and save the content // Fetch the page's annotations and save the content
// in case of interactive form fields. // in case of interactive form fields.
return this._parsedAnnotations.then(function (annotations) { const annotations = await this._parsedAnnotations;
const promises = []; const promises = [];
for (const annotation of annotations) { for (const annotation of annotations) {
promises.push( promises.push(
@ -407,22 +408,18 @@ class Page {
}) })
); );
} }
return Promise.all(promises); return Promise.all(promises);
});
} }
loadResources(keys) { async loadResources(keys) {
// TODO: add async `_getInheritableProperty` and remove this. // TODO: add async `_getInheritableProperty` and remove this.
this.resourcesPromise ||= this.pdfManager.ensure(this, "resources"); await (this.resourcesPromise ??= this.pdfManager.ensure(this, "resources"));
return this.resourcesPromise.then(() => {
const objectLoader = new ObjectLoader(this.resources, keys, this.xref); const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
return objectLoader.load(); await objectLoader.load();
});
} }
getOperatorList({ async getOperatorList({
handler, handler,
sink, sink,
task, task,
@ -527,7 +524,7 @@ class Page {
const pageListPromise = Promise.all([ const pageListPromise = Promise.all([
contentStreamPromise, contentStreamPromise,
resourcesPromise, resourcesPromise,
]).then(([contentStream]) => { ]).then(async ([contentStream]) => {
const opList = new OperatorList(intent, sink); const opList = new OperatorList(intent, sink);
handler.send("StartRenderPage", { handler.send("StartRenderPage", {
@ -539,27 +536,27 @@ class Page {
cacheKey, cacheKey,
}); });
return partialEvaluator await partialEvaluator.getOperatorList({
.getOperatorList({
stream: contentStream, stream: contentStream,
task, task,
resources: this.resources, resources: this.resources,
operatorList: opList, operatorList: opList,
}) });
.then(() => opList); return opList;
}); });
// Fetch the page's annotations and add their operator lists to the // Fetch the page's annotations and add their operator lists to the
// page's operator list to render them. // page's operator list to render them.
return Promise.all([ // eslint-disable-next-line prefer-const
let [pageOpList, annotations, newAnnotations] = await Promise.all([
pageListPromise, pageListPromise,
this._parsedAnnotations, this._parsedAnnotations,
newAnnotationsPromise, newAnnotationsPromise,
]).then(function ([pageOpList, annotations, newAnnotations]) { ]);
if (newAnnotations) { if (newAnnotations) {
// Some annotations can already exist (if it has the refToReplace // Some annotations can already exist (if it has the refToReplace
// property). In this case, we replace the old annotation by the new // property). In this case, we replace the old annotation by the new one.
// one.
annotations = annotations.filter( annotations = annotations.filter(
a => !(a.ref && deletedAnnotations.has(a.ref)) a => !(a.ref && deletedAnnotations.has(a.ref))
); );
@ -604,12 +601,7 @@ class Page {
) { ) {
opListPromises.push( opListPromises.push(
annotation annotation
.getOperatorList( .getOperatorList(partialEvaluator, task, intent, annotationStorage)
partialEvaluator,
task,
intent,
annotationStorage
)
.catch(function (reason) { .catch(function (reason) {
warn( warn(
"getOperatorList - ignoring annotation data during " + "getOperatorList - ignoring annotation data during " +
@ -625,7 +617,7 @@ class Page {
} }
} }
return Promise.all(opListPromises).then(function (opLists) { const opLists = await Promise.all(opListPromises);
let form = false, let form = false,
canvas = false; canvas = false;
@ -640,8 +632,6 @@ class Page {
/* separateAnnots = */ { form, canvas } /* separateAnnots = */ { form, canvas }
); );
return { length: pageOpList.totalLength }; return { length: pageOpList.totalLength };
});
});
} }
async extractTextContent({ async extractTextContent({