Skip to content

Commit

Permalink
Simplify the newRefs computation in the "SaveDocument"-handler in t…
Browse files Browse the repository at this point in the history
…he 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.
  • Loading branch information
Snuffleupagus committed Jun 19, 2022
1 parent 45de73b commit bdf0a4b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ class Page {
);
}

return Promise.all(newRefsPromises);
return Promise.all(newRefsPromises).then(function (newRefs) {
return newRefs.filter(newRef => !!newRef);
});
});
}

Expand Down
16 changes: 4 additions & 12 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bdf0a4b

Please sign in to comment.