@testring/utils
Advanced tools
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.MultiLock = void 0; | ||
| /** | ||
| * manages multi-count lock for multiple Ids - holds total count of locks & manages its change | ||
| * | ||
| * during init max total lock amount can be set defaulted to 1 | ||
| * | ||
| * on any given id lock can be acquired and released in the future | ||
| * all locks can be cleared for specified id | ||
| */ | ||
| class MultiLock { | ||
| /** | ||
| * | ||
| * @param lockLimit - max amount of locks for all ids | ||
| */ | ||
| constructor(lockLimit = 1) { | ||
| this.lockLimit = lockLimit; | ||
| this.lockHash = new Map(); | ||
| this.lockLength = 0; | ||
| } | ||
| /** | ||
| * try to one acquire lock - if lock acquired returns true otherwise false | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| acquire(id) { | ||
| if (this.lockLength >= this.lockLimit) { | ||
| return false; | ||
| } | ||
| const val = this.lockHash.get(id) || 0; | ||
| this.lockHash.set(id, val + 1); | ||
| this.lockLength += 1; | ||
| return true; | ||
| } | ||
| /** | ||
| * releases one lock for given id | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| release(id) { | ||
| const val = this.lockHash.get(id); | ||
| if (!val) { | ||
| return false; | ||
| } | ||
| if (val === 1) { | ||
| this.lockHash.delete(id); | ||
| } | ||
| else { | ||
| this.lockHash.set(id, val - 1); | ||
| } | ||
| this.lockLength -= 1; | ||
| return true; | ||
| } | ||
| /** | ||
| * unlocks all locks for given Id | ||
| * | ||
| * @param {string|void} id - lockID | ||
| */ | ||
| clean(id) { | ||
| if (id) { | ||
| const count = this.lockHash.get(id) || 0; | ||
| this.lockHash.delete(id); | ||
| this.lockLength -= count; | ||
| return; | ||
| } | ||
| this.lockHash.forEach((_, key) => { | ||
| this.lockHash.delete(key); | ||
| }); | ||
| this.lockLength = 0; | ||
| } | ||
| /** | ||
| * if given a string, returns lock amount for that id else returns total amount for all ids in sum | ||
| * | ||
| * @param {string|void} id - lockID | ||
| */ | ||
| getSize(id) { | ||
| if (id) { | ||
| return this.lockHash.get(id) || 0; | ||
| } | ||
| return this.lockLength; | ||
| } | ||
| } | ||
| exports.MultiLock = MultiLock; |
| /** | ||
| * manages multi-count lock for multiple Ids - holds total count of locks & manages its change | ||
| * | ||
| * during init max total lock amount can be set defaulted to 1 | ||
| * | ||
| * on any given id lock can be acquired and released in the future | ||
| * all locks can be cleared for specified id | ||
| */ | ||
| export class MultiLock { | ||
| private lockHash: Map<string, number> = new Map(); | ||
| private lockLength: number = 0; | ||
| /** | ||
| * | ||
| * @param lockLimit - max amount of locks for all ids | ||
| */ | ||
| constructor(public lockLimit: number = 1) {} | ||
| /** | ||
| * 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) { | ||
| return false; | ||
| } | ||
| const val = this.lockHash.get(id) || 0; | ||
| this.lockHash.set(id, val + 1); | ||
| this.lockLength += 1; | ||
| return true; | ||
| } | ||
| /** | ||
| * releases one lock for given id | ||
| * | ||
| * @param {string} id - lockID | ||
| */ | ||
| release(id: string): boolean { | ||
| const val = this.lockHash.get(id); | ||
| if (!val) { | ||
| return false; | ||
| } | ||
| if (val === 1) { | ||
| this.lockHash.delete(id); | ||
| } else { | ||
| this.lockHash.set(id, val-1); | ||
| } | ||
| this.lockLength -= 1; | ||
| return true; | ||
| } | ||
| /** | ||
| * unlocks all locks for given Id | ||
| * | ||
| * @param {string|void} id - lockID | ||
| */ | ||
| clean(id: string | void) { | ||
| if (id) { | ||
| const count = this.lockHash.get(id) || 0; | ||
| this.lockHash.delete(id); | ||
| this.lockLength -= count; | ||
| return; | ||
| } | ||
| this.lockHash.forEach((_, key)=>{ | ||
| this.lockHash.delete(key); | ||
| }); | ||
| this.lockLength = 0; | ||
| } | ||
| /** | ||
| * if given a string, returns lock amount for that id else returns total amount for all ids in sum | ||
| * | ||
| * @param {string|void} id - lockID | ||
| */ | ||
| getSize(id: string | void): number { | ||
| if (id) { | ||
| return this.lockHash.get(id) || 0; | ||
| } | ||
| return this.lockLength; | ||
| } | ||
| } |
+2
-0
@@ -17,2 +17,4 @@ "use strict"; | ||
| Object.defineProperty(exports, "Stack", { enumerable: true, get: function () { return stack_1.Stack; } }); | ||
| var multi_lock_1 = require("./multi-lock"); | ||
| Object.defineProperty(exports, "MultiLock", { enumerable: true, get: function () { return multi_lock_1.MultiLock; } }); | ||
| var generate_uniq_id_1 = require("./generate-uniq-id"); | ||
@@ -19,0 +21,0 @@ Object.defineProperty(exports, "generateUniqId", { enumerable: true, get: function () { return generate_uniq_id_1.generateUniqId; } }); |
+10
-0
@@ -22,2 +22,12 @@ "use strict"; | ||
| } | ||
| /** | ||
| * | ||
| * @param {(T, number?)=>boolean} fn - function to filter elements for removal | ||
| * @returns - number of elements removed | ||
| */ | ||
| remove(fn) { | ||
| const len = this.array.length; | ||
| this.array = this.array.filter((item, index) => !fn(item, index)); | ||
| return len - this.array.length; | ||
| } | ||
| getFirstElement(offset = 0) { | ||
@@ -24,0 +34,0 @@ const elementIndex = offset; |
+2
-2
| { | ||
| "name": "@testring/utils", | ||
| "version": "0.5.30", | ||
| "version": "0.5.31", | ||
| "main": "./dist/index.js", | ||
@@ -13,3 +13,3 @@ "types": "./src/index.ts", | ||
| "dependencies": { | ||
| "@testring/types": "0.5.30", | ||
| "@testring/types": "0.5.31", | ||
| "@types/bytes": "3.1.0", | ||
@@ -16,0 +16,0 @@ "@types/nanoid": "2.1.0", |
+1
-0
@@ -17,2 +17,3 @@ export { | ||
| export { Stack } from './stack'; | ||
| export { MultiLock } from './multi-lock'; | ||
@@ -19,0 +20,0 @@ export { generateUniqId } from './generate-uniq-id'; |
+11
-0
@@ -26,2 +26,13 @@ import { IQueue } from '@testring/types'; | ||
| /** | ||
| * | ||
| * @param {(T, number?)=>boolean} fn - function to filter elements for removal | ||
| * @returns - number of elements removed | ||
| */ | ||
| public remove(fn: <T>(T, number?) => boolean): number { | ||
| const len = this.array.length; | ||
| this.array = this.array.filter((item, index)=>!fn(item, index)); | ||
| return len - this.array.length; | ||
| } | ||
| public getFirstElement(offset: number = 0): T | null { | ||
@@ -28,0 +39,0 @@ const elementIndex = offset; |
26190
25.08%23
9.52%772
31.29%+ Added
- Removed
Updated