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

@ -65,13 +65,17 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
controllerUtils.setSocketHandlers();
if ($rootScope.wallet) {
$scope.$on('$idleStart', function(a) {
notification.warning('Session will be closed', 'Your session is about to expire due to inactivity');
$scope.$on('$idleWarn', function(a,countdown) {
if (!(countdown%5))
notification.warning('Session will be closed', 'Your session is about to expire due to inactivity in ' + countdown + ' seconds');
});
$scope.$on('$idleTimeout', function() {
$scope.signout();
notification.warning('Session closed', 'Session closed because a long time of inactivity');
});
$scope.$on('$keepalive', function() {
$rootScope.wallet.keepAlive();
});
}
});

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;
};

View file

@ -69,13 +69,15 @@ angular
//Setting HTML5 Location Mode
angular
.module('copayApp')
.config(function($locationProvider, $idleProvider) {
.config(function($locationProvider, $idleProvider, $keepaliveProvider) {
$locationProvider
.html5Mode(false)
.hashPrefix('!');
// IDLE timeout
$idleProvider.idleDuration(config.wallet.idleDurationMin * 60); // in seconds
var timeout = config.wallet.idleDurationMin * 60 || 300;
$idleProvider.idleDuration(timeout); // in seconds
$idleProvider.warningDuration(20); // in seconds
$keepaliveProvider.interval(5); // in seconds
})
.run(function($rootScope, $location, $idle) {
$idle.watch();
@ -83,20 +85,6 @@ angular
if (!util.supports.data) {
$location.path('unsupported');
} else {
// Locked?
if ($rootScope.showLockWarning) {
if ($rootScope.tmp) {
if ($location.path() !== '/warning') {
$location.path('/warning');
}
else {
delete $rootScope['showLockWarning'];
}
}
return;
}
if ((!$rootScope.wallet || !$rootScope.wallet.id) && next.validate) {
$idle.unwatch();
$location.path('/');

View file

@ -26,7 +26,7 @@ angular.module('copayApp.services')
Socket.removeAllListeners();
$rootScope.wallet = $rootScope.tmp = null;
$rootScope.wallet = null;
delete $rootScope['wallet'];
video.close();
@ -72,7 +72,6 @@ angular.module('copayApp.services')
root.setupRootVariables = function() {
uriHandler.register();
$rootScope.unitName = config.unitName;
$rootScope.showLockWarning = false;
$rootScope.txAlertCount = 0;
$rootScope.insightError = 0;
$rootScope.isCollapsed = true;
@ -125,12 +124,6 @@ angular.module('copayApp.services')
};
notification.enableHtml5Mode(); // for chrome: if support, enable it
w.on('locked', function() {
$rootScope.tmp = w;
$rootScope.showLockWarning=true;
$location.path('/warning');
$rootScope.$digest();
});
w.on('badMessage', function(peerId) {
notification.error('Error', 'Received wrong message from peer ' + peerId);
@ -195,6 +188,7 @@ angular.module('copayApp.services')
$rootScope.$digest();
});
w.on('close', root.onErrorDigest);
w.on('locked', root.onErrorDigest.bind(this));
w.netStart();
};