From f83745944c9604776dceed8361b665d48a90bb79 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 15 Aug 2023 11:40:07 +0200 Subject: [PATCH] refactor: add response type to SMTP client pool example MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../typescript/smtp-client-pool/src/main.ts | 3 ++- .../typescript/smtp-client-pool/src/pool.ts | 25 +++++++++---------- .../typescript/smtp-client-pool/src/worker.ts | 8 ++++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/examples/typescript/smtp-client-pool/src/main.ts b/examples/typescript/smtp-client-pool/src/main.ts index 70b1cb24..1b463fa0 100644 --- a/examples/typescript/smtp-client-pool/src/main.ts +++ b/examples/typescript/smtp-client-pool/src/main.ts @@ -1,8 +1,9 @@ +import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js' import { smtpClientPool } from './pool.js' const tos = ['bar@example.com, baz@example.com'] -const smtpClientPoolPromises = new Set>() +const smtpClientPoolPromises = new Set>() for (const to of tos) { smtpClientPoolPromises.add( smtpClientPool.execute({ diff --git a/examples/typescript/smtp-client-pool/src/pool.ts b/examples/typescript/smtp-client-pool/src/pool.ts index f7309447..ec2ec136 100644 --- a/examples/typescript/smtp-client-pool/src/pool.ts +++ b/examples/typescript/smtp-client-pool/src/pool.ts @@ -1,6 +1,7 @@ import { fileURLToPath } from 'node:url' import { dirname, extname, join } from 'node:path' import { DynamicThreadPool, availableParallelism } from 'poolifier' +import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js' import { type WorkerData } from './types.js' const workerFile = join( @@ -8,17 +9,15 @@ const workerFile = join( `worker${extname(fileURLToPath(import.meta.url))}` ) -export const smtpClientPool = new DynamicThreadPool( - 1, - availableParallelism(), - workerFile, - { - enableTasksQueue: true, - tasksQueueOptions: { - concurrency: 8 - }, - errorHandler: (e: Error) => { - console.error('Thread worker error:', e) - } +export const smtpClientPool = new DynamicThreadPool< +WorkerData, +SMTPTransport.SentMessageInfo +>(1, availableParallelism(), workerFile, { + enableTasksQueue: true, + tasksQueueOptions: { + concurrency: 8 + }, + errorHandler: (e: Error) => { + console.error('Thread worker error:', e) } -) +}) diff --git a/examples/typescript/smtp-client-pool/src/worker.ts b/examples/typescript/smtp-client-pool/src/worker.ts index a6026e4b..3ac62cca 100644 --- a/examples/typescript/smtp-client-pool/src/worker.ts +++ b/examples/typescript/smtp-client-pool/src/worker.ts @@ -1,13 +1,17 @@ import { ThreadWorker } from 'poolifier' import { createTransport } from 'nodemailer' import type Mail from 'nodemailer/lib/mailer/index.js' +import type SMTPTransport from 'nodemailer/lib/smtp-transport/index.js' import { type WorkerData } from './types.js' -class SmtpClientWorker extends ThreadWorker { +class SmtpClientWorker extends ThreadWorker< +WorkerData, +SMTPTransport.SentMessageInfo +> { public constructor () { super({ nodemailer: async (workerData?: WorkerData) => { - await createTransport(workerData?.smtpTransport).sendMail( + return await createTransport(workerData?.smtpTransport).sendMail( workerData?.mail as Mail.Options ) } -- 2.34.1