Replace a bunch of Array.prototype.forEach() cases with for...of loops instead

Using `for...of` is a modern and generally much nicer pattern, since it gets rid of unnecessary callback-functions. (In a couple of spots, a "regular" `for` loop had to be used.)
This commit is contained in:
Jonas Jenwald 2021-04-24 12:36:01 +02:00
parent da0e7ea969
commit da22146b95
14 changed files with 67 additions and 71 deletions

View File

@ -1394,9 +1394,9 @@ class PartialEvaluator {
) { ) {
const groupIds = []; const groupIds = [];
if (Array.isArray(optionalContentGroups)) { if (Array.isArray(optionalContentGroups)) {
optionalContent.get("OCGs").forEach(ocg => { for (const ocg of optionalContentGroups) {
groupIds.push(ocg.toString()); groupIds.push(ocg.toString());
}); }
} else { } else {
// Dictionary, just use the obj id. // Dictionary, just use the obj id.
groupIds.push(optionalContentGroups.objId); groupIds.push(optionalContentGroups.objId);

View File

@ -1337,13 +1337,14 @@ var PostScriptCompiler = (function PostScriptCompilerClosure() {
} }
var result = []; var result = [];
instructions.forEach(function (instruction) { for (const instruction of instructions) {
var statementBuilder = new ExpressionBuilderVisitor(); const statementBuilder = new ExpressionBuilderVisitor();
instruction.visit(statementBuilder); instruction.visit(statementBuilder);
result.push(statementBuilder.toString()); result.push(statementBuilder.toString());
}); }
stack.forEach(function (expr, i) { for (let i = 0, ii = stack.length; i < ii; i++) {
var statementBuilder = new ExpressionBuilderVisitor(); const expr = stack[i],
statementBuilder = new ExpressionBuilderVisitor();
expr.visit(statementBuilder); expr.visit(statementBuilder);
var min = range[i * 2], var min = range[i * 2],
max = range[i * 2 + 1]; max = range[i * 2 + 1];
@ -1359,7 +1360,7 @@ var PostScriptCompiler = (function PostScriptCompilerClosure() {
out.unshift("dest[destOffset + ", i, "] = "); out.unshift("dest[destOffset + ", i, "] = ");
out.push(";"); out.push(";");
result.push(out.join("")); result.push(out.join(""));
}); }
return result.join("\n"); return result.join("\n");
} }
} }

View File

@ -785,10 +785,10 @@ class WorkerMessageHandler {
cancelXHRs(new AbortException("Worker was terminated.")); cancelXHRs(new AbortException("Worker was terminated."));
} }
WorkerTasks.forEach(function (task) { for (const task of WorkerTasks) {
waitOn.push(task.finished); waitOn.push(task.finished);
task.terminate(); task.terminate();
}); }
return Promise.all(waitOn).then(function () { return Promise.all(waitOn).then(function () {
// Notice that even if we destroying handler, resolved response promise // Notice that even if we destroying handler, resolved response promise

View File

@ -43,10 +43,9 @@ class PDFWorkerStream {
if (this._fullRequestReader) { if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason); this._fullRequestReader.cancel(reason);
} }
const readers = this._rangeRequestReaders.slice(0); for (const reader of this._rangeRequestReaders.slice(0)) {
readers.forEach(function (reader) {
reader.cancel(reason); reader.cancel(reason);
}); }
} }
} }

View File

@ -343,9 +343,9 @@ class AnnotationElement {
assert(this.quadrilaterals, "Missing quadrilaterals during rendering"); assert(this.quadrilaterals, "Missing quadrilaterals during rendering");
} }
this.quadrilaterals.forEach(quadrilateral => { for (const quadrilateral of this.quadrilaterals) {
quadrilateral.className = className; quadrilateral.className = className;
}); }
return this.quadrilaterals; return this.quadrilaterals;
} }
@ -1445,11 +1445,11 @@ class PopupElement {
} }
// Attach the event listeners to the trigger element. // Attach the event listeners to the trigger element.
this.trigger.forEach(element => { for (const element of this.trigger) {
element.addEventListener("click", this._toggle.bind(this)); element.addEventListener("click", this._toggle.bind(this));
element.addEventListener("mouseover", this._show.bind(this, false)); element.addEventListener("mouseover", this._show.bind(this, false));
element.addEventListener("mouseout", this._hide.bind(this, false)); element.addEventListener("mouseout", this._hide.bind(this, false));
}); }
popup.addEventListener("click", this._hide.bind(this, true)); popup.addEventListener("click", this._hide.bind(this, true));
wrapper.appendChild(popup); wrapper.appendChild(popup);
@ -2104,9 +2104,9 @@ class AnnotationLayer {
`[data-annotation-id="${data.id}"]` `[data-annotation-id="${data.id}"]`
); );
if (elements) { if (elements) {
elements.forEach(element => { for (const element of elements) {
element.style.transform = transform; element.style.transform = transform;
}); }
} }
} }
parameters.div.hidden = false; parameters.div.hidden = false;

View File

@ -2289,11 +2289,11 @@ class WorkerTransport {
const waitOn = []; const waitOn = [];
// We need to wait for all renderings to be completed, e.g. // We need to wait for all renderings to be completed, e.g.
// timeout/rAF can take a long time. // timeout/rAF can take a long time.
this.pageCache.forEach(function (page) { for (const page of this.pageCache) {
if (page) { if (page) {
waitOn.push(page._destroy()); waitOn.push(page._destroy());
} }
}); }
this.pageCache.length = 0; this.pageCache.length = 0;
this.pagePromises.length = 0; this.pagePromises.length = 0;
// Allow `AnnotationStorage`-related clean-up when destroying the document. // Allow `AnnotationStorage`-related clean-up when destroying the document.

View File

@ -91,10 +91,9 @@ class PDFFetchStream {
if (this._fullRequestReader) { if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason); this._fullRequestReader.cancel(reason);
} }
const readers = this._rangeRequestReaders.slice(0); for (const reader of this._rangeRequestReaders.slice(0)) {
readers.forEach(function (reader) {
reader.cancel(reason); reader.cancel(reason);
}); }
} }
} }

View File

@ -61,9 +61,9 @@ class BaseFontLoader {
} }
clear() { clear() {
this.nativeFontFaces.forEach(nativeFontFace => { for (const nativeFontFace of this.nativeFontFaces) {
this._document.fonts.delete(nativeFontFace); this._document.fonts.delete(nativeFontFace);
}); }
this.nativeFontFaces.length = 0; this.nativeFontFaces.length = 0;
if (this.styleElement) { if (this.styleElement) {

View File

@ -259,10 +259,9 @@ class PDFNetworkStream {
if (this._fullRequestReader) { if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason); this._fullRequestReader.cancel(reason);
} }
const readers = this._rangeRequestReaders.slice(0); for (const reader of this._rangeRequestReaders.slice(0)) {
readers.forEach(function (reader) {
reader.cancel(reason); reader.cancel(reason);
}); }
} }
} }
@ -348,9 +347,9 @@ class PDFNetworkStreamFullRequestReader {
if (this._cachedChunks.length > 0) { if (this._cachedChunks.length > 0) {
return; return;
} }
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true }); requestCapability.resolve({ value: undefined, done: true });
}); }
this._requests = []; this._requests = [];
} }
@ -359,9 +358,9 @@ class PDFNetworkStreamFullRequestReader {
const exception = createResponseStatusError(status, url); const exception = createResponseStatusError(status, url);
this._storedError = exception; this._storedError = exception;
this._headersReceivedCapability.reject(exception); this._headersReceivedCapability.reject(exception);
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.reject(exception); requestCapability.reject(exception);
}); }
this._requests = []; this._requests = [];
this._cachedChunks = []; this._cachedChunks = [];
} }
@ -414,9 +413,9 @@ class PDFNetworkStreamFullRequestReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
this._headersReceivedCapability.reject(reason); this._headersReceivedCapability.reject(reason);
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true }); requestCapability.resolve({ value: undefined, done: true });
}); }
this._requests = []; this._requests = [];
if (this._manager.isPendingRequest(this._fullRequestId)) { if (this._manager.isPendingRequest(this._fullRequestId)) {
this._manager.abortRequest(this._fullRequestId); this._manager.abortRequest(this._fullRequestId);
@ -457,9 +456,9 @@ class PDFNetworkStreamRangeRequestReader {
this._queuedChunk = chunk; this._queuedChunk = chunk;
} }
this._done = true; this._done = true;
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true }); requestCapability.resolve({ value: undefined, done: true });
}); }
this._requests = []; this._requests = [];
this._close(); this._close();
} }
@ -492,9 +491,9 @@ class PDFNetworkStreamRangeRequestReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true }); requestCapability.resolve({ value: undefined, done: true });
}); }
this._requests = []; this._requests = [];
if (this._manager.isPendingRequest(this._requestId)) { if (this._manager.isPendingRequest(this._requestId)) {
this._manager.abortRequest(this._requestId); this._manager.abortRequest(this._requestId);

View File

@ -98,11 +98,9 @@ class PDFNodeStream {
if (this._fullRequestReader) { if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason); this._fullRequestReader.cancel(reason);
} }
for (const reader of this._rangeRequestReaders.slice(0)) {
const readers = this._rangeRequestReaders.slice(0);
readers.forEach(function (reader) {
reader.cancel(reason); reader.cancel(reason);
}); }
} }
} }

View File

@ -1370,9 +1370,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// The previous clipping group content can go out of order -- resetting // The previous clipping group content can go out of order -- resetting
// cached clipGroups. // cached clipGroups.
current.clipGroup = null; current.clipGroup = null;
this.extraStack.forEach(function (prev) { for (const prev of this.extraStack) {
prev.clipGroup = null; prev.clipGroup = null;
}); }
// Intersect with the previous clipping path. // Intersect with the previous clipping path.
clipPath.setAttributeNS(null, "clip-path", current.activeClipUrl); clipPath.setAttributeNS(null, "clip-path", current.activeClipUrl);
} }

View File

@ -312,8 +312,8 @@ const renderTextLayer = (function renderTextLayerClosure() {
// Finding intersections with expanded box. // Finding intersections with expanded box.
const points = [[0, 0], [0, b.size[1]], [b.size[0], 0], b.size]; const points = [[0, 0], [0, b.size[1]], [b.size[0], 0], b.size];
const ts = new Float64Array(64); const ts = new Float64Array(64);
points.forEach(function (p, j) { for (let j = 0, jj = points.length; j < jj; j++) {
const t = Util.applyTransform(p, m); const t = Util.applyTransform(points[j], m);
ts[j + 0] = c && (e.left - t[0]) / c; ts[j + 0] = c && (e.left - t[0]) / c;
ts[j + 4] = s && (e.top - t[1]) / s; ts[j + 4] = s && (e.top - t[1]) / s;
ts[j + 8] = c && (e.right - t[0]) / c; ts[j + 8] = c && (e.right - t[0]) / c;
@ -333,7 +333,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
ts[j + 52] = c && (e.top - t[1]) / -c; ts[j + 52] = c && (e.top - t[1]) / -c;
ts[j + 56] = s && (e.right - t[0]) / s; ts[j + 56] = s && (e.right - t[0]) / s;
ts[j + 60] = c && (e.bottom - t[1]) / -c; ts[j + 60] = c && (e.bottom - t[1]) / -c;
}); }
// Not based on math, but to simplify calculations, using cos and sin // Not based on math, but to simplify calculations, using cos and sin
// absolute values to not exceed the box (it can but insignificantly). // absolute values to not exceed the box (it can but insignificantly).
const boxScale = 1 + Math.min(Math.abs(c), Math.abs(s)); const boxScale = 1 + Math.min(Math.abs(c), Math.abs(s));
@ -358,8 +358,9 @@ const renderTextLayer = (function renderTextLayerClosure() {
}; };
}); });
expandBoundsLTR(width, bounds); expandBoundsLTR(width, bounds);
const expanded = new Array(boxes.length); const expanded = new Array(boxes.length);
bounds.forEach(function (b) { for (const b of bounds) {
const i = b.index; const i = b.index;
expanded[i] = { expanded[i] = {
left: b.x1New, left: b.x1New,
@ -367,7 +368,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
right: b.x2New, right: b.x2New,
bottom: 0, bottom: 0,
}; };
}); }
// Rotating on 90 degrees and extending extended boxes. Reusing the bounds // Rotating on 90 degrees and extending extended boxes. Reusing the bounds
// array and objects. // array and objects.
@ -384,11 +385,11 @@ const renderTextLayer = (function renderTextLayerClosure() {
}); });
expandBoundsLTR(height, bounds); expandBoundsLTR(height, bounds);
bounds.forEach(function (b) { for (const b of bounds) {
const i = b.index; const i = b.index;
expanded[i].top = b.x1New; expanded[i].top = b.x1New;
expanded[i].bottom = b.x2New; expanded[i].bottom = b.x2New;
}); }
return expanded; return expanded;
} }
@ -416,7 +417,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
}, },
]; ];
bounds.forEach(function (boundary) { for (const boundary of bounds) {
// Searching for the affected part of horizon. // Searching for the affected part of horizon.
// TODO red-black tree or simple binary search // TODO red-black tree or simple binary search
let i = 0; let i = 0;
@ -555,15 +556,15 @@ const renderTextLayer = (function renderTextLayerClosure() {
horizon, horizon,
[i, j - i + 1].concat(changedHorizon) [i, j - i + 1].concat(changedHorizon)
); );
}); }
// Set new x2 for all unset boundaries. // Set new x2 for all unset boundaries.
horizon.forEach(function (horizonPart) { for (const horizonPart of horizon) {
const affectedBoundary = horizonPart.boundary; const affectedBoundary = horizonPart.boundary;
if (affectedBoundary.x2New === undefined) { if (affectedBoundary.x2New === undefined) {
affectedBoundary.x2New = Math.max(width, affectedBoundary.x2); affectedBoundary.x2New = Math.max(width, affectedBoundary.x2);
} }
}); }
} }
/** /**

View File

@ -147,10 +147,9 @@ class PDFDataTransportStream {
if (this._fullRequestReader) { if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason); this._fullRequestReader.cancel(reason);
} }
const readers = this._rangeReaders.slice(0); for (const reader of this._rangeReaders.slice(0)) {
readers.forEach(function (rangeReader) { reader.cancel(reason);
rangeReader.cancel(reason); }
});
this._pdfDataRangeTransport.abort(); this._pdfDataRangeTransport.abort();
} }
} }
@ -228,9 +227,9 @@ class PDFDataTransportStreamReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true }); requestCapability.resolve({ value: undefined, done: true });
}); }
this._requests = []; this._requests = [];
} }
@ -264,9 +263,9 @@ class PDFDataTransportStreamRangeReader {
} else { } else {
const requestsCapability = this._requests.shift(); const requestsCapability = this._requests.shift();
requestsCapability.resolve({ value: chunk, done: false }); requestsCapability.resolve({ value: chunk, done: false });
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true }); requestCapability.resolve({ value: undefined, done: true });
}); }
this._requests = []; this._requests = [];
} }
this._done = true; this._done = true;
@ -293,9 +292,9 @@ class PDFDataTransportStreamRangeReader {
cancel(reason) { cancel(reason) {
this._done = true; this._done = true;
this._requests.forEach(function (requestCapability) { for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true }); requestCapability.resolve({ value: undefined, done: true });
}); }
this._requests = []; this._requests = [];
this._stream._removeRangeReader(this); this._stream._removeRangeReader(this);
} }

View File

@ -775,22 +775,22 @@ class EventBus {
let externalListeners; let externalListeners;
// Making copy of the listeners array in case if it will be modified // Making copy of the listeners array in case if it will be modified
// during dispatch. // during dispatch.
eventListeners.slice(0).forEach(({ listener, external, once }) => { for (const { listener, external, once } of eventListeners.slice(0)) {
if (once) { if (once) {
this._off(eventName, listener); this._off(eventName, listener);
} }
if (external) { if (external) {
(externalListeners ||= []).push(listener); (externalListeners ||= []).push(listener);
return; continue;
} }
listener.apply(null, args); listener.apply(null, args);
}); }
// Dispatch any "external" listeners *after* the internal ones, to give the // Dispatch any "external" listeners *after* the internal ones, to give the
// viewer components time to handle events and update their state first. // viewer components time to handle events and update their state first.
if (externalListeners) { if (externalListeners) {
externalListeners.forEach(listener => { for (const listener of externalListeners) {
listener.apply(null, args); listener.apply(null, args);
}); }
externalListeners = null; externalListeners = null;
} }
if ( if (