@altano/tiny-async-pool 1.0.3 → 2.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 +7 -6
- package/dist/doWork.d.ts +17 -2
- package/dist/doWorkAndYield.d.ts +1 -2
- package/dist/index.js +6 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# tiny-async-pool
|
|
2
2
|
|
|
3
|
+
This is an ESM + TypeScript rewrite of [tiny-async-pool](https://www.npmjs.com/package/tiny-async-pool) by Rafael Xavier de Souza.
|
|
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,16 @@ 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 returns a Promise that resolves when all work resolves (or rejects immediately when any Promise rejects). Useful when you don't care about the return 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. For example, a batch file eraser:
|
|
73
75
|
|
|
74
76
|
```js
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
await doWork(2, [1000, 5000, 3000, 2000], timeout);
|
|
77
|
+
const eraseFile = (path) => fs.unlink(path);
|
|
78
|
+
const eraseFiles = async (files) => doWork(4, files, eraseFile);
|
|
78
79
|
```
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
It will be enforced that `iteratorFn` returns `Promise<void>` when using TypeScript.
|
|
81
82
|
|
|
82
|
-
|
|
83
|
+
## License
|
|
83
84
|
|
|
84
85
|
MIT © [Rafael Xavier de Souza](http://rafael.xavier.blog.br)
|
|
85
86
|
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, TOut, TIterable extends Iterable<TIn>>(
|
|
5
|
+
export declare function doWork<TIn, TOut, 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/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
|
package/dist/index.js
CHANGED
|
@@ -23,8 +23,12 @@ async function* doWorkAndYield(concurrentCount, iterable, iteratorFn) {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
// src/doWork.ts
|
|
26
|
-
async function doWork(
|
|
27
|
-
for await (const _result of doWorkAndYield(
|
|
26
|
+
async function doWork(concurrentCount, iterable, iteratorFn) {
|
|
27
|
+
for await (const _result of doWorkAndYield(
|
|
28
|
+
concurrentCount,
|
|
29
|
+
iterable,
|
|
30
|
+
iteratorFn
|
|
31
|
+
)) {
|
|
28
32
|
}
|
|
29
33
|
}
|
|
30
34
|
export {
|
package/package.json
CHANGED