@altano/tiny-async-pool 2.0.0 → 3.0.1
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 +4 -2
- package/dist/doWork.d.ts +2 -2
- package/dist/doWork.js +12 -0
- package/dist/doWorkAndYield.js +25 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -37
- package/package.json +19 -19
- /package/{LICENSE-MIT → LICENSE} +0 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# tiny-async-pool
|
|
2
2
|
|
|
3
|
-
This is an ESM + TypeScript
|
|
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
4
|
|
|
5
5
|
## Why?
|
|
6
6
|
|
|
@@ -71,7 +71,9 @@ Iterator function that takes two arguments: the value of each iteration and the
|
|
|
71
71
|
|
|
72
72
|
### `doWork(concurrency, iterable, iteratorFn)`
|
|
73
73
|
|
|
74
|
-
Like `doWorkAndYield(...)` but
|
|
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.
|
|
75
|
+
|
|
76
|
+
For example, a batch file eraser is as simple as:
|
|
75
77
|
|
|
76
78
|
```js
|
|
77
79
|
const eraseFile = (path) => fs.unlink(path);
|
package/dist/doWork.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type IterableItem } from "./doWorkAndYield";
|
|
1
|
+
import { type IterableItem } from "./doWorkAndYield.js";
|
|
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
6
|
/**
|
|
7
7
|
* The size of the batch of work, or, how many times `iteratorFn` will be
|
|
8
8
|
* called in parallel.
|
package/dist/doWork.js
ADDED
|
@@ -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.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from "./doWork";
|
|
2
|
-
export * from "./doWorkAndYield";
|
|
1
|
+
export * from "./doWork.js";
|
|
2
|
+
export * from "./doWorkAndYield.js";
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,37 +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(concurrentCount, iterable, iteratorFn) {
|
|
27
|
-
for await (const _result of doWorkAndYield(
|
|
28
|
-
concurrentCount,
|
|
29
|
-
iterable,
|
|
30
|
-
iteratorFn
|
|
31
|
-
)) {
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
export {
|
|
35
|
-
doWork,
|
|
36
|
-
doWorkAndYield
|
|
37
|
-
};
|
|
1
|
+
export * from "./doWork.js";
|
|
2
|
+
export * from "./doWorkAndYield.js";
|
package/package.json
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altano/tiny-async-pool",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
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,35 @@
|
|
|
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
|
-
"
|
|
48
|
+
"vite": "^5.2.8",
|
|
49
|
+
"vitest": "^1.4.0",
|
|
52
50
|
"@altano/tsconfig": "0.0.0",
|
|
53
|
-
"eslint-config-altano": "0.0.
|
|
51
|
+
"eslint-config-altano": "0.0.0"
|
|
54
52
|
},
|
|
55
53
|
"publishConfig": {
|
|
56
54
|
"access": "public"
|
|
57
55
|
},
|
|
58
56
|
"scripts": {
|
|
59
|
-
"build": "tsup
|
|
60
|
-
"build:types": "tsc --
|
|
61
|
-
"
|
|
57
|
+
"build": "tsup --config build-config/tsup.config.node.ts",
|
|
58
|
+
"build:types": "tsc --project tsconfig.declarations.json",
|
|
59
|
+
"clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
|
|
60
|
+
"dev": "pnpm run build --watch",
|
|
62
61
|
"format": "prettier --check src",
|
|
63
62
|
"format:fix": "prettier --write src",
|
|
64
|
-
"lint": "eslint src/**/*.ts*",
|
|
65
|
-
"lint:
|
|
66
|
-
"lint:
|
|
67
|
-
"
|
|
68
|
-
"test": "vitest
|
|
69
|
-
"
|
|
63
|
+
"lint": "eslint package.json src/**/*.ts*",
|
|
64
|
+
"lint:fix": "TIMING=1 pnpm lint --fix",
|
|
65
|
+
"lint:timing": "TIMING=1 pnpm lint",
|
|
66
|
+
"test:unit": "vitest --run",
|
|
67
|
+
"test:unit:watch": "vitest",
|
|
68
|
+
"typecheck:src": "tsc --noEmit",
|
|
69
|
+
"typecheck:test": "tsc --noEmit --project ./tests/tsconfig.json"
|
|
70
70
|
}
|
|
71
71
|
}
|
/package/{LICENSE-MIT → LICENSE}
RENAMED
|
File without changes
|