Fix orderbook reset on reconnect

This commit is contained in:
wltsmrz
2014-09-17 13:42:24 -07:00
parent 1599eb9629
commit 2cd434e861
2 changed files with 20 additions and 6 deletions

View File

@@ -78,19 +78,20 @@ function OrderBook(remote, getsC, getsI, paysC, paysI, key) {
});
this.on('unsubscribe', function() {
self._ownerFunds = { };
self.resetCache();
self._remote.removeListener('transaction', updateFundedAmounts);
self._remote.removeListener('transaction', updateTransferRate);
});
this._remote.on('prepare_subscribe', function() {
this._remote.once('prepare_subscribe', function() {
self.subscribe();
});
this._remote.on('disconnect', function() {
self._ownerFunds = { };
self._offerCounts = { };
self._synchronized = false;
self.resetCache();
self._remote.once('prepare_subscribe', function() {
self.subscribe();
});
});
function updateFundedAmounts(message) {
@@ -195,6 +196,16 @@ OrderBook.prototype.unsubscribe = function() {
this.emit('unsubscribe');
};
/**
* Reset cached owner funds, offer counts
*/
OrderBook.prototype.resetCache = function() {
this._ownerFunds = { };
this._offerCounts = { };
this._synchronized = false;
};
/**
* Check that the funds for offer owner have been cached
*
@@ -695,6 +706,9 @@ OrderBook.prototype.requestOffers = function(callback) {
log.info('requested offers', self._key, 'offers: ' + res.offers.length);
}
// Reset offers
self._offers = [ ];
for (var i=0, l=res.offers.length; i<l; i++) {
var offer = res.offers[i];
var fundedAmount;

View File

@@ -1546,7 +1546,7 @@ describe('OrderBook', function() {
]
book.once('model', function(model) {
book.on('model', function(model) {
assert.deepEqual(model, expected);
assert.strictEqual(book._synchronized, true);
done();