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

ISSUE-11: Maximum call stack size exceeded with filters #12

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/async/for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ module.exports = asyncForEach;
/**
* Simultaneously processes all items in the given array.
*
* @param {DirectoryReader} ctx - The DirectoryReader context
* @param {object} dir - The current queue item
* @param {array} array - The array to iterate over
* @param {function} iterator - The function to call for each item in the array
* @param {function} done - The function to call when all iterators have completed
*/
function asyncForEach (array, iterator, done) {
function asyncForEach (ctx, dir, array, iterator, done) {
if (array.length === 0) {
// NOTE: Normally a bad idea to mix sync and async, but it's safe here because
// of the way that this method is currently used by DirectoryReader.
done();
done.call(ctx);
return;
}

// Simultaneously process all items in the array.
let pending = array.length;
array.forEach(item => {
iterator(item, () => {
iterator.call(ctx, dir, item, () => {
if (--pending === 0) {
done();
done.call(ctx);
}
});
});
Expand Down
6 changes: 4 additions & 2 deletions lib/directory-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ class DirectoryReader {
try {
// Process each item in the directory (simultaneously, if async)
facade.forEach(
this,
dir,
items,
this.processItem.bind(this, dir),
this.finishedReadingDirectory.bind(this, dir)
this.processItem,
this.finishedReadingDirectory
);
}
catch (err2) {
Expand Down
8 changes: 5 additions & 3 deletions lib/sync/for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ module.exports = syncForEach;
/**
* A facade that allows {@link Array.forEach} to be called as though it were asynchronous.
*
* @param {DirectoryReader} ctx - The DirectoryReader context
* @param {object} dir - The current queue item
* @param {array} array - The array to iterate over
* @param {function} iterator - The function to call for each item in the array
* @param {function} done - The function to call when all iterators have completed
*/
function syncForEach (array, iterator, done) {
function syncForEach (ctx, dir, array, iterator, done) {
array.forEach(item => {
iterator(item, () => {
iterator.call(ctx, dir, item, () => {
// Note: No error-handling here because this is currently only ever called
// by DirectoryReader, which never passes an `error` parameter to the callback.
// Instead, DirectoryReader emits an "error" event if an error occurs.
});
});

done();
done.call(ctx);
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
"engines": {
"node": ">=4"
}
}
}