fix tests and stringify for localstorage

This commit is contained in:
Matias Alejo Garcia 2014-08-15 12:43:43 -04:00
commit 18aadede29
9 changed files with 75 additions and 76 deletions

View file

@ -497,7 +497,12 @@ Wallet.prototype.getRegisteredPeerIds = function() {
};
Wallet.prototype.keepAlive = function() {
this.lock.keepAlive();
try{
this.lock.keepAlive();
} catch(e){
this.log(e);
this.emit('locked',null,'Wallet appears to be openned on other browser instance. Closing this one.' );
}
};
Wallet.prototype.store = function() {

View file

@ -17,20 +17,24 @@ WalletLock._keyFor = function(walletId) {
};
WalletLock.prototype._isLockedByOther = function() {
var wl = this.storage.getGlobal(this.key);
if (!wl || wl.expireTs < Date.now() || wl.sessionId === this.sessionId)
var json = this.storage.getGlobal(this.key);
var wl = json ? JSON.parse(json) : null;
var t = wl ? (Date.now() - wl.expireTs) : false;
// is not locked?
if (!wl || t > 0 || wl.sessionId === this.sessionId)
return false;
return true;
// Seconds remainding
return parseInt(-t/1000.);
};
WalletLock.prototype.keepAlive = function() {
preconditions.checkState(this.sessionId);
if (this._isLockedByOther())
throw new Error('Could not adquire lock');
var t = this._isLockedByOther();
if (t)
throw new Error('Wallet is already open. Close it to proceed or wait '+ t + ' seconds if you close it already' );
this.storage.setGlobal(this.key, {
sessionId: this.sessionId,

View file

@ -101,7 +101,7 @@ Storage.prototype.getSessionId = function() {
var sessionId = this.sessionStorage.getItem('sessionId');
if (!sessionId) {
sessionId = bitcore.SecureRandom.getRandomBuffer(8).toString('hex');
this.sessionStorage.setItem(sessionId, 'sessionId');
this.sessionStorage.setItem('sessionId', sessionId);
}
return sessionId;
};