From bdf0a4bb8923766c812a0ab9b890da0c85ce53c0 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 19 Jun 2022 17:46:59 +0200 Subject: [PATCH] Simplify the `newRefs` computation in the "SaveDocument"-handler in the worker-thread - Let the `Page.save`-method filter out "empty" entries, similar to the `Page._parsedAnnotations`-getter, since that on its own already simplifies the "SaveDocument"-handler a tiny bit. - The existing `reduce` and `concat` construction isn't exactly a wonder of readability :-) Thanks to modern JavaScript features it should be possible to replace all of this with `Array.prototype.flat()` instead, which at least to me feels a lot easier to understand. --- src/core/document.js | 4 +++- src/core/worker.js | 16 ++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index b827589716b82c..a943e5dddb63ac 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -349,7 +349,9 @@ class Page { ); } - return Promise.all(newRefsPromises); + return Promise.all(newRefsPromises).then(function (newRefs) { + return newRefs.filter(newRef => !!newRef); + }); }); } diff --git a/src/core/worker.js b/src/core/worker.js index 1c1ccd916b157f..53b6a09286d73a 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -621,24 +621,16 @@ class WorkerMessageHandler { startXRef, ...refs ]) { - let newRefs = []; + const newRefs = refs.flat(2); let xfaData = null; if (isPureXfa) { xfaData = refs[0]; if (!xfaData) { return stream.bytes; } - } else { - for (const ref of refs) { - newRefs = ref - .filter(x => x !== null) - .reduce((a, b) => a.concat(b), newRefs); - } - - if (newRefs.length === 0) { - // No new refs so just return the initial bytes - return stream.bytes; - } + } else if (newRefs.length === 0) { + // No new refs so just return the initial bytes. + return stream.bytes; } const xfa = (acroForm instanceof Dict && acroForm.get("XFA")) || null;