From ff96c413d3efdc3ec08ff6c7596d57b555c1af99 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Sat, 16 Sep 2023 13:06:48 +0200
Subject: [PATCH] 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.
---
 src/core/worker.js | 147 ++++++++++++++++++++++-----------------------
 1 file changed, 73 insertions(+), 74 deletions(-)

diff --git a/src/core/worker.js b/src/core/worker.js
index d03c2b957..94f5d2fd6 100644
--- a/src/core/worker.js
+++ b/src/core/worker.js
@@ -588,89 +588,88 @@ class WorkerMessageHandler {
             );
           }
         }
+        const refs = await Promise.all(promises);
 
-        return Promise.all(promises).then(refs => {
-          let newRefs = [];
-          let xfaData = null;
-          if (isPureXfa) {
-            xfaData = refs[0];
-            if (!xfaData) {
-              return stream.bytes;
-            }
-          } else {
-            newRefs = refs.flat(2);
+        let newRefs = [];
+        let xfaData = null;
+        if (isPureXfa) {
+          xfaData = refs[0];
+          if (!xfaData) {
+            return stream.bytes;
+          }
+        } else {
+          newRefs = refs.flat(2);
 
-            if (newRefs.length === 0) {
-              // No new refs so just return the initial bytes
-              return stream.bytes;
+          if (newRefs.length === 0) {
+            // No new refs so just return the initial 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 =
-            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;
+        let newXrefInfo = Object.create(null);
+        if (xref.trailer) {
+          // Get string info from Info in order to compute fileId.
+          const infoObj = Object.create(null);
+          const xrefInfo = xref.trailer.get("Info") || null;
+          if (xrefInfo instanceof Dict) {
+            xrefInfo.forEach((key, value) => {
+              if (typeof value === "string") {
+                infoObj[key] = stringToPDFString(value);
               }
-            }
-            if (xfaDatasetsRef === null) {
-              xfaDatasetsRef = xref.getNewTemporaryRef();
-            }
-          } else if (xfa) {
-            // TODO: Support XFA streams.
-            warn("Unsupported XFA type.");
+            });
           }
 
-          let newXrefInfo = Object.create(null);
-          if (xref.trailer) {
-            // Get string info from Info in order to compute fileId.
-            const infoObj = Object.create(null);
-            const xrefInfo = xref.trailer.get("Info") || null;
-            if (xrefInfo instanceof Dict) {
-              xrefInfo.forEach((key, value) => {
-                if (typeof value === "string") {
-                  infoObj[key] = stringToPDFString(value);
-                }
-              });
-            }
+          newXrefInfo = {
+            rootRef: xref.trailer.getRaw("Root") || null,
+            encryptRef: xref.trailer.getRaw("Encrypt") || null,
+            newRef: xref.getNewTemporaryRef(),
+            infoRef: xref.trailer.getRaw("Info") || null,
+            info: infoObj,
+            fileIds: xref.trailer.get("ID") || null,
+            startXRef: linearization
+              ? startXRef
+              : xref.lastXRefStreamPos ?? startXRef,
+            filename,
+          };
+        }
 
-            newXrefInfo = {
-              rootRef: xref.trailer.getRaw("Root") || null,
-              encryptRef: xref.trailer.getRaw("Encrypt") || null,
-              newRef: xref.getNewTemporaryRef(),
-              infoRef: xref.trailer.getRaw("Info") || null,
-              info: infoObj,
-              fileIds: xref.trailer.get("ID") || null,
-              startXRef: linearization
-                ? startXRef
-                : xref.lastXRefStreamPos ?? startXRef,
-              filename,
-            };
-          }
-
-          return incrementalUpdate({
-            originalData: stream.bytes,
-            xrefInfo: newXrefInfo,
-            newRefs,
-            xref,
-            hasXfa: !!xfa,
-            xfaDatasetsRef,
-            hasXfaDatasetsEntry,
-            needAppearances,
-            acroFormRef,
-            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();
         });
       }
     );