@code-essentials/utils 1.1.0 → 1.1.2
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/dist/barrier.d.ts +3 -2
- package/dist/barrier.js +8 -4
- package/package.json +5 -5
package/dist/barrier.d.ts
CHANGED
|
@@ -3,13 +3,14 @@ interface EnqueuedTask<T> {
|
|
|
3
3
|
task: PromiseLike<T>;
|
|
4
4
|
completion: AsyncVariable<T>;
|
|
5
5
|
}
|
|
6
|
-
export declare class Barrier<T = any> implements PromiseLike<T[]
|
|
6
|
+
export declare class Barrier<T = any> implements PromiseLike<T[]>, AsyncDisposable {
|
|
7
7
|
#private;
|
|
8
8
|
readonly queue: EnqueuedTask<T>[];
|
|
9
9
|
await(...asyncTasks: PromiseLike<T>[]): void;
|
|
10
|
-
run(...
|
|
10
|
+
run(...asyncFunctions: (() => PromiseLike<T>)[]): void;
|
|
11
11
|
clear(): void;
|
|
12
12
|
complete(): Promise<this>;
|
|
13
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
13
14
|
then<TResult1 = T[], TResult2 = never>(onfulfilled?: ((value: T[]) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>;
|
|
14
15
|
}
|
|
15
16
|
export {};
|
package/dist/barrier.js
CHANGED
|
@@ -2,16 +2,17 @@ import { AsyncVariable } from "./async-variable.js";
|
|
|
2
2
|
export class Barrier {
|
|
3
3
|
queue = [];
|
|
4
4
|
await(...asyncTasks) {
|
|
5
|
-
|
|
5
|
+
for (const asyncTask of asyncTasks)
|
|
6
|
+
this.#run(asyncTask);
|
|
6
7
|
}
|
|
7
8
|
async #run(task) {
|
|
8
9
|
const completion = new AsyncVariable();
|
|
9
10
|
this.queue.push({ task, completion });
|
|
10
11
|
completion.writeResult(task);
|
|
11
12
|
}
|
|
12
|
-
run(...
|
|
13
|
-
for (const
|
|
14
|
-
this.#run(
|
|
13
|
+
run(...asyncFunctions) {
|
|
14
|
+
for (const func of asyncFunctions)
|
|
15
|
+
this.#run(func());
|
|
15
16
|
}
|
|
16
17
|
clear() {
|
|
17
18
|
this.queue.splice(0, this.queue.length);
|
|
@@ -21,6 +22,9 @@ export class Barrier {
|
|
|
21
22
|
this.clear();
|
|
22
23
|
return result;
|
|
23
24
|
}
|
|
25
|
+
async [Symbol.asyncDispose]() {
|
|
26
|
+
await this.complete();
|
|
27
|
+
}
|
|
24
28
|
async then(onfulfilled, onrejected) {
|
|
25
29
|
try {
|
|
26
30
|
const values = await Promise.all(this.queue.map(({ completion }) => completion));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@code-essentials/utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -28,12 +28,12 @@
|
|
|
28
28
|
"license": "MIT",
|
|
29
29
|
"scripts": {
|
|
30
30
|
"clean": "rm -rf dist",
|
|
31
|
-
"prebuild": "
|
|
32
|
-
"prebuild:debug": "
|
|
31
|
+
"prebuild": "npm run clean",
|
|
32
|
+
"prebuild:debug": "npm run clean",
|
|
33
33
|
"build": "tsc -p tsconfig.prod.json",
|
|
34
34
|
"build:debug": "tsc -p tsconfig.debug.json",
|
|
35
|
-
"pretest": "
|
|
36
|
-
"test": "
|
|
35
|
+
"pretest": "npm run build:debug",
|
|
36
|
+
"test": "npm run test:run",
|
|
37
37
|
"pretest:run": "",
|
|
38
38
|
"test:run": "ava"
|
|
39
39
|
}
|