@testring/utils
Advanced tools
@@ -17,3 +17,3 @@ "use strict"; | ||
| */ | ||
| constructor(lockLimit = 1) { | ||
| constructor(lockLimit = 0) { | ||
| this.lockLimit = lockLimit; | ||
@@ -29,3 +29,3 @@ this.lockHash = new Map(); | ||
| acquire(id) { | ||
| if (this.lockLength >= this.lockLimit) { | ||
| if (this.lockLimit !== 0 && this.lockLength >= this.lockLimit) { | ||
| return false; | ||
@@ -85,3 +85,10 @@ } | ||
| } | ||
| /** | ||
| * return map if given a string, returns lock amount for that id else returns total amount for all ids in sum | ||
| * | ||
| */ | ||
| getIds() { | ||
| return new Map(this.lockHash); | ||
| } | ||
| } | ||
| exports.MultiLock = MultiLock; |
+2
-2
| { | ||
| "name": "@testring/utils", | ||
| "version": "0.5.32", | ||
| "version": "0.5.33", | ||
| "main": "./dist/index.js", | ||
@@ -13,3 +13,3 @@ "types": "./src/index.ts", | ||
| "dependencies": { | ||
| "@testring/types": "0.5.32", | ||
| "@testring/types": "0.5.33", | ||
| "@types/bytes": "3.1.0", | ||
@@ -16,0 +16,0 @@ "@types/nanoid": "2.1.0", |
| const { nanoid } = require('nanoid'); | ||
| export function generateUniqId(size?: number) { | ||
| export function generateUniqId(size?: number): string { | ||
| return nanoid(size); | ||
| } |
+27
-20
@@ -9,3 +9,3 @@ | ||
| * all locks can be cleared for specified id | ||
| */ | ||
| */ | ||
| export class MultiLock { | ||
@@ -20,12 +20,12 @@ | ||
| */ | ||
| constructor(public lockLimit: number = 1) {} | ||
| /** | ||
| * try to one acquire lock - if lock acquired returns true otherwise false | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| constructor(public lockLimit: number = 0) { } | ||
| /** | ||
| * try to one acquire lock - if lock acquired returns true otherwise false | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| acquire(id: string): boolean { | ||
| if (this.lockLength >= this.lockLimit) { | ||
| if (this.lockLimit !== 0 && this.lockLength >= this.lockLimit) { | ||
| return false; | ||
@@ -51,8 +51,8 @@ } | ||
| } else { | ||
| this.lockHash.set(id, val-1); | ||
| this.lockHash.set(id, val - 1); | ||
| } | ||
| this.lockLength -= 1; | ||
| return true; | ||
| } | ||
| } | ||
| /** | ||
@@ -63,15 +63,15 @@ * unlocks all locks for given Id | ||
| */ | ||
| clean(id: string | void) { | ||
| clean(id: string | void) { | ||
| if (id) { | ||
| const count = this.lockHash.get(id) || 0; | ||
| const count = this.lockHash.get(id) || 0; | ||
| this.lockHash.delete(id); | ||
| this.lockLength -= count; | ||
| return; | ||
| } | ||
| this.lockHash.forEach((_, key)=>{ | ||
| return; | ||
| } | ||
| this.lockHash.forEach((_, key) => { | ||
| this.lockHash.delete(key); | ||
| }); | ||
| this.lockLength = 0; | ||
| } | ||
| } | ||
| /** | ||
@@ -88,2 +88,9 @@ * if given a string, returns lock amount for that id else returns total amount for all ids in sum | ||
| } | ||
| /** | ||
| * return map if given a string, returns lock amount for that id else returns total amount for all ids in sum | ||
| * | ||
| */ | ||
| getIds() { | ||
| return new Map<string, number>(this.lockHash); | ||
| } | ||
| } |
26621
1.65%786
1.81%+ Added
- Removed
Updated