-
Notifications
You must be signed in to change notification settings - Fork 312
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
Light-weight async.parallel for fetcher's single use-case #68
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -33,11 +33,10 @@ and returns an identifying object: | |
} | ||
*/ | ||
|
||
var Backbone, CollectionStore, ModelStore, async, modelUtils, _; | ||
var Backbone, CollectionStore, ModelStore, modelUtils, _; | ||
|
||
_ = require('underscore'); | ||
Backbone = require('backbone'); | ||
async = require('async'); | ||
|
||
modelUtils = require('./modelUtils'); | ||
ModelStore = require('./store/model_store'); | ||
|
@@ -56,6 +55,34 @@ function Fetcher(options) { | |
}); | ||
} | ||
|
||
function parallel(tasks, callback) { | ||
var results = {}, | ||
completed = 0, | ||
length; | ||
|
||
if (_.isEmpty(tasks)) { | ||
return callback(null, results); | ||
} | ||
|
||
length = _.keys(tasks).length; | ||
_.each(tasks, function(task, key) { | ||
task(function(err) { | ||
var args = Array.prototype.slice.call(arguments, 1); | ||
if (args.length <= 1) { | ||
args = args[0]; | ||
} | ||
results[key] = args; | ||
|
||
if (err) { | ||
callback(err, results); | ||
callback = function() { }; | ||
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. Why set this to empty? 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. Oh, this is an alternative to breaking out of the loop? 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 it makes sense to bail early and stop iterating through the callbacks, in case we're doing some extra work in the tasks that we should avoid. 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 can see that. Would be nice if 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. Actually, breaking out won't really help. The asynchronous nature means we could already be through the loop before an error occurs. Checking for it would require tracking the state somehow, and seems like over engineering the problem. I could see not clearing the callback and continuing to collect results so it fires off again at the end. That may be strange behavior, though, as the callback could get used more than once (even more than twice). 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'll merge this in for the next minor version release, because removing 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. Yeah, rendr-app-template will need an update to the Gruntfile (although it is locked to the rendr release it works with, so that's good). |
||
} else if (++completed >= length) { | ||
callback(null, results); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Returns an instance of Model or Collection. | ||
*/ | ||
|
@@ -185,7 +212,7 @@ Fetcher.prototype._retrieve = function(fetchSpecs, options, callback) { | |
} | ||
}.bind(this); | ||
}, this); | ||
async.parallel(batchedRequests, callback); | ||
parallel(batchedRequests, callback); | ||
}; | ||
|
||
Fetcher.prototype.needsFetch = function(modelData, spec) { | ||
|
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.
What's the purpose of this? Is this trying to replicate behavior in
async
? When would there ever be more than one argument (besideserr
)?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.
This was simply to replicate async exactly.
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.
Cool, all good then.