Skip to content

Commit

Permalink
Merge pull request #18871 from Snuffleupagus/PDFDocument-more-ensure
Browse files Browse the repository at this point in the history
Improve the promise-caching in the `PDFDocument.fieldObjects` getter
  • Loading branch information
timvandermeij authored Oct 8, 2024
2 parents b15dd55 + 662bd02 commit 1269c6a
Showing 1 changed file with 44 additions and 49 deletions.
93 changes: 44 additions & 49 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1871,49 +1871,52 @@ class PDFDocument {
}

get fieldObjects() {
if (!this.formInfo.hasFields) {
return shadow(this, "fieldObjects", Promise.resolve(null));
}
const promise = this.pdfManager
.ensureDoc("formInfo")
.then(async formInfo => {
if (!formInfo.hasFields) {
return null;
}

const promise = Promise.all([
this.pdfManager.ensureDoc("annotationGlobals"),
this.pdfManager.ensureCatalog("acroForm"),
]).then(async ([annotationGlobals, acroForm]) => {
if (!annotationGlobals) {
return null;
}
const [annotationGlobals, acroForm] = await Promise.all([
this.pdfManager.ensureDoc("annotationGlobals"),
this.pdfManager.ensureCatalog("acroForm"),
]);
if (!annotationGlobals) {
return null;
}

const visitedRefs = new RefSet();
const allFields = Object.create(null);
const fieldPromises = new Map();
const orphanFields = new RefSetCache();
for (const fieldRef of await acroForm.getAsync("Fields")) {
await this.#collectFieldObjects(
"",
null,
fieldRef,
fieldPromises,
annotationGlobals,
visitedRefs,
orphanFields
);
}
const visitedRefs = new RefSet();
const allFields = Object.create(null);
const fieldPromises = new Map();
const orphanFields = new RefSetCache();
for (const fieldRef of await acroForm.getAsync("Fields")) {
await this.#collectFieldObjects(
"",
null,
fieldRef,
fieldPromises,
annotationGlobals,
visitedRefs,
orphanFields
);
}

const allPromises = [];
for (const [name, promises] of fieldPromises) {
allPromises.push(
Promise.all(promises).then(fields => {
fields = fields.filter(field => !!field);
if (fields.length > 0) {
allFields[name] = fields;
}
})
);
}
const allPromises = [];
for (const [name, promises] of fieldPromises) {
allPromises.push(
Promise.all(promises).then(fields => {
fields = fields.filter(field => !!field);
if (fields.length > 0) {
allFields[name] = fields;
}
})
);
}

await Promise.all(allPromises);
return { allFields, orphanFields };
});
await Promise.all(allPromises);
return { allFields, orphanFields };
});

return shadow(this, "fieldObjects", promise);
}
Expand Down Expand Up @@ -1944,12 +1947,7 @@ class PDFDocument {
}

get calculationOrderIds() {
const acroForm = this.catalog.acroForm;
if (!acroForm?.has("CO")) {
return shadow(this, "calculationOrderIds", null);
}

const calculationOrder = acroForm.get("CO");
const calculationOrder = this.catalog.acroForm?.get("CO");
if (!Array.isArray(calculationOrder) || calculationOrder.length === 0) {
return shadow(this, "calculationOrderIds", null);
}
Expand All @@ -1960,10 +1958,7 @@ class PDFDocument {
ids.push(id.toString());
}
}
if (ids.length === 0) {
return shadow(this, "calculationOrderIds", null);
}
return shadow(this, "calculationOrderIds", ids);
return shadow(this, "calculationOrderIds", ids.length ? ids : null);
}

get annotationGlobals() {
Expand Down

0 comments on commit 1269c6a

Please sign in to comment.