Skip to content

Commit

Permalink
Merge pull request #9 from irothschild/pre-bind-methods
Browse files Browse the repository at this point in the history
Prebind the _handleSqsResponse and _processMessage methods to use as callbacks.
  • Loading branch information
mrwillihog committed Sep 14, 2015
2 parents 750479c + 12e4c4e commit 831af05
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ function Consumer(options) {
this.sqs = options.sqs || new AWS.SQS({
region: options.region || 'eu-west-1'
});

this._handleSqsResponseBound = this._handleSqsResponse.bind(this);
this._processMessageBound = this._processMessage.bind(this);
}

util.inherits(Consumer, EventEmitter);
Expand Down Expand Up @@ -83,7 +86,7 @@ Consumer.prototype._poll = function () {

if (!this.stopped) {
debug('Polling for messages');
this.sqs.receiveMessage(receiveParams, this._handleSqsResponse.bind(this));
this.sqs.receiveMessage(receiveParams, this._handleSqsResponseBound);
}
};

Expand All @@ -96,7 +99,7 @@ Consumer.prototype._handleSqsResponse = function (err, response) {
debug(response);

if (response && response.Messages && response.Messages.length > 0) {
async.each(response.Messages, this._processMessage.bind(this), function () {
async.each(response.Messages, this._processMessageBound, function () {
// start polling again once all of the messages have been processed
consumer._poll();
});
Expand Down

0 comments on commit 831af05

Please sign in to comment.