Skip to content

Commit 65fc6db

Browse files
committed
feat: make piscina entrypoint easier for typescript
1 parent c43b078 commit 65fc6db

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"c8": "^10.1.2",
5252
"concat-stream": "^2.0.0",
5353
"eslint": "^9.16.0",
54+
"expect-type": "^1.2.1",
5455
"gen-esm-wrapper": "^1.1.1",
5556
"neostandard": "^0.12.0",
5657
"tap": "^16.3.7",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ class ThreadPool {
720720
}
721721
}
722722

723-
export default class Piscina<T = any, R = any> extends EventEmitterAsyncResource {
723+
export default class Piscina<F extends (payload: any) => any = (payload: any) => any> extends EventEmitterAsyncResource {
724724
#pool : ThreadPool;
725725
#histogram: PiscinaHistogram | null = null;
726726

@@ -793,7 +793,7 @@ export default class Piscina<T = any, R = any> extends EventEmitterAsyncResource
793793
this.#pool = new ThreadPool(this, options);
794794
}
795795

796-
run (task : T, options : RunOptions = kDefaultRunOptions): Promise<R> {
796+
run (task : Parameters<F>[0], options : RunOptions = kDefaultRunOptions): Promise<ReturnType<F>> {
797797
if (options === null || typeof options !== 'object') {
798798
return Promise.reject(
799799
new TypeError('options must be an object'));

test/fixtures/worker-type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function add({ a, b }: { a: number; b: number }): number {
2+
return a + b;
3+
}

test/types-entrypoint.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { resolve } from "node:path";
2+
import { expectTypeOf } from "expect-type";
3+
4+
import { test } from "tap";
5+
6+
import Piscina from "../dist";
7+
8+
test("types can be inferred from function", async (t) => {
9+
const pool = new Piscina<typeof import("./fixtures/worker-type.js").default>({
10+
filename: resolve(__dirname, "fixtures/worker-type.js"),
11+
concurrentTasksPerWorker: 1,
12+
});
13+
14+
expectTypeOf(pool.run).parameter(0).toEqualTypeOf<{ a: number; b: number }>();
15+
expectTypeOf(pool.run).returns.toEqualTypeOf<Promise<number>>();
16+
17+
const result = await pool.run({ a: 4, b: 6 });
18+
t.equal(result, 10);
19+
});
20+
21+
test("types can be manually specified", async (t) => {
22+
const pool = new Piscina<(payload: { a: number; b: number }) => number>({
23+
filename: resolve(__dirname, "fixtures/worker-type.js"),
24+
concurrentTasksPerWorker: 1,
25+
});
26+
27+
expectTypeOf(pool.run).parameter(0).toEqualTypeOf<{ a: number; b: number }>();
28+
expectTypeOf(pool.run).returns.toEqualTypeOf<Promise<number>>();
29+
30+
const result = await pool.run({ a: 4, b: 6 });
31+
t.equal(result, 10);
32+
});

0 commit comments

Comments
 (0)