From 57c10ac21397d58c0d702187d04b3bc1a9944722 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 | 6 +----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index b827589716b82..a943e5dddb63a 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 1c1ccd916b157..8384c3a702e4b 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -629,11 +629,7 @@ class WorkerMessageHandler { return stream.bytes; } } else { - for (const ref of refs) { - newRefs = ref - .filter(x => x !== null) - .reduce((a, b) => a.concat(b), newRefs); - } + newRefs = refs.flat(2); if (newRefs.length === 0) { // No new refs so just return the initial bytes