@code-essentials/utils 1.0.13 → 1.1.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/dist/async-variable.d.ts +2 -1
- package/dist/async-variable.js +9 -1
- package/dist/barrier.d.ts +15 -0
- package/dist/barrier.js +33 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +7 -5
package/dist/async-variable.d.ts
CHANGED
|
@@ -12,7 +12,8 @@ export declare class AsyncVariable<T> implements PromiseLike<T> {
|
|
|
12
12
|
set(value: T, throw_if_set?: boolean): Promise<void>;
|
|
13
13
|
reject(error: unknown, throw_if_set?: boolean): Promise<void>;
|
|
14
14
|
static performCallback<R = void>(fn: (cb: (err?: unknown, res?: R) => void) => void): AsyncVariable<R>;
|
|
15
|
-
|
|
15
|
+
writeResult(task: PromiseLike<T>): Promise<void>;
|
|
16
|
+
perform(fn: () => PromiseLike<T>): this;
|
|
16
17
|
timeout(milliseconds: number): this;
|
|
17
18
|
static perform<T>(fn: () => Promise<T>): AsyncVariable<T>;
|
|
18
19
|
static wait(milliseconds: number): AsyncVariable<void>;
|
package/dist/async-variable.js
CHANGED
|
@@ -81,8 +81,16 @@ export class AsyncVariable {
|
|
|
81
81
|
});
|
|
82
82
|
return av;
|
|
83
83
|
}
|
|
84
|
+
async writeResult(task) {
|
|
85
|
+
try {
|
|
86
|
+
await this.set(await task);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
await this.reject(error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
84
92
|
perform(fn) {
|
|
85
|
-
|
|
93
|
+
this.writeResult(fn());
|
|
86
94
|
return this;
|
|
87
95
|
}
|
|
88
96
|
timeout(milliseconds) {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AsyncVariable } from "./async-variable.js";
|
|
2
|
+
interface EnqueuedTask<T> {
|
|
3
|
+
task: PromiseLike<T>;
|
|
4
|
+
completion: AsyncVariable<T>;
|
|
5
|
+
}
|
|
6
|
+
export declare class Barrier<T = any> implements PromiseLike<T[]> {
|
|
7
|
+
#private;
|
|
8
|
+
readonly queue: EnqueuedTask<T>[];
|
|
9
|
+
await(...asyncTasks: PromiseLike<T>[]): void;
|
|
10
|
+
run(...asyncTasks: PromiseLike<T>[]): void;
|
|
11
|
+
clear(): void;
|
|
12
|
+
complete(): Promise<this>;
|
|
13
|
+
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
|
+
export {};
|
package/dist/barrier.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AsyncVariable } from "./async-variable.js";
|
|
2
|
+
export class Barrier {
|
|
3
|
+
queue = [];
|
|
4
|
+
await(...asyncTasks) {
|
|
5
|
+
this.run(...asyncTasks);
|
|
6
|
+
}
|
|
7
|
+
async #run(task) {
|
|
8
|
+
const completion = new AsyncVariable();
|
|
9
|
+
this.queue.push({ task, completion });
|
|
10
|
+
completion.writeResult(task);
|
|
11
|
+
}
|
|
12
|
+
run(...asyncTasks) {
|
|
13
|
+
for (const task of asyncTasks)
|
|
14
|
+
this.#run(task);
|
|
15
|
+
}
|
|
16
|
+
clear() {
|
|
17
|
+
this.queue.splice(0, this.queue.length);
|
|
18
|
+
}
|
|
19
|
+
async complete() {
|
|
20
|
+
const result = await this;
|
|
21
|
+
this.clear();
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
async then(onfulfilled, onrejected) {
|
|
25
|
+
try {
|
|
26
|
+
const values = await Promise.all(this.queue.map(({ completion }) => completion));
|
|
27
|
+
return await onfulfilled?.(values);
|
|
28
|
+
}
|
|
29
|
+
catch (reason) {
|
|
30
|
+
return await onrejected?.(reason);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@code-essentials/utils",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
],
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@ava/typescript": "^6.0.0",
|
|
13
|
-
"@code-essentials/ava-typescript": "
|
|
14
|
-
"@code-essentials/tsconfig": "
|
|
13
|
+
"@code-essentials/ava-typescript": "^1.1.1",
|
|
14
|
+
"@code-essentials/tsconfig": "^1.2.1",
|
|
15
15
|
"@types/node": "^24.10.1",
|
|
16
16
|
"ava": "^6.4.1",
|
|
17
17
|
"typescript": "^5.9.3"
|
|
@@ -30,9 +30,11 @@
|
|
|
30
30
|
"clean": "rm -rf dist",
|
|
31
31
|
"prebuild": "pnpm run clean",
|
|
32
32
|
"prebuild:debug": "pnpm run clean",
|
|
33
|
-
"build": "tsc
|
|
33
|
+
"build": "tsc -p tsconfig.prod.json",
|
|
34
34
|
"build:debug": "tsc -p tsconfig.debug.json",
|
|
35
35
|
"pretest": "pnpm run build:debug",
|
|
36
|
-
"test": "
|
|
36
|
+
"test": "pnpm run test:run",
|
|
37
|
+
"pretest:run": "",
|
|
38
|
+
"test:run": "ava"
|
|
37
39
|
}
|
|
38
40
|
}
|