@altano/tiny-async-pool 1.0.3 → 3.0.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 +9 -6
- package/dist/doWork.d.ts +17 -2
- package/dist/doWork.js +12 -0
- package/dist/doWorkAndYield.d.ts +1 -2
- package/dist/doWorkAndYield.js +25 -0
- package/dist/index.js +2 -33
- package/package.json +18 -19
- /package/{LICENSE-MIT → LICENSE} +0 -0
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# tiny-async-pool
|
|
2
2
|
|
|
3
|
+
This is an ESM + TypeScript fork of [tiny-async-pool](https://www.npmjs.com/package/tiny-async-pool) by Rafael Xavier de Souza, with some added functionality.
|
|
4
|
+
|
|
3
5
|
## Why?
|
|
4
6
|
|
|
5
7
|
The goal of this library is to use the native async iterator (ES9), native async functions, and native Promise to implement concurrent/batched work.
|
|
@@ -69,17 +71,18 @@ Iterator function that takes two arguments: the value of each iteration and the
|
|
|
69
71
|
|
|
70
72
|
### `doWork(concurrency, iterable, iteratorFn)`
|
|
71
73
|
|
|
72
|
-
|
|
74
|
+
Like `doWorkAndYield(...)` but with a simpler API. Useful when you don't care about the return value of `iteratorFn`, such as a worker pool where you do batch file proccessing and the workers produce a side effect instead of returning a value.
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
const timeout = (ms) => new Promise((resolve) => setTimeout(() => resolve(ms), ms));
|
|
76
|
+
For example, a batch file eraser is as simple as:
|
|
76
77
|
|
|
77
|
-
|
|
78
|
+
```js
|
|
79
|
+
const eraseFile = (path) => fs.unlink(path);
|
|
80
|
+
const eraseFiles = async (files) => doWork(4, files, eraseFile);
|
|
78
81
|
```
|
|
79
82
|
|
|
80
|
-
|
|
83
|
+
It will be enforced that `iteratorFn` returns `Promise<void>` when using TypeScript.
|
|
81
84
|
|
|
82
|
-
|
|
85
|
+
## License
|
|
83
86
|
|
|
84
87
|
MIT © [Rafael Xavier de Souza](http://rafael.xavier.blog.br)
|
|
85
88
|
MIT © [Alan Norbauer](https://alan.norbauer.com)
|
package/dist/doWork.d.ts
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type IterableItem } from "./doWorkAndYield";
|
|
2
2
|
/**
|
|
3
3
|
* Process items from `iterable` in batches.
|
|
4
4
|
*/
|
|
5
|
-
export declare function doWork<TIn,
|
|
5
|
+
export declare function doWork<TIn, TIterable extends Iterable<TIn>>(
|
|
6
|
+
/**
|
|
7
|
+
* The size of the batch of work, or, how many times `iteratorFn` will be
|
|
8
|
+
* called in parallel.
|
|
9
|
+
*/
|
|
10
|
+
concurrentCount: number,
|
|
11
|
+
/**
|
|
12
|
+
* An iterable that contains the items that should be passed to `iteratorFn`.
|
|
13
|
+
*/
|
|
14
|
+
iterable: TIterable,
|
|
15
|
+
/**
|
|
16
|
+
* The async callback function that does the work. Will be passed items from
|
|
17
|
+
* `iterable`. Promise returned must not resolve to a value, as it will be
|
|
18
|
+
* ignored.
|
|
19
|
+
*/
|
|
20
|
+
iteratorFn: (item: IterableItem<TIterable>, Iterable: TIterable) => Promise<void>): Promise<void>;
|
|
6
21
|
//# sourceMappingURL=doWork.d.ts.map
|
package/dist/doWork.js
ADDED
package/dist/doWorkAndYield.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type IterableItem<T> = T extends Iterable<infer R> ? R : never;
|
|
1
|
+
export type IterableItem<T> = T extends Iterable<infer R> ? R : never;
|
|
2
2
|
/**
|
|
3
3
|
* Process items from `iterable` in batches and yield the result of each call to
|
|
4
4
|
* `iteratorFn`
|
|
@@ -18,5 +18,4 @@ iterable: TIterable,
|
|
|
18
18
|
* `iterable`.
|
|
19
19
|
*/
|
|
20
20
|
iteratorFn: (item: IterableItem<TIterable>, Iterable: TIterable) => Promise<TOut>): AsyncGenerator<Awaited<TOut>, void, unknown>;
|
|
21
|
-
export {};
|
|
22
21
|
//# sourceMappingURL=doWorkAndYield.d.ts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
async function* doWorkAndYield(concurrentCount, iterable, iteratorFn) {
|
|
2
|
+
const executing = /* @__PURE__ */ new Set();
|
|
3
|
+
async function consume() {
|
|
4
|
+
const [promise, value] = await Promise.race(executing);
|
|
5
|
+
executing.delete(promise);
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
for (const item of iterable) {
|
|
9
|
+
const result = iteratorFn(item, iterable);
|
|
10
|
+
const promise = result.then((value) => [
|
|
11
|
+
promise,
|
|
12
|
+
value
|
|
13
|
+
]);
|
|
14
|
+
executing.add(promise);
|
|
15
|
+
if (executing.size >= concurrentCount) {
|
|
16
|
+
yield await consume();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
while (executing.size) {
|
|
20
|
+
yield await consume();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
doWorkAndYield
|
|
25
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -1,33 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const executing = /* @__PURE__ */ new Set();
|
|
4
|
-
async function consume() {
|
|
5
|
-
const [promise, value] = await Promise.race(executing);
|
|
6
|
-
executing.delete(promise);
|
|
7
|
-
return value;
|
|
8
|
-
}
|
|
9
|
-
for (const item of iterable) {
|
|
10
|
-
const result = iteratorFn(item, iterable);
|
|
11
|
-
const promise = result.then((value) => [
|
|
12
|
-
promise,
|
|
13
|
-
value
|
|
14
|
-
]);
|
|
15
|
-
executing.add(promise);
|
|
16
|
-
if (executing.size >= concurrentCount) {
|
|
17
|
-
yield await consume();
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
while (executing.size) {
|
|
21
|
-
yield await consume();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// src/doWork.ts
|
|
26
|
-
async function doWork(...args) {
|
|
27
|
-
for await (const _result of doWorkAndYield(...args)) {
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
export {
|
|
31
|
-
doWork,
|
|
32
|
-
doWorkAndYield
|
|
33
|
-
};
|
|
1
|
+
export * from "./doWork";
|
|
2
|
+
export * from "./doWorkAndYield";
|
package/package.json
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altano/tiny-async-pool",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Run multiple promise-returning & async functions with limited concurrency using native ES9",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"exports": "./dist/index.js",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"files": [
|
|
10
|
-
"LICENSE-MIT",
|
|
11
10
|
"dist/**/*.js",
|
|
12
|
-
"dist/**/*.d.ts"
|
|
13
|
-
"README.md"
|
|
11
|
+
"dist/**/*.d.ts"
|
|
14
12
|
],
|
|
15
13
|
"repository": {
|
|
16
14
|
"type": "git",
|
|
@@ -39,33 +37,34 @@
|
|
|
39
37
|
"license": "MIT",
|
|
40
38
|
"devDependencies": {
|
|
41
39
|
"@types/node": "^20.11.25",
|
|
42
|
-
"@
|
|
40
|
+
"@vitest/coverage-v8": "^1.4.0",
|
|
43
41
|
"@vitest/ui": "^1.3.1",
|
|
44
42
|
"chai": "^4.4.1",
|
|
45
43
|
"chai-as-promised": "^7.1.1",
|
|
46
44
|
"eslint": "^8.57.0",
|
|
47
45
|
"mocha": "^10.3.0",
|
|
48
46
|
"prettier": "^3.2.5",
|
|
49
|
-
"tsup": "^8.0.2",
|
|
50
47
|
"typescript": "^5.4.2",
|
|
51
|
-
"vitest": "^1.
|
|
48
|
+
"vitest": "^1.4.0",
|
|
52
49
|
"@altano/tsconfig": "0.0.0",
|
|
53
|
-
"eslint-config-altano": "0.0.
|
|
50
|
+
"eslint-config-altano": "0.0.0"
|
|
54
51
|
},
|
|
55
52
|
"publishConfig": {
|
|
56
53
|
"access": "public"
|
|
57
54
|
},
|
|
58
55
|
"scripts": {
|
|
59
|
-
"build": "tsup
|
|
60
|
-
"build:types": "tsc --
|
|
61
|
-
"
|
|
56
|
+
"build": "tsup --config build-config/tsup.config.node.ts",
|
|
57
|
+
"build:types": "tsc --project tsconfig.declarations.json",
|
|
58
|
+
"clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
|
|
59
|
+
"dev": "pnpm run build --watch",
|
|
62
60
|
"format": "prettier --check src",
|
|
63
61
|
"format:fix": "prettier --write src",
|
|
64
|
-
"lint": "eslint src/**/*.ts*",
|
|
65
|
-
"lint:
|
|
66
|
-
"lint:
|
|
67
|
-
"
|
|
68
|
-
"test": "vitest
|
|
69
|
-
"
|
|
62
|
+
"lint": "eslint package.json src/**/*.ts*",
|
|
63
|
+
"lint:fix": "TIMING=1 pnpm lint --fix",
|
|
64
|
+
"lint:timing": "TIMING=1 pnpm lint",
|
|
65
|
+
"test:unit": "vitest --run",
|
|
66
|
+
"test:unit:watch": "vitest",
|
|
67
|
+
"typecheck:src": "tsc --noEmit",
|
|
68
|
+
"typecheck:test": "tsc --noEmit --project ./tests/tsconfig.json"
|
|
70
69
|
}
|
|
71
70
|
}
|
/package/{LICENSE-MIT → LICENSE}
RENAMED
|
File without changes
|