Comparing version 3.5.12 to 3.5.13
@@ -5,2 +5,8 @@ # Changelog | ||
## [3.5.13][] - 2021-09-20 | ||
- Wait for available (released) item in Pool with waiting timeout | ||
- Pool: prevent to add duplicates and to release not captured items | ||
- Regrouped utilities into modules and tests | ||
## [3.5.12][] - 2021-09-18 | ||
@@ -107,3 +113,4 @@ | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.12....HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.5.13....HEAD | ||
[3.5.13]: https://github.com/metarhia/metautil/compare/v3.5.12...v3.5.13 | ||
[3.5.12]: https://github.com/metarhia/metautil/compare/v3.5.11...v3.5.12 | ||
@@ -110,0 +117,0 @@ [3.5.11]: https://github.com/metarhia/metautil/compare/v3.5.10...v3.5.11 |
'use strict'; | ||
const path = require('path'); | ||
const { EventEmitter } = require('events'); | ||
@@ -295,78 +294,2 @@ const random = (min, max) => { | ||
const createAbortController = () => { | ||
const signal = new EventEmitter(); | ||
const abort = () => { | ||
signal.emit('abort'); | ||
}; | ||
return { abort, signal }; | ||
}; | ||
const timeout = (msec, signal = null) => | ||
new Promise((resolve, reject) => { | ||
const timer = setTimeout(() => { | ||
reject(new Error('Timeout reached')); | ||
}, msec); | ||
if (!signal) return; | ||
signal.on('abort', () => { | ||
clearTimeout(timer); | ||
reject(new Error('Timeout aborted')); | ||
}); | ||
}); | ||
const delay = (msec, signal = null) => | ||
new Promise((resolve, reject) => { | ||
const timer = setTimeout(resolve, msec); | ||
if (!signal) return; | ||
signal.on('abort', () => { | ||
clearTimeout(timer); | ||
reject(new Error('Delay aborted')); | ||
}); | ||
}); | ||
class Pool { | ||
constructor() { | ||
this.items = []; | ||
this.free = []; | ||
this.current = 0; | ||
this.size = 0; | ||
this.available = 0; | ||
} | ||
next() { | ||
if (this.available === 0) return null; | ||
let item = null; | ||
let free = false; | ||
do { | ||
item = this.items[this.current]; | ||
free = this.free[this.current]; | ||
this.current++; | ||
} while (!item && !free); | ||
if (this.current === this.size) this.current = 0; | ||
return item; | ||
} | ||
add(item) { | ||
this.size++; | ||
this.available++; | ||
this.items.push(item); | ||
this.free.push(true); | ||
} | ||
capture() { | ||
const item = this.next(); | ||
if (!item) return null; | ||
const index = this.current - 1; | ||
this.free[index] = false; | ||
this.available--; | ||
return item; | ||
} | ||
release(item) { | ||
const index = this.items.indexOf(item); | ||
if (index < 0) throw new Error('Pool: release unexpected item'); | ||
this.free[index] = true; | ||
this.available++; | ||
} | ||
} | ||
module.exports = { | ||
@@ -397,6 +320,2 @@ random, | ||
parseCookies, | ||
createAbortController, | ||
timeout, | ||
delay, | ||
Pool, | ||
}; |
@@ -90,13 +90,19 @@ import { EventEmitter } from 'events'; | ||
//interface PoolOptions { | ||
// timeout?: number; | ||
//} | ||
export class Pool { | ||
concurrency: number; | ||
constructor(options: { timeout?: number }); | ||
items: Array<object>; | ||
free: Array<object>; | ||
free: Array<boolean>; | ||
queue: Array<object>; | ||
current: number; | ||
size: number; | ||
available: number; | ||
next(): object | null; | ||
timeout: number; | ||
next(): Promise<object | null>; | ||
add(item: object): void; | ||
capture(): object | null; | ||
capture(): Promise<object | null>; | ||
release(item: object): void; | ||
} |
'use strict'; | ||
const utilities = require('./lib/utilities.js'); | ||
const secutity = require('./lib/security.js'); | ||
const crypto = require('./lib/crypto.js'); | ||
const async = require('./lib/async.js'); | ||
const semaphore = require('./lib/semaphore.js'); | ||
const pool = require('./lib/pool.js'); | ||
module.exports = { ...utilities, ...secutity, ...semaphore }; | ||
module.exports = { ...utilities, ...crypto, ...semaphore, ...pool, ...async }; |
{ | ||
"name": "metautil", | ||
"version": "3.5.12", | ||
"version": "3.5.13", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -41,12 +41,12 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^16.9.0", | ||
"@types/node": "^16.9.1", | ||
"eslint": "^7.32.0", | ||
"eslint-config-metarhia": "^7.0.1", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.24.0", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"metatests": "^0.7.2", | ||
"prettier": "^2.4.0", | ||
"typescript": "^4.4.2" | ||
"typescript": "^4.4.3" | ||
} | ||
} |
@@ -25,2 +25,3 @@ # Metarhia utilities | ||
- `fileExt(fileName: string): string` | ||
- `parsePath(relPath: string): Array<string>` | ||
- `between(s: string, prefix: string, suffix: string): string` | ||
@@ -56,3 +57,15 @@ - `isFirstUpper(s: string): boolean` | ||
- `new Semaphore(concurrency: number, size?: number, timeout?: number)` | ||
- Semaphore for limit concurrency accessing limited resource | ||
- `new Semaphore(concurrency: number, size?: number, timeout?: number)` | ||
- `empty: boolean` | ||
- `enter(): Promise<void>` | ||
- `leave(): void` | ||
- Pool with round-robin and exclusive item capture | ||
- `new Pool()` | ||
- `size: number` | ||
- `available: number` | ||
- `next(): object | null` | ||
- `add(item: object): void` | ||
- `capture(): object | null` | ||
- `release(item: object): void` | ||
@@ -59,0 +72,0 @@ ## License & Contributors |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28412
11
649
75