Use await even more in the "SaveDocument" worker-thread handler

Given that the function is already asynchronous we can make use of `await` even more and reduce the amount of indentation a little bit.
This commit is contained in:
Jonas Jenwald 2023-09-16 13:06:48 +02:00
parent 4dd197ae3f
commit ff96c413d3

View File

@ -588,89 +588,88 @@ class WorkerMessageHandler {
); );
} }
} }
const refs = await Promise.all(promises);
return Promise.all(promises).then(refs => { let newRefs = [];
let newRefs = []; let xfaData = null;
let xfaData = null; if (isPureXfa) {
if (isPureXfa) { xfaData = refs[0];
xfaData = refs[0]; if (!xfaData) {
if (!xfaData) { return stream.bytes;
return stream.bytes; }
} } else {
} else { newRefs = refs.flat(2);
newRefs = refs.flat(2);
if (newRefs.length === 0) { if (newRefs.length === 0) {
// No new refs so just return the initial bytes // No new refs so just return the initial bytes
return stream.bytes; return stream.bytes;
}
}
const needAppearances =
acroFormRef &&
acroForm instanceof Dict &&
newRefs.some(ref => ref.needAppearances);
const xfa = (acroForm instanceof Dict && acroForm.get("XFA")) || null;
let xfaDatasetsRef = null;
let hasXfaDatasetsEntry = false;
if (Array.isArray(xfa)) {
for (let i = 0, ii = xfa.length; i < ii; i += 2) {
if (xfa[i] === "datasets") {
xfaDatasetsRef = xfa[i + 1];
hasXfaDatasetsEntry = true;
} }
} }
if (xfaDatasetsRef === null) {
xfaDatasetsRef = xref.getNewTemporaryRef();
}
} else if (xfa) {
// TODO: Support XFA streams.
warn("Unsupported XFA type.");
}
const needAppearances = let newXrefInfo = Object.create(null);
acroFormRef && if (xref.trailer) {
acroForm instanceof Dict && // Get string info from Info in order to compute fileId.
newRefs.some(ref => ref.needAppearances); const infoObj = Object.create(null);
const xrefInfo = xref.trailer.get("Info") || null;
const xfa = (acroForm instanceof Dict && acroForm.get("XFA")) || null; if (xrefInfo instanceof Dict) {
let xfaDatasetsRef = null; xrefInfo.forEach((key, value) => {
let hasXfaDatasetsEntry = false; if (typeof value === "string") {
if (Array.isArray(xfa)) { infoObj[key] = stringToPDFString(value);
for (let i = 0, ii = xfa.length; i < ii; i += 2) {
if (xfa[i] === "datasets") {
xfaDatasetsRef = xfa[i + 1];
hasXfaDatasetsEntry = true;
} }
} });
if (xfaDatasetsRef === null) {
xfaDatasetsRef = xref.getNewTemporaryRef();
}
} else if (xfa) {
// TODO: Support XFA streams.
warn("Unsupported XFA type.");
} }
let newXrefInfo = Object.create(null); newXrefInfo = {
if (xref.trailer) { rootRef: xref.trailer.getRaw("Root") || null,
// Get string info from Info in order to compute fileId. encryptRef: xref.trailer.getRaw("Encrypt") || null,
const infoObj = Object.create(null); newRef: xref.getNewTemporaryRef(),
const xrefInfo = xref.trailer.get("Info") || null; infoRef: xref.trailer.getRaw("Info") || null,
if (xrefInfo instanceof Dict) { info: infoObj,
xrefInfo.forEach((key, value) => { fileIds: xref.trailer.get("ID") || null,
if (typeof value === "string") { startXRef: linearization
infoObj[key] = stringToPDFString(value); ? startXRef
} : xref.lastXRefStreamPos ?? startXRef,
}); filename,
} };
}
newXrefInfo = { return incrementalUpdate({
rootRef: xref.trailer.getRaw("Root") || null, originalData: stream.bytes,
encryptRef: xref.trailer.getRaw("Encrypt") || null, xrefInfo: newXrefInfo,
newRef: xref.getNewTemporaryRef(), newRefs,
infoRef: xref.trailer.getRaw("Info") || null, xref,
info: infoObj, hasXfa: !!xfa,
fileIds: xref.trailer.get("ID") || null, xfaDatasetsRef,
startXRef: linearization hasXfaDatasetsEntry,
? startXRef needAppearances,
: xref.lastXRefStreamPos ?? startXRef, acroFormRef,
filename, acroForm,
}; xfaData,
} }).finally(() => {
xref.resetNewTemporaryRef();
return incrementalUpdate({
originalData: stream.bytes,
xrefInfo: newXrefInfo,
newRefs,
xref,
hasXfa: !!xfa,
xfaDatasetsRef,
hasXfaDatasetsEntry,
needAppearances,
acroFormRef,
acroForm,
xfaData,
}).finally(() => {
xref.resetNewTemporaryRef();
});
}); });
} }
); );