Fix orderbook unsubscribe

This commit is contained in:
wltsmrz
2014-01-08 15:13:18 -08:00
parent 99351aa2dc
commit 19e29722dc

View File

@@ -36,11 +36,9 @@ function OrderBook(remote, currency_gets, issuer_gets, currency_pays, issuer_pay
function listenerAdded(type, listener) { function listenerAdded(type, listener) {
if (~OrderBook.subscribe_events.indexOf(type)) { if (~OrderBook.subscribe_events.indexOf(type)) {
self._subs += 1;
if (!self._subs && self._remote._connected) { if (!self._subs && self._remote._connected) {
self._subs += 1;
self._subscribe(); self._subscribe();
} else {
self._subs += 1;
} }
} }
}; };
@@ -59,6 +57,8 @@ function OrderBook(remote, currency_gets, issuer_gets, currency_pays, issuer_pay
} }
}; };
this.on('removeListener', listenerRemoved);
// ST: This *should* call _prepareSubscribe. // ST: This *should* call _prepareSubscribe.
this._remote.on('prepare_subscribe', this._subscribe.bind(this)); this._remote.on('prepare_subscribe', this._subscribe.bind(this));
@@ -85,17 +85,21 @@ OrderBook.subscribe_events = ['transaction', 'model', 'trade'];
*/ */
OrderBook.prototype._subscribe = function () { OrderBook.prototype._subscribe = function () {
var self = this; var self = this;
if (self.is_valid() && self._subs) { if (self.is_valid() && self._subs) {
var request = this._remote.request_subscribe(); var request = this._remote.request_subscribe();
request.addBook(self.to_json(), true); request.addBook(self.to_json(), true);
request.once('success', function(res) { request.once('success', function(res) {
self._sync = true; self._sync = true;
self._offers = res.offers; self._offers = res.offers;
self.emit('model', self._offers); self.emit('model', self._offers);
}); });
request.once('error', function(err) { request.once('error', function(err) {
// XXX What now? // XXX What now?
}); });
request.request(); request.request();
} }
}; };