Merge pull request #18871 from Snuffleupagus/PDFDocument-more-ensure
Improve the promise-caching in the `PDFDocument.fieldObjects` getter
This commit is contained in:
commit
1269c6a4f9
@ -1871,49 +1871,52 @@ class PDFDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get fieldObjects() {
|
get fieldObjects() {
|
||||||
if (!this.formInfo.hasFields) {
|
const promise = this.pdfManager
|
||||||
return shadow(this, "fieldObjects", Promise.resolve(null));
|
.ensureDoc("formInfo")
|
||||||
}
|
.then(async formInfo => {
|
||||||
|
if (!formInfo.hasFields) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const promise = Promise.all([
|
const [annotationGlobals, acroForm] = await Promise.all([
|
||||||
this.pdfManager.ensureDoc("annotationGlobals"),
|
this.pdfManager.ensureDoc("annotationGlobals"),
|
||||||
this.pdfManager.ensureCatalog("acroForm"),
|
this.pdfManager.ensureCatalog("acroForm"),
|
||||||
]).then(async ([annotationGlobals, acroForm]) => {
|
]);
|
||||||
if (!annotationGlobals) {
|
if (!annotationGlobals) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const visitedRefs = new RefSet();
|
const visitedRefs = new RefSet();
|
||||||
const allFields = Object.create(null);
|
const allFields = Object.create(null);
|
||||||
const fieldPromises = new Map();
|
const fieldPromises = new Map();
|
||||||
const orphanFields = new RefSetCache();
|
const orphanFields = new RefSetCache();
|
||||||
for (const fieldRef of await acroForm.getAsync("Fields")) {
|
for (const fieldRef of await acroForm.getAsync("Fields")) {
|
||||||
await this.#collectFieldObjects(
|
await this.#collectFieldObjects(
|
||||||
"",
|
"",
|
||||||
null,
|
null,
|
||||||
fieldRef,
|
fieldRef,
|
||||||
fieldPromises,
|
fieldPromises,
|
||||||
annotationGlobals,
|
annotationGlobals,
|
||||||
visitedRefs,
|
visitedRefs,
|
||||||
orphanFields
|
orphanFields
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const allPromises = [];
|
const allPromises = [];
|
||||||
for (const [name, promises] of fieldPromises) {
|
for (const [name, promises] of fieldPromises) {
|
||||||
allPromises.push(
|
allPromises.push(
|
||||||
Promise.all(promises).then(fields => {
|
Promise.all(promises).then(fields => {
|
||||||
fields = fields.filter(field => !!field);
|
fields = fields.filter(field => !!field);
|
||||||
if (fields.length > 0) {
|
if (fields.length > 0) {
|
||||||
allFields[name] = fields;
|
allFields[name] = fields;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(allPromises);
|
await Promise.all(allPromises);
|
||||||
return { allFields, orphanFields };
|
return { allFields, orphanFields };
|
||||||
});
|
});
|
||||||
|
|
||||||
return shadow(this, "fieldObjects", promise);
|
return shadow(this, "fieldObjects", promise);
|
||||||
}
|
}
|
||||||
@ -1944,12 +1947,7 @@ class PDFDocument {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get calculationOrderIds() {
|
get calculationOrderIds() {
|
||||||
const acroForm = this.catalog.acroForm;
|
const calculationOrder = this.catalog.acroForm?.get("CO");
|
||||||
if (!acroForm?.has("CO")) {
|
|
||||||
return shadow(this, "calculationOrderIds", null);
|
|
||||||
}
|
|
||||||
|
|
||||||
const calculationOrder = acroForm.get("CO");
|
|
||||||
if (!Array.isArray(calculationOrder) || calculationOrder.length === 0) {
|
if (!Array.isArray(calculationOrder) || calculationOrder.length === 0) {
|
||||||
return shadow(this, "calculationOrderIds", null);
|
return shadow(this, "calculationOrderIds", null);
|
||||||
}
|
}
|
||||||
@ -1960,10 +1958,7 @@ class PDFDocument {
|
|||||||
ids.push(id.toString());
|
ids.push(id.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ids.length === 0) {
|
return shadow(this, "calculationOrderIds", ids.length ? ids : null);
|
||||||
return shadow(this, "calculationOrderIds", null);
|
|
||||||
}
|
|
||||||
return shadow(this, "calculationOrderIds", ids);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get annotationGlobals() {
|
get annotationGlobals() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user