diff --git a/js/models/Identity.js b/js/models/Identity.js index 4e6effb71..bf1985258 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -86,7 +86,9 @@ Identity.create = function(opts, cb) { opts = _.extend({}, opts); var iden = new Identity(opts); - iden.store(_.extend(opts, {failIfExists: true}), function(err) { + iden.store(_.extend(opts, { + failIfExists: true + }), function(err) { if (err) return cb(err); return cb(null, iden); }); diff --git a/js/models/RateService.js b/js/models/RateService.js index 267631419..29fd21971 100644 --- a/js/models/RateService.js +++ b/js/models/RateService.js @@ -20,15 +20,26 @@ var RateService = function(opts) { self._isAvailable = false; self._rates = {}; - self._alternatives = {}; - self.queued = []; + self._alternatives = []; + self._queued = []; self._fetchCurrencies(); -} +}; + +var _instance; +RateService.singleton = function(opts) { + if (!_instance) { + _instance = new RateService(opts); + } + return _instance; +}; + RateService.prototype._fetchCurrencies = function() { var self = this; + log.info('Fetching exchange rates'); + var backoffSeconds = 5; var updateFrequencySeconds = 3600; var rateServiceUrl = 'https://bitpay.com/api/rates'; @@ -43,15 +54,15 @@ RateService.prototype._fetchCurrencies = function() { return; } _.each(body, function(currency) { - self.rates[currency.code] = currency.rate; - self.alternatives.push({ + self._rates[currency.code] = currency.rate; + self._alternatives.push({ name: currency.name, isoCode: currency.code, rate: currency.rate }); }); self._isAvailable = true; - _.each(self.queued, function(callback) { + _.each(self._queued, function(callback) { setTimeout(callback, 1); }); setTimeout(function() { @@ -81,7 +92,7 @@ RateService.prototype.whenAvailable = function(callback) { if (!this.isAvailable()) { setTimeout(callback, 1); } else { - this.queued.push(callback); + this._queued.push(callback); } }; diff --git a/test/PayPro.js b/test/PayPro.js index 969c64dcf..8a2cd58ea 100644 --- a/test/PayPro.js +++ b/test/PayPro.js @@ -83,6 +83,7 @@ describe('PayPro (in Wallet) model', function() { c.network.getHexNonces = sinon.stub(); c.network.send = sinon.stub(); + Wallet._newRateService = sinon.stub().returns(null); return new Wallet(c); } diff --git a/test/Wallet.js b/test/Wallet.js index 4c760977e..4412cae69 100644 --- a/test/Wallet.js +++ b/test/Wallet.js @@ -56,8 +56,8 @@ var addCopayers = function(w) { } }; -describe('Wallet model', function() { +describe('Wallet model', function() { it('should fail to create an instance', function() { (function() { new Wallet(walletConfig) @@ -102,8 +102,6 @@ describe('Wallet model', function() { c.network.peerFromCopayer = sinon.stub().returns('xxxx'); c.network.send = sinon.stub(); - - c.addressBook = { '2NFR2kzH9NUdp8vsXTB4wWQtTtzhpKxsyoJ': { label: 'John', @@ -122,6 +120,8 @@ describe('Wallet model', function() { c.networkName = walletConfig.networkName; c.version = '0.0.1'; + Wallet._newRateService = sinon.stub().returns(null); + return new Wallet(c); }