Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,9 +1394,9 @@ class PartialEvaluator {
) {
const groupIds = [];
if (Array.isArray(optionalContentGroups)) {
optionalContent.get("OCGs").forEach(ocg => {
for (const ocg of optionalContentGroups) {
groupIds.push(ocg.toString());
});
}
} else {
// Dictionary, just use the obj id.
groupIds.push(optionalContentGroups.objId);
Expand Down
13 changes: 7 additions & 6 deletions src/core/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -1337,13 +1337,14 @@ var PostScriptCompiler = (function PostScriptCompilerClosure() {
}

var result = [];
instructions.forEach(function (instruction) {
var statementBuilder = new ExpressionBuilderVisitor();
for (const instruction of instructions) {
const statementBuilder = new ExpressionBuilderVisitor();
instruction.visit(statementBuilder);
result.push(statementBuilder.toString());
});
stack.forEach(function (expr, i) {
var statementBuilder = new ExpressionBuilderVisitor();
}
for (let i = 0, ii = stack.length; i < ii; i++) {
const expr = stack[i],
statementBuilder = new ExpressionBuilderVisitor();
expr.visit(statementBuilder);
var min = range[i * 2],
max = range[i * 2 + 1];
Expand All @@ -1359,7 +1360,7 @@ var PostScriptCompiler = (function PostScriptCompilerClosure() {
out.unshift("dest[destOffset + ", i, "] = ");
out.push(";");
result.push(out.join(""));
});
}
return result.join("\n");
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,10 @@ class WorkerMessageHandler {
cancelXHRs(new AbortException("Worker was terminated."));
}

WorkerTasks.forEach(function (task) {
for (const task of WorkerTasks) {
waitOn.push(task.finished);
task.terminate();
});
}

return Promise.all(waitOn).then(function () {
// Notice that even if we destroying handler, resolved response promise
Expand Down
5 changes: 2 additions & 3 deletions src/core/worker_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ class PDFWorkerStream {
if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason);
}
const readers = this._rangeRequestReaders.slice(0);
readers.forEach(function (reader) {
for (const reader of this._rangeRequestReaders.slice(0)) {
reader.cancel(reason);
});
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,9 @@ class AnnotationElement {
assert(this.quadrilaterals, "Missing quadrilaterals during rendering");
}

this.quadrilaterals.forEach(quadrilateral => {
for (const quadrilateral of this.quadrilaterals) {
quadrilateral.className = className;
});
}
return this.quadrilaterals;
}

Expand Down Expand Up @@ -1445,11 +1445,11 @@ class PopupElement {
}

// 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("mouseover", this._show.bind(this, false));
element.addEventListener("mouseout", this._hide.bind(this, false));
});
}
popup.addEventListener("click", this._hide.bind(this, true));

wrapper.appendChild(popup);
Expand Down Expand Up @@ -2104,9 +2104,9 @@ class AnnotationLayer {
`[data-annotation-id="${data.id}"]`
);
if (elements) {
elements.forEach(element => {
for (const element of elements) {
element.style.transform = transform;
});
}
}
}
parameters.div.hidden = false;
Expand Down
4 changes: 2 additions & 2 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2289,11 +2289,11 @@ class WorkerTransport {
const waitOn = [];
// We need to wait for all renderings to be completed, e.g.
// timeout/rAF can take a long time.
this.pageCache.forEach(function (page) {
for (const page of this.pageCache) {
if (page) {
waitOn.push(page._destroy());
}
});
}
this.pageCache.length = 0;
this.pagePromises.length = 0;
// Allow `AnnotationStorage`-related clean-up when destroying the document.
Expand Down
5 changes: 2 additions & 3 deletions src/display/fetch_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ class PDFFetchStream {
if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason);
}
const readers = this._rangeRequestReaders.slice(0);
readers.forEach(function (reader) {
for (const reader of this._rangeRequestReaders.slice(0)) {
reader.cancel(reason);
});
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class BaseFontLoader {
}

clear() {
this.nativeFontFaces.forEach(nativeFontFace => {
for (const nativeFontFace of this.nativeFontFaces) {
this._document.fonts.delete(nativeFontFace);
});
}
this.nativeFontFaces.length = 0;

if (this.styleElement) {
Expand Down
37 changes: 18 additions & 19 deletions src/display/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,9 @@ class PDFNetworkStream {
if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason);
}
const readers = this._rangeRequestReaders.slice(0);
readers.forEach(function (reader) {
for (const reader of this._rangeRequestReaders.slice(0)) {
reader.cancel(reason);
});
}
}
}

Expand Down Expand Up @@ -348,22 +347,22 @@ class PDFNetworkStreamFullRequestReader {
if (this._cachedChunks.length > 0) {
return;
}
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true });
});
this._requests = [];
}
this._requests.length = 0;
}

_onError(status) {
const url = this._url;
const exception = createResponseStatusError(status, url);
this._storedError = exception;
this._headersReceivedCapability.reject(exception);
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.reject(exception);
});
this._requests = [];
this._cachedChunks = [];
}
this._requests.length = 0;
this._cachedChunks.length = 0;
}

_onProgress(data) {
Expand Down Expand Up @@ -414,10 +413,10 @@ class PDFNetworkStreamFullRequestReader {
cancel(reason) {
this._done = true;
this._headersReceivedCapability.reject(reason);
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true });
});
this._requests = [];
}
this._requests.length = 0;
if (this._manager.isPendingRequest(this._fullRequestId)) {
this._manager.abortRequest(this._fullRequestId);
}
Expand Down Expand Up @@ -457,10 +456,10 @@ class PDFNetworkStreamRangeRequestReader {
this._queuedChunk = chunk;
}
this._done = true;
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true });
});
this._requests = [];
}
this._requests.length = 0;
this._close();
}

Expand Down Expand Up @@ -492,10 +491,10 @@ class PDFNetworkStreamRangeRequestReader {

cancel(reason) {
this._done = true;
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true });
});
this._requests = [];
}
this._requests.length = 0;
if (this._manager.isPendingRequest(this._requestId)) {
this._manager.abortRequest(this._requestId);
}
Expand Down
6 changes: 2 additions & 4 deletions src/display/node_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ class PDFNodeStream {
if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason);
}

const readers = this._rangeRequestReaders.slice(0);
readers.forEach(function (reader) {
for (const reader of this._rangeRequestReaders.slice(0)) {
reader.cancel(reason);
});
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/display/svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,9 +1370,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// The previous clipping group content can go out of order -- resetting
// cached clipGroups.
current.clipGroup = null;
this.extraStack.forEach(function (prev) {
for (const prev of this.extraStack) {
prev.clipGroup = null;
});
}
// Intersect with the previous clipping path.
clipPath.setAttributeNS(null, "clip-path", current.activeClipUrl);
}
Expand Down
23 changes: 12 additions & 11 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ const renderTextLayer = (function renderTextLayerClosure() {
// Finding intersections with expanded box.
const points = [[0, 0], [0, b.size[1]], [b.size[0], 0], b.size];
const ts = new Float64Array(64);
points.forEach(function (p, j) {
const t = Util.applyTransform(p, m);
for (let j = 0, jj = points.length; j < jj; j++) {
const t = Util.applyTransform(points[j], m);
ts[j + 0] = c && (e.left - t[0]) / c;
ts[j + 4] = s && (e.top - t[1]) / s;
ts[j + 8] = c && (e.right - t[0]) / c;
Expand All @@ -333,7 +333,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
ts[j + 52] = c && (e.top - t[1]) / -c;
ts[j + 56] = s && (e.right - t[0]) / s;
ts[j + 60] = c && (e.bottom - t[1]) / -c;
});
}
// Not based on math, but to simplify calculations, using cos and sin
// absolute values to not exceed the box (it can but insignificantly).
const boxScale = 1 + Math.min(Math.abs(c), Math.abs(s));
Expand All @@ -358,16 +358,17 @@ const renderTextLayer = (function renderTextLayerClosure() {
};
});
expandBoundsLTR(width, bounds);

const expanded = new Array(boxes.length);
bounds.forEach(function (b) {
for (const b of bounds) {
const i = b.index;
expanded[i] = {
left: b.x1New,
top: 0,
right: b.x2New,
bottom: 0,
};
});
}

// Rotating on 90 degrees and extending extended boxes. Reusing the bounds
// array and objects.
Expand All @@ -384,11 +385,11 @@ const renderTextLayer = (function renderTextLayerClosure() {
});
expandBoundsLTR(height, bounds);

bounds.forEach(function (b) {
for (const b of bounds) {
const i = b.index;
expanded[i].top = b.x1New;
expanded[i].bottom = b.x2New;
});
}
return expanded;
}

Expand Down Expand Up @@ -416,7 +417,7 @@ const renderTextLayer = (function renderTextLayerClosure() {
},
];

bounds.forEach(function (boundary) {
for (const boundary of bounds) {
// Searching for the affected part of horizon.
// TODO red-black tree or simple binary search
let i = 0;
Expand Down Expand Up @@ -555,15 +556,15 @@ const renderTextLayer = (function renderTextLayerClosure() {
horizon,
[i, j - i + 1].concat(changedHorizon)
);
});
}

// Set new x2 for all unset boundaries.
horizon.forEach(function (horizonPart) {
for (const horizonPart of horizon) {
const affectedBoundary = horizonPart.boundary;
if (affectedBoundary.x2New === undefined) {
affectedBoundary.x2New = Math.max(width, affectedBoundary.x2);
}
});
}
}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/display/transport_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,9 @@ class PDFDataTransportStream {
if (this._fullRequestReader) {
this._fullRequestReader.cancel(reason);
}
const readers = this._rangeReaders.slice(0);
readers.forEach(function (rangeReader) {
rangeReader.cancel(reason);
});
for (const reader of this._rangeReaders.slice(0)) {
reader.cancel(reason);
}
this._pdfDataRangeTransport.abort();
}
}
Expand Down Expand Up @@ -228,10 +227,10 @@ class PDFDataTransportStreamReader {

cancel(reason) {
this._done = true;
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true });
});
this._requests = [];
}
this._requests.length = 0;
}

progressiveDone() {
Expand Down Expand Up @@ -264,10 +263,10 @@ class PDFDataTransportStreamRangeReader {
} else {
const requestsCapability = this._requests.shift();
requestsCapability.resolve({ value: chunk, done: false });
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true });
});
this._requests = [];
}
this._requests.length = 0;
}
this._done = true;
this._stream._removeRangeReader(this);
Expand All @@ -293,10 +292,10 @@ class PDFDataTransportStreamRangeReader {

cancel(reason) {
this._done = true;
this._requests.forEach(function (requestCapability) {
for (const requestCapability of this._requests) {
requestCapability.resolve({ value: undefined, done: true });
});
this._requests = [];
}
this._requests.length = 0;
this._stream._removeRangeReader(this);
}
}
Expand Down