Permalink
Browse files

Move function body out of waterfall, add comments for clarity

  • Loading branch information...
1 parent 71eeae5 commit 8cac9698636e8e19472b09c705593b6f00abaa77 @nikhilm committed Nov 30, 2012
Showing with 22 additions and 15 deletions.
  1. +22 −15 lib/knode.js
View
@@ -378,22 +378,29 @@ exports.KNode.prototype.connect = function(address, port, cb) {
var callback = cb || function() {};
assert.ok(this.self.nodeID);
var contact = util.make_contact(address, port);
+
+ var refreshBucketsFartherThanClosestKnown = function(type, contacts, asyncCallback) {
+ // FIXME: Do we update buckets or does iterativeFindNode do it?
+ var leastBucket = _.min(_.keys(this._buckets));
+ var bucketsToRefresh = _.filter(_.keys(this._buckets),
+ function(num) { return num >= leastBucket; });
+ var queue = async.queue(_.bind(this._refreshBucket, this), 1);
+ _.each(bucketsToRefresh, function(bucketId) {
+ // wrapper is required because the each iterator is passed
+ // 3 arguments (element, index, list) and queue.push interprets
+ // the second argument as a callback
+ queue.push(bucketId);
+ });
+ asyncCallback(); // success
+ }
+
async.waterfall([
- _.bind(this._updateContact, this, contact),
- _.bind(this._iterativeFindNode, this, this.self.nodeID),
- _.bind(function(type, contacts, callback) {
- // FIXME: Do we update buckets or does iterativeFindNode do it?
- var leastBucket = _.min(_.keys(this._buckets));
- var bucketsToRefresh = _.filter(_.keys(this._buckets), function(num) { return num >= leastBucket; });
- var queue = async.queue(_.bind(this._refreshBucket, this), 1);
- _.each(bucketsToRefresh, function(bucketId) {
- // wrapper is required because the each iterator is passed
- // 3 arguments (element, index, list) and queue.push interprets
- // the second argument as a callback
- queue.push(bucketId);
- });
- callback(null);
- }, this)
+ _.bind(this._updateContact, this, contact), // callback is invoked with no arguments
+ _.bind(this._iterativeFindNode, this, this.self.nodeID), // callback is invoked with
+ // type (NODE) and shortlist,
+ // which gets passed on to
+ // refreshBucketsFartherThanClosestKnown
+ _.bind(refreshBucketsFartherThanClosestKnown, this) // callback is invoked with no arguments
], callback);
}

0 comments on commit 8cac969

Please sign in to comment.