From 4b56e06472aea840337605b8aab4a02262250112 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 19 Sep 2014 15:00:38 -0300 Subject: [PATCH] fix async getName calls --- config.js | 4 ++-- js/models/Storage.js | 25 ++++++++++++++----------- test/test.Storage.js | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/config.js b/config.js index 811431cd6..d4772c096 100644 --- a/config.js +++ b/config.js @@ -53,8 +53,8 @@ var defaultConfig = { verbose: 1, plugins: { - LocalStorage: true, - //GoogleDrive: true, + //LocalStorage: true, + GoogleDrive: true, }, GoogleDrive: { diff --git a/js/models/Storage.js b/js/models/Storage.js index 6d5be0046..8ccd4cda5 100644 --- a/js/models/Storage.js +++ b/js/models/Storage.js @@ -224,18 +224,21 @@ Storage.prototype.getWallets = function(cb) { return cb([]); for (var ii in ids) { - var id = ids[ii]; - self.getName(id, function(name) { - wallets.push({ - id: id, - name: name, + // Create a closure for async calls. + (function() { + var id = ids[ii]; + self.getName(id, function(name) { + wallets.push({ + id: id, + name: name, + }); + if (++i == l) { + self.wListCache.data = wallets; + self.wListCache.ts = Date.now() + CACHE_DURATION; + return cb(wallets); + } }); - if (++i == l) { - self.wListCache.data = wallets; - self.wListCache.ts = Date.now() + CACHE_DURATION; - return cb(wallets); - } - }) + })(); } }); }; diff --git a/test/test.Storage.js b/test/test.Storage.js index 186a30070..559dae141 100644 --- a/test/test.Storage.js +++ b/test/test.Storage.js @@ -1,5 +1,6 @@ 'use strict'; var chai = chai || require('chai'); +var sinon = require('sinon'); var should = chai.should(); var is_browser = typeof process == 'undefined' || typeof process.versions === 'undefined'; var copay = copay || require('../copay'); @@ -153,6 +154,7 @@ describe('Storage model', function() { s.set('1', "hola", 'juan', function() { s.set('2', "hola", 'juan', function() { s.setName(1, 'hola', function() { + s.getWallets(function(ws) { ws[0].should.deep.equal({ id: '1', @@ -168,7 +170,36 @@ describe('Storage model', function() { }); }); }); - }); describe('#deleteWallet', function() { + it('should retreive wallets from storage (with delay)', function(done) { + s.set('1', "hola", 'juan', function() { + s.set('2', "hola", 'juan', function() { + s.setName(1, 'hola', function() { + + var orig = s.getName.bind(s); + s.getName = function(wid, cb) { + setTimeout(function() { + orig(wid, cb); + },1); + }; + + s.getWallets(function(ws) { + ws[0].should.deep.equal({ + id: '1', + name: 'hola', + }); + ws[1].should.deep.equal({ + id: '2', + name: undefined + }); + done(); + }); + }); + }); + }); + }); + }); + + describe('#deleteWallet', function() { it('should fail to delete a unexisting wallet', function(done) { s.set('1', "hola", 'juan', function() { s.set('2', "hola", 'juan', function() {