@nmtjs/core 0.14.4 → 0.15.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,105 +0,0 @@
1
- export class PoolError extends Error {
2
- }
3
- // Fixed pool from https://github.com/metarhia/metautil
4
- export class Pool {
5
- options;
6
- #items = [];
7
- #free = [];
8
- #queue = [];
9
- #current = 0;
10
- #size = 0;
11
- #available = 0;
12
- constructor(options = {}) {
13
- this.options = options;
14
- }
15
- add(item) {
16
- if (this.#items.includes(item))
17
- throw new PoolError('Item already exists');
18
- this.#size++;
19
- this.#available++;
20
- this.#items.push(item);
21
- this.#free.push(true);
22
- }
23
- remove(item) {
24
- if (this.#size === 0)
25
- throw new PoolError('Pool is empty');
26
- const index = this.#items.indexOf(item);
27
- if (index < 0)
28
- throw new PoolError('Item is not in the pool');
29
- const isCaptured = this.isFree(item);
30
- if (isCaptured)
31
- this.#available--;
32
- this.#size--;
33
- this.#current--;
34
- this.#items.splice(index, 1);
35
- this.#free.splice(index, 1);
36
- }
37
- capture(timeout = this.options.timeout) {
38
- return this.next(true, timeout);
39
- }
40
- async next(exclusive = false, timeout = this.options.timeout) {
41
- if (this.#size === 0)
42
- throw new PoolError('Pool is empty');
43
- if (this.#available === 0) {
44
- return new Promise((resolve, reject) => {
45
- const waiting = {
46
- resolve: (item) => {
47
- if (exclusive)
48
- this.#capture(item);
49
- resolve(item);
50
- },
51
- timer: undefined,
52
- };
53
- if (timeout) {
54
- waiting.timer = setTimeout(() => {
55
- waiting.resolve = undefined;
56
- this.#queue.shift();
57
- reject(new PoolError('Next item timeout'));
58
- }, timeout);
59
- }
60
- this.#queue.push(waiting);
61
- });
62
- }
63
- let item;
64
- let free = false;
65
- do {
66
- item = this.#items[this.#current];
67
- free = this.#free[this.#current];
68
- this.#current++;
69
- if (this.#current >= this.#size)
70
- this.#current = 0;
71
- } while (typeof item === 'undefined' || !free);
72
- if (exclusive)
73
- this.#capture(item);
74
- return item;
75
- }
76
- release(item) {
77
- const index = this.#items.indexOf(item);
78
- if (index < 0)
79
- throw new PoolError('Unexpected item');
80
- if (this.#free[index])
81
- throw new PoolError('Unable to release not captured item');
82
- this.#free[index] = true;
83
- this.#available++;
84
- if (this.#queue.length > 0) {
85
- const { resolve, timer } = this.#queue.shift();
86
- clearTimeout(timer);
87
- if (resolve)
88
- setTimeout(resolve, 0, item);
89
- }
90
- }
91
- isFree(item) {
92
- const index = this.#items.indexOf(item);
93
- if (index < 0)
94
- return false;
95
- return this.#free[index];
96
- }
97
- get items() {
98
- return [...this.#items];
99
- }
100
- #capture(item) {
101
- const index = this.#items.indexOf(item);
102
- this.#free[index] = false;
103
- this.#available--;
104
- }
105
- }
@@ -1,13 +0,0 @@
1
- export declare class SemaphoreError extends Error {
2
- }
3
- export declare class Semaphore {
4
- private readonly size;
5
- private readonly timeout;
6
- private counter;
7
- private readonly queue;
8
- constructor(concurrency: number, size?: number, timeout?: number);
9
- enter(): Promise<void>;
10
- leave(): void;
11
- get isEmpty(): boolean;
12
- get queueLength(): number;
13
- }
@@ -1,55 +0,0 @@
1
- export class SemaphoreError extends Error {
2
- }
3
- // Semaphore from https://github.com/metarhia/metautil
4
- export class Semaphore {
5
- size;
6
- timeout;
7
- counter;
8
- queue = [];
9
- constructor(concurrency, size = 0, timeout = 0) {
10
- this.size = size;
11
- this.timeout = timeout;
12
- this.counter = concurrency;
13
- }
14
- enter() {
15
- if (this.counter > 0) {
16
- this.counter--;
17
- return Promise.resolve();
18
- }
19
- else if (this.queue.length >= this.size) {
20
- return Promise.reject(new SemaphoreError('Queue is full'));
21
- }
22
- else {
23
- return new Promise((resolve, reject) => {
24
- const waiting = { resolve };
25
- waiting.timer = setTimeout(() => {
26
- waiting.resolve = undefined;
27
- this.queue.shift();
28
- reject(new SemaphoreError('Timeout'));
29
- }, this.timeout);
30
- this.queue.push(waiting);
31
- });
32
- }
33
- }
34
- leave() {
35
- if (this.queue.length === 0) {
36
- this.counter++;
37
- }
38
- else {
39
- const item = this.queue.shift();
40
- if (item) {
41
- const { resolve, timer } = item;
42
- if (timer)
43
- clearTimeout(timer);
44
- if (resolve)
45
- setTimeout(resolve, 0);
46
- }
47
- }
48
- }
49
- get isEmpty() {
50
- return this.queue.length === 0;
51
- }
52
- get queueLength() {
53
- return this.queue.length;
54
- }
55
- }