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
Optimized waterfall, parallel, et al. #1395
Changes from 4 commits
cc06eac
4014466
2857bc7
2ae3492
67e76ea
fddea47
a6bd3a0
9b0f9eb
dddb9f5
53019a8
b8a051b
6ef3a91
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import noop from 'lodash/noop'; | ||
import rest from './internal/rest'; | ||
import slice from './internal/slice'; | ||
import onlyOnce from './internal/onlyOnce'; | ||
import wrapAsync from './internal/wrapAsync'; | ||
|
||
|
@@ -28,11 +28,12 @@ export default function doDuring(fn, test, callback) { | |
var _fn = wrapAsync(fn); | ||
var _test = wrapAsync(test); | ||
|
||
var next = rest(function(err, args) { | ||
if (err) return callback(err); | ||
function next(err/*, args...*/) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mind changing comments to |
||
if (err) return callback(err); | ||
var args = slice(arguments, 1); | ||
args.push(check); | ||
_test.apply(this, args); | ||
}); | ||
}; | ||
|
||
function check(err, truth) { | ||
if (err) return callback(err); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function slice(arrayLike, start) { | ||
start = start|0; | ||
var newLen = Math.max(arrayLike.length - start, 0); | ||
var newArr = Array(newLen); | ||
for(var idx = 0; idx < newLen; idx++) { | ||
newArr[idx] = arrayLike[start + idx]; | ||
} | ||
return newArr; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import initialParams from './internal/initialParams'; | ||
import rest from './internal/rest'; | ||
import slice from './internal/slice'; | ||
import wrapAsync from './internal/wrapAsync'; | ||
|
||
/** | ||
|
@@ -44,23 +44,19 @@ import wrapAsync from './internal/wrapAsync'; | |
export default function reflect(fn) { | ||
var _fn = wrapAsync(fn); | ||
return initialParams(function reflectOn(args, reflectCallback) { | ||
args.push(rest(function callback(err, cbArgs) { | ||
args.push(function callback(err, cbArg) { | ||
if (err) { | ||
reflectCallback(null, { | ||
error: err | ||
}); | ||
} else { | ||
var value = null; | ||
if (cbArgs.length === 1) { | ||
value = cbArgs[0]; | ||
} else if (cbArgs.length > 1) { | ||
value = cbArgs; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prefer this condition, except removing the |
||
var value = cbArg; | ||
if (arguments.length > 2) value = slice(arguments, 1); | ||
reflectCallback(null, { | ||
value: value | ||
}); | ||
} | ||
})); | ||
}); | ||
|
||
return _fn.apply(this, args); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import noop from 'lodash/noop'; | ||
import rest from './internal/rest'; | ||
import slice from 'lodash/slice'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as before, sorry, is it possible to use |
||
import reduce from './reduce'; | ||
import wrapAsync from './internal/wrapAsync'; | ||
import arrayMap from 'lodash/_arrayMap'; | ||
|
@@ -55,9 +56,10 @@ export default rest(function seq(functions) { | |
} | ||
|
||
reduce(_functions, args, function(newargs, fn, cb) { | ||
fn.apply(that, newargs.concat(rest(function(err, nextargs) { | ||
fn.apply(that, newargs.concat(function(err/*, nextargs...*/) { | ||
var nextargs = slice(arguments, 1); | ||
cb(err, nextargs); | ||
}))); | ||
})); | ||
}, | ||
function(err, results) { | ||
cb.apply(that, [err].concat(results)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import isArray from 'lodash/isArray'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really nice cleanup of waterfall ⭐ |
||
import noop from 'lodash/noop'; | ||
import once from './internal/once'; | ||
import rest from './internal/rest'; | ||
import slice from './internal/slice'; | ||
|
||
import onlyOnce from './internal/onlyOnce'; | ||
import wrapAsync from './internal/wrapAsync'; | ||
|
@@ -74,12 +74,13 @@ export default function(tasks, callback) { | |
return callback.apply(null, [null].concat(args)); | ||
} | ||
|
||
var taskCallback = onlyOnce(rest(function(err, args) { | ||
var taskCallback = onlyOnce(function(err/*, cbArgs...*/) { | ||
var cbArgs = slice(arguments, 1); | ||
if (err) { | ||
return callback.apply(null, [err].concat(args)); | ||
return callback.apply(null, [err].concat(cbArgs)); | ||
} | ||
nextTask(args); | ||
})); | ||
nextTask(cbArgs); | ||
}); | ||
|
||
args.push(taskCallback); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import noop from 'lodash/noop'; | ||
import rest from './internal/rest'; | ||
import slice from './internal/slice'; | ||
|
||
import onlyOnce from './internal/onlyOnce'; | ||
import wrapAsync from './internal/wrapAsync'; | ||
|
@@ -42,10 +42,10 @@ export default function whilst(test, iteratee, callback) { | |
callback = onlyOnce(callback || noop); | ||
var _iteratee = wrapAsync(iteratee); | ||
if (!test()) return callback(null); | ||
var next = rest(function(err, args) { | ||
var next = function(err/*, args...*/) { | ||
if (err) return callback(err); | ||
if (test()) return _iteratee(next); | ||
callback.apply(null, [null].concat(args)); | ||
}); | ||
callback.apply(null, [null].concat(slice(arguments, 1))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I long for splats 💨 |
||
}; | ||
_iteratee(next); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use
./internal/slice
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, looks like my auto-import plugin picked up the wrong
slice
.