@altano/tiny-async-pool 1.0.2 → 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 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.
@@ -9,8 +11,7 @@ The goal of this library is to use the native async iterator (ES9), native async
9
11
  `doWorkAndYield` runs multiple promise-returning & async functions in a limited concurrency pool. It rejects immediately as soon as one of the promises rejects. It calls the iterator function as soon as possible (under concurrency limit). It returns an async iterator that yields as soon as a promise completes (under concurrency limit). For example:
10
12
 
11
13
  ```js
12
- const timeout = (ms) =>
13
- new Promise((resolve) => setTimeout(() => resolve(ms), ms));
14
+ const timeout = (ms) => new Promise((resolve) => setTimeout(() => resolve(ms), ms));
14
15
 
15
16
  for await (const ms of doWorkAndYield(2, [1000, 5000, 3000, 2000], timeout)) {
16
17
  console.log(ms);
@@ -70,18 +71,16 @@ Iterator function that takes two arguments: the value of each iteration and the
70
71
 
71
72
  ### `doWork(concurrency, iterable, iteratorFn)`
72
73
 
73
- The same as `doWorkAndYield(...)` but just returns a Promise that resolves when all work resolves/rejects, e.g.:
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:
74
75
 
75
76
  ```js
76
- const timeout = (ms) =>
77
- new Promise((resolve) => setTimeout(() => resolve(ms), ms));
78
-
79
- await doWork(2, [1000, 5000, 3000, 2000], timeout);
77
+ const eraseFile = (path) => fs.unlink(path);
78
+ const eraseFiles = async (files) => doWork(4, files, eraseFile);
80
79
  ```
81
80
 
82
- ## License
81
+ It will be enforced that `iteratorFn` returns `Promise<void>` when using TypeScript.
83
82
 
84
- This project is a fork of `async-pool` by Rafael Xavier de Souza. It modernizes (as of 2022) the library by being a first-class TypeScript and ESM module.
83
+ ## License
85
84
 
86
85
  MIT © [Rafael Xavier de Souza](http://rafael.xavier.blog.br)
87
86
  MIT © [Alan Norbauer](https://alan.norbauer.com)
@@ -0,0 +1,21 @@
1
+ import { type IterableItem } from "./doWorkAndYield";
2
+ /**
3
+ * Process items from `iterable` in batches.
4
+ */
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>;
21
+ //# sourceMappingURL=doWork.d.ts.map
@@ -0,0 +1,21 @@
1
+ export type IterableItem<T> = T extends Iterable<infer R> ? R : never;
2
+ /**
3
+ * Process items from `iterable` in batches and yield the result of each call to
4
+ * `iteratorFn`
5
+ */
6
+ export declare function doWorkAndYield<TIn, TOut, TIterable extends Iterable<TIn>>(
7
+ /**
8
+ * The size of the batch of work, or, how many times `iteratorFn` will be
9
+ * called in parallel.
10
+ */
11
+ concurrentCount: number,
12
+ /**
13
+ * An iterable that contains the items that should be passed to `iteratorFn`.
14
+ */
15
+ iterable: TIterable,
16
+ /**
17
+ * The async callback function that does the work. Will be passed items from
18
+ * `iterable`.
19
+ */
20
+ iteratorFn: (item: IterableItem<TIterable>, Iterable: TIterable) => Promise<TOut>): AsyncGenerator<Awaited<TOut>, void, unknown>;
21
+ //# sourceMappingURL=doWorkAndYield.d.ts.map
package/dist/index.d.ts CHANGED
@@ -1,27 +1,3 @@
1
- declare type IterableItem<T> = T extends Iterable<infer R> ? R : never;
2
- /**
3
- * Process items from `iterable` in batches and yield the result of each call to
4
- * `iteratorFn`
5
- */
6
- declare function doWorkAndYield<TIn, TOut, TIterable extends Iterable<TIn>>(
7
- /**
8
- * The size of the batch of work, or, how many times `iteratorFn` will be
9
- * called in parallel.
10
- */
11
- concurrentCount: number,
12
- /**
13
- * An iterable that contains the items that should be passed to `iteratorFn`.
14
- */
15
- iterable: TIterable,
16
- /**
17
- * The async callback function that does the work. Will be passed items from
18
- * `iterable`.
19
- */
20
- iteratorFn: (item: IterableItem<TIterable>, Iterable: TIterable) => Promise<TOut>): AsyncGenerator<Awaited<TOut>, void, unknown>;
21
-
22
- /**
23
- * Process items from `iterable` in batches.
24
- */
25
- declare function doWork<TIn, TOut, TIterable extends Iterable<TIn>>(...args: Parameters<typeof doWorkAndYield<TIn, TOut, TIterable>>): Promise<void>;
26
-
27
- export { doWork, doWorkAndYield };
1
+ export * from "./doWork";
2
+ export * from "./doWorkAndYield";
3
+ //# sourceMappingURL=index.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(...args) {
27
- for await (const _result of doWorkAndYield(...args)) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altano/tiny-async-pool",
3
- "version": "1.0.2",
3
+ "version": "2.0.0",
4
4
  "description": "Run multiple promise-returning & async functions with limited concurrency using native ES9",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,18 +8,10 @@
8
8
  "sideEffects": false,
9
9
  "files": [
10
10
  "LICENSE-MIT",
11
- "dist",
11
+ "dist/**/*.js",
12
+ "dist/**/*.d.ts",
12
13
  "README.md"
13
14
  ],
14
- "scripts": {
15
- "build": "tsup src/index.ts --format esm --dts",
16
- "dev": "tsup src/index.ts --watch --format esm --dts",
17
- "format": "prettier --write src",
18
- "lint": "TIMING=1 eslint src/**/*.ts* --fix",
19
- "clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
20
- "test": "vitest --run",
21
- "test:watch": "vitest"
22
- },
23
15
  "repository": {
24
16
  "type": "git",
25
17
  "url": "https://github.com/altano/npm-packages",
@@ -46,25 +38,34 @@
46
38
  ],
47
39
  "license": "MIT",
48
40
  "devDependencies": {
49
- "@altano/tsconfig": "*",
50
- "@types/node": "^18.6.4",
51
- "@types/prettier": "^2.7.0",
52
- "@typescript-eslint/eslint-plugin": "^5.36.1",
53
- "@typescript-eslint/parser": "^5.36.1",
54
- "@vitest/ui": "^0.21.0",
41
+ "@types/node": "^20.11.25",
42
+ "@types/prettier": "^3.0.0",
43
+ "@vitest/ui": "^1.3.1",
44
+ "chai": "^4.4.1",
55
45
  "chai-as-promised": "^7.1.1",
56
- "chai": "^4.3.6",
57
- "eslint-config-altano": "*",
58
- "eslint-config-prettier": "^8.5.0",
59
- "eslint-plugin-prettier": "^4.2.1",
60
- "eslint": "^8.23.0",
61
- "mocha": "^10.0.0",
62
- "prettier": "^2.7.1",
63
- "tsup": "^6.2.3",
64
- "typescript": "^4.8.2",
65
- "vitest": "^0.21.0"
46
+ "eslint": "^8.57.0",
47
+ "mocha": "^10.3.0",
48
+ "prettier": "^3.2.5",
49
+ "tsup": "^8.0.2",
50
+ "typescript": "^5.4.2",
51
+ "vitest": "^1.3.1",
52
+ "@altano/tsconfig": "0.0.0",
53
+ "eslint-config-altano": "0.0.1"
66
54
  },
67
55
  "publishConfig": {
68
56
  "access": "public"
57
+ },
58
+ "scripts": {
59
+ "build": "tsup src/index.ts --format esm --onSuccess \"pnpm build:types\"",
60
+ "build:types": "tsc --emitDeclarationOnly --declaration",
61
+ "dev": "tsup src/index.ts --watch --format esm --dts",
62
+ "format": "prettier --check src",
63
+ "format:fix": "prettier --write src",
64
+ "lint": "eslint src/**/*.ts*",
65
+ "lint:timing": "TIMING=1 eslint src/**/*.ts*",
66
+ "lint:fix": "TIMING=1 eslint src/**/*.ts* --fix",
67
+ "clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
68
+ "test": "vitest --run",
69
+ "test:watch": "vitest"
69
70
  }
70
- }
71
+ }