wallet listing working

This commit is contained in:
Matias Alejo Garcia 2014-09-03 01:25:08 -03:00
commit 6efa4f86de
14 changed files with 538 additions and 240 deletions

View file

@ -10,46 +10,51 @@ function WalletLock(storage, walletId, timeoutMin) {
this.storage = storage;
this.timeoutMin = timeoutMin || 5;
this.key = WalletLock._keyFor(walletId);
this._setLock();
this._setLock(function() {});
}
WalletLock._keyFor = function(walletId) {
return 'lock' + '::' + walletId;
};
WalletLock.prototype._isLockedByOther = function() {
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;
WalletLock.prototype._isLockedByOther = function(cb) {
var self=this;
this.storage.getGlobal(this.key, function(json) {
var wl = json ? JSON.parse(json) : null;
var t = wl ? (Date.now() - wl.expireTs) : false;
// is not locked?
if (!wl || t > 0 || wl.sessionId === self.sessionId)
return cb(false);
// Seconds remainding
return parseInt(-t/1000.);
};
WalletLock.prototype._setLock = function() {
this.storage.setGlobal(this.key, {
sessionId: this.sessionId,
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
// Seconds remainding
return cb(parseInt(-t / 1000.));
});
};
WalletLock.prototype.keepAlive = function() {
preconditions.checkState(this.sessionId);
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._setLock();
WalletLock.prototype._setLock = function(cb) {
this.storage.setGlobal(this.key, {
sessionId: this.sessionId,
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
}, cb);
};
WalletLock.prototype.release = function() {
this.storage.removeGlobal(this.key);
WalletLock.prototype.keepAlive = function(cb) {
preconditions.checkState(this.sessionId);
var self = this;
this._isLockedByOther(function(t) {
if (t)
return cb(new Error('Wallet is already open. Close it to proceed or wait ' + t + ' seconds if you close it already'));
self._setLock(cb);
});
};
WalletLock.prototype.release = function(cb) {
this.storage.removeGlobal(this.key, cb);
};