From f5fa0bfda276b5bc6fcc7cdd0d048ffba4316963 Mon Sep 17 00:00:00 2001 From: jkingyens Date: Thu, 18 Sep 2014 00:40:35 -0700 Subject: [PATCH] use array instead of event emitter for queuing requests --- lib/common/connection.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/common/connection.js b/lib/common/connection.js index 41470241cba..4a5f631daa2 100644 --- a/lib/common/connection.js +++ b/lib/common/connection.js @@ -97,6 +97,7 @@ function Connection(opts) { opts = opts || {}; + this.bufferedRequests = []; this.credentials = null; this.opts = opts; this.scopes = opts.scopes || []; @@ -132,11 +133,17 @@ Connection.prototype.connect = function() { this.fetchToken(function(err, token) { that.isConnecting = false; if (err) { - that.emit('connected', err); + that.bufferedRequests.forEach(function (cb) { + cb(err); + }); + that.bufferedRequests = []; return; } that.token = token; - that.emit('connected'); + that.bufferedRequests.forEach(function (cb) { + cb(); + }); + that.bufferedRequests = []; }); }; @@ -256,7 +263,7 @@ Connection.prototype.createAuthorizedReq = function(reqOpts, callback) { reqOpts.headers['User-Agent'] = USER_AGENT; } - function onConnected(err) { + var onConnected = function (err) { if (err) { callback(err); return; @@ -264,16 +271,18 @@ Connection.prototype.createAuthorizedReq = function(reqOpts, callback) { callback(null, that.authorizeReq(reqOpts)); } + if (!this.isConnecting) { + this.connect(); + } + if (this.isConnected()) { setImmediate(onConnected); return; + } else { + this.bufferedRequests.push(onConnected); + return; } - this.once('connected', onConnected); - - if (!this.isConnecting) { - this.connect(); - } }; /**