@bitblit/ratchet-aws
Version:
Common tools for use with AWS browser and node
37 lines • 1.09 kB
JavaScript
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
export class MemorySyncLock {
_locks = new Map();
constructor() {
}
async acquireLock(lockKey, expirationSeconds = 30) {
let rval = false;
if (StringRatchet.trimToNull(lockKey)) {
const now = Date.now();
const val = this._locks.get(lockKey);
if (!val || val < now) {
this._locks.set(lockKey, now + expirationSeconds * 1000);
rval = true;
}
}
return rval;
}
async releaseLock(lockKey) {
if (StringRatchet.trimToNull(lockKey)) {
this._locks.delete(lockKey);
}
}
async clearExpiredSyncLocks() {
const toRemove = [];
const now = Date.now();
this._locks.forEach((v, k) => {
if (v < now) {
toRemove.push(k);
}
});
toRemove.forEach((k) => {
this._locks.delete(k);
});
return toRemove.length;
}
}
//# sourceMappingURL=memory-sync-lock.js.map