fix async getName calls

This commit is contained in:
Matias Alejo Garcia 2014-09-19 15:00:38 -03:00
commit 4b56e06472
3 changed files with 48 additions and 14 deletions

View file

@ -53,8 +53,8 @@ var defaultConfig = {
verbose: 1,
plugins: {
LocalStorage: true,
//GoogleDrive: true,
//LocalStorage: true,
GoogleDrive: true,
},
GoogleDrive: {

View file

@ -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);
}
})
})();
}
});
};

View file

@ -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() {