From 66c56df7dc5918cecb1ea98ce27bd3bc7bbaa7f3 Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Mon, 18 Aug 2014 12:22:41 -0700 Subject: [PATCH] Fix requestOffers not returning an EventEmitter --- src/js/ripple/orderbook.js | 59 ++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/js/ripple/orderbook.js b/src/js/ripple/orderbook.js index 16c9cd92..9756656b 100644 --- a/src/js/ripple/orderbook.js +++ b/src/js/ripple/orderbook.js @@ -153,8 +153,20 @@ OrderBook.prototype.subscribe = function() { log.info('subscribing', this._key); } - this.requestOffers().once('success', function() { - self.subscribeTransactions(); + var steps = [ + function(callback) { + self.requestTransferRate(callback); + }, + function(callback) { + self.requestOffers(callback); + }, + function(callback) { + self.subscribeTransactions(callback); + } + ]; + + async.series(steps, function(err) { + //XXX What now? }); }; @@ -646,25 +658,15 @@ OrderBook.prototype.updateTransferRate = function(message) { * Request orderbook entries from server */ -OrderBook.prototype.requestOffers = function() { +OrderBook.prototype.requestOffers = function(callback) { var self = this; - if (!this._shouldSubscribe) { - return; + if (typeof callback !== 'function') { + callback = function(){}; } - if (!this._currencyGets.is_native() && !this._issuerTransferRate) { - // Defer until transfer rate is requested - if (this._remote.trace) { - log.info('waiting for transfer rate'); - } - - this.once('transfer_rate', function() { - self.requestOffers(); - }); - - this.requestTransferRate(); - return; + if (!this._shouldSubscribe) { + return callback(new Error('Should not request offers')); } if (this._remote.trace) { @@ -674,7 +676,7 @@ OrderBook.prototype.requestOffers = function() { function handleOffers(res) { if (!Array.isArray(res.offers)) { // XXX What now? - return; + return callback(new Error('Invalid response')); } if (self._remote.trace) { @@ -700,6 +702,8 @@ OrderBook.prototype.requestOffers = function() { self._synchronized = true; self.emit('model', self._offers); + + callback(null, self._offers); }; function handleError(err) { @@ -707,6 +711,8 @@ OrderBook.prototype.requestOffers = function() { if (self._remote.trace) { log.info('failed to request offers', self._key, err); } + + callback(err); }; var request = this._remote.requestBookOffers(this.toJSON()); @@ -721,28 +727,37 @@ OrderBook.prototype.requestOffers = function() { * Subscribe to transactions stream */ -OrderBook.prototype.subscribeTransactions = function() { +OrderBook.prototype.subscribeTransactions = function(callback) { var self = this; + if (typeof callback !== 'function') { + callback = function(){}; + } + if (!this._shouldSubscribe) { - return; + return callback('Should not subscribe'); } if (this._remote.trace) { log.info('subscribing to transactions'); } - function handleSubscribed() { - self._subscribed = true; + function handleSubscribed(res) { if (self._remote.trace) { log.info('subscribed to transactions'); } + + self._subscribed = true; + + callback(null, res); }; function handleError(err) { if (self._remote.trace) { log.info('failed to subscribe to transactions', self._key, err); } + + callback(err); }; var request = this._remote.requestSubscribe();