add WalletLock class
This commit is contained in:
parent
c804083f89
commit
d8e0d50dce
4 changed files with 151 additions and 3 deletions
47
js/models/core/WalletLock.js
Normal file
47
js/models/core/WalletLock.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
'use strict';
|
||||
|
||||
var preconditions = require('preconditions').singleton();
|
||||
|
||||
function WalletLock(storage, walletId, timeoutMin) {
|
||||
preconditions.checkArgument(storage);
|
||||
preconditions.checkArgument(walletId);
|
||||
|
||||
this.sessionId = storage.getSessionId();
|
||||
this.storage = storage;
|
||||
this.timeoutMin = timeoutMin || 5;
|
||||
this.key = WalletLock._keyFor(walletId);
|
||||
this.keepAlive();
|
||||
}
|
||||
WalletLock._keyFor = function(walletId) {
|
||||
return 'lock' + '::' + walletId;
|
||||
};
|
||||
|
||||
WalletLock.prototype._isLockedByOther = function() {
|
||||
var wl = this.storage.getGlobal(this.key);
|
||||
|
||||
if (!wl || wl.expireTs < Date.now() || wl.sessionId === this.sessionId)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype.keepAlive = function() {
|
||||
preconditions.checkState(this.sessionId);
|
||||
|
||||
if (this._isLockedByOther())
|
||||
throw new Error('Could not adquire lock');
|
||||
|
||||
this.storage.setGlobal(this.key, {
|
||||
sessionId: this.sessionId,
|
||||
expireTs: Date.now() + this.timeoutMin * 60 * 1000,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
WalletLock.prototype.release = function() {
|
||||
this.storage.removeGlobal(this.key);
|
||||
};
|
||||
|
||||
|
||||
module.exports = WalletLock;
|
||||
Loading…
Add table
Add a link
Reference in a new issue