From: Christopher Quadflieg Date: Fri, 2 Oct 2020 06:55:25 +0000 (+0200) Subject: Add typescript examples X-Git-Tag: v1.2.0~2^2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e9e162504b02f053c805c43e7cd5a4a340730970;p=poolifier.git Add typescript examples --- diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts new file mode 100644 index 00000000..8e7d4bb7 --- /dev/null +++ b/examples/typescript/pool.ts @@ -0,0 +1,22 @@ +import { join } from "path"; +import { DynamicThreadPool, FixedThreadPool } from "poolifier"; +import { MyData, MyResponse } from "./worker"; + +export const fixedPool = new FixedThreadPool>( + 8, + join(__dirname, "worker.js"), + { + errorHandler: (e) => console.error(e), + onlineHandler: () => console.log("Worker is online"), + } +); + +export const dynamicPool = new DynamicThreadPool>( + 2, + 8, + join(__dirname, "worker.js"), + { + errorHandler: (e) => console.error(e), + onlineHandler: () => console.log("Worker is online"), + } +); diff --git a/examples/typescript/worker.ts b/examples/typescript/worker.ts new file mode 100644 index 00000000..0b44e640 --- /dev/null +++ b/examples/typescript/worker.ts @@ -0,0 +1,29 @@ +import { ThreadWorker } from "poolifier"; + +export interface MyData { + ok: number; +} + +export interface MyResponse { + message: string; + data: MyData; +} + +class MyThreadWorker extends ThreadWorker> { + constructor() { + super((data) => this.process(data), { + maxInactiveTime: 60_000, + async: true, + }); + } + + private async process(data: MyData): Promise { + return new Promise((resolve) => { + setTimeout(() => { + resolve({ message: "Hello from Worker :)", data }); + }, 10_000); + }); + } +} + +export default new MyThreadWorker();