time-queues 1.1.2 → 1.2.0
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.
- package/README.md +2 -0
- package/package.json +3 -3
- package/src/Counter.d.ts +63 -0
- package/src/Counter.js +83 -0
package/README.md
CHANGED
|
@@ -39,6 +39,7 @@ The full documentation is available in the project's [wiki](https://github.com/u
|
|
|
39
39
|
- [Scheduler](https://github.com/uhop/time-queues/wiki/Scheduler): Time-based task scheduling
|
|
40
40
|
- [Retainer](https://github.com/uhop/time-queues/wiki/Retainer): Manage resource lifecycle
|
|
41
41
|
- [Throttler](https://github.com/uhop/time-queues/wiki/Throttler): Control execution rate based on keys
|
|
42
|
+
- [Counter](https://github.com/uhop/time-queues/wiki/Counter): Track the number of pending tasks asynchronously
|
|
42
43
|
|
|
43
44
|
### Browser-Specific Components
|
|
44
45
|
|
|
@@ -61,6 +62,7 @@ This project is licensed under the BSD-3-Clause License.
|
|
|
61
62
|
|
|
62
63
|
## Release History
|
|
63
64
|
|
|
65
|
+
* 1.2.0 *Added `Counter`. Updated dev dependencies.*
|
|
64
66
|
* 1.1.2 *Updated dev dependencies. No need to upgrade.*
|
|
65
67
|
* 1.1.1 *Updates to TS typings.*
|
|
66
68
|
* 1.1.0 *Added `Throttler`, `Retainer`, promise-based convenience time methods.*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "time-queues",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Time queues to organize multitasking and scheduled tasks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
@@ -56,10 +56,10 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@types/node": "^22.
|
|
59
|
+
"@types/node": "^22.14.1",
|
|
60
60
|
"tape-six": "^1.1.1",
|
|
61
61
|
"tape-six-proc": "^1.0.1",
|
|
62
|
-
"typescript": "^5.8.
|
|
62
|
+
"typescript": "^5.8.3"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"list-toolkit": "^2.2.1"
|
package/src/Counter.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A counter that can be used to track the number of pending tasks.
|
|
3
|
+
*/
|
|
4
|
+
export declare class Counter {
|
|
5
|
+
/**
|
|
6
|
+
* The current count.
|
|
7
|
+
*/
|
|
8
|
+
count: number;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new counter.
|
|
12
|
+
* @param initial The initial count. Default: 0
|
|
13
|
+
*/
|
|
14
|
+
constructor(initial?: number);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Gets the current value of the counter.
|
|
18
|
+
* @returns The current value of the counter.
|
|
19
|
+
*/
|
|
20
|
+
get value(): number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sets the counter to a specific value.
|
|
24
|
+
* @param value The new value for the counter.
|
|
25
|
+
*/
|
|
26
|
+
set value(value: number): void;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Increments the counter.
|
|
30
|
+
*/
|
|
31
|
+
increment(): void;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Decrements the counter.
|
|
35
|
+
*/
|
|
36
|
+
decrement(): void;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Advances the counter by a given amount.
|
|
40
|
+
* @param amount The amount to advance. Default: 1
|
|
41
|
+
*/
|
|
42
|
+
advance(amount?: number): void;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Waits for the counter to reach zero. If the counter is already zero, the promise is resolved immediately.
|
|
46
|
+
* @returns A promise that resolves when the counter reaches zero.
|
|
47
|
+
*/
|
|
48
|
+
waitForZero(): Promise<number>;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Waits for the counter to reach a specific value. If the counter is already at the desired value, the promise is resolved immediately.
|
|
52
|
+
* @param fn A function that returns `true` when the counter reaches the desired value.
|
|
53
|
+
* @returns A promise that resolves when the counter reaches the desired value.
|
|
54
|
+
*/
|
|
55
|
+
waitFor(fn: (count: number) => boolean): Promise<number>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Clears all waiters.
|
|
59
|
+
*/
|
|
60
|
+
clearWaiters(): void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default Counter;
|
package/src/Counter.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// @ts-self-types="./Counter.d.ts"
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
export class Counter {
|
|
6
|
+
constructor(initial = 0) {
|
|
7
|
+
this.count = initial;
|
|
8
|
+
this.zeroWaiters = [];
|
|
9
|
+
this.functionWaiters = new Set();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get value() {
|
|
13
|
+
return this.count;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
set value(value) {
|
|
17
|
+
this.count = value;
|
|
18
|
+
this.notify();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
increment() {
|
|
22
|
+
++this.count;
|
|
23
|
+
this.notify();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
decrement() {
|
|
27
|
+
--this.count;
|
|
28
|
+
this.notify();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
advance(amount = 1) {
|
|
32
|
+
this.count += amount;
|
|
33
|
+
this.notify();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
waitForZero() {
|
|
37
|
+
if (this.count === 0) return Promise.resolve(0);
|
|
38
|
+
return new Promise(resolve => this.zeroWaiters.push(resolve));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
waitFor(fn) {
|
|
42
|
+
if (fn(this.count)) return Promise.resolve(this.count);
|
|
43
|
+
return new Promise(resolve => this.functionWaiters.add({fn, resolve}));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
clearWaiters() {
|
|
47
|
+
if (this.zeroWaiters.length > 0) {
|
|
48
|
+
for (const resolve of this.zeroWaiters) {
|
|
49
|
+
resolve(NaN);
|
|
50
|
+
}
|
|
51
|
+
this.zeroWaiters = [];
|
|
52
|
+
}
|
|
53
|
+
if (this.functionWaiters.size > 0) {
|
|
54
|
+
for (const {resolve} of this.functionWaiters) {
|
|
55
|
+
resolve(NaN);
|
|
56
|
+
}
|
|
57
|
+
this.functionWaiters.clear();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
notify() {
|
|
62
|
+
if (this.count === 0 && this.zeroWaiters.length > 0) {
|
|
63
|
+
for (const resolve of this.zeroWaiters) {
|
|
64
|
+
resolve(0);
|
|
65
|
+
}
|
|
66
|
+
this.zeroWaiters = [];
|
|
67
|
+
}
|
|
68
|
+
if (this.functionWaiters.size > 0) {
|
|
69
|
+
const ready = [];
|
|
70
|
+
for (const waiter of this.functionWaiters) {
|
|
71
|
+
if (waiter.fn(this.count)) ready.push(waiter);
|
|
72
|
+
}
|
|
73
|
+
if (ready.length > 0) {
|
|
74
|
+
for (const waiter of ready) {
|
|
75
|
+
waiter.resolve(this.count);
|
|
76
|
+
this.functionWaiters.delete(waiter);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export default Counter;
|