From 7de4b13c741f1a600dab8f5cff5f42e1235f8384 Mon Sep 17 00:00:00 2001 From: femshima <49227365+femshima@users.noreply.github.com> Date: Sat, 21 Oct 2023 14:18:32 +0900 Subject: [PATCH] remove promisify --- src/synthesis/index.ts | 9 ++------- src/synthesis/worker-pool.ts | 14 +++++++++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/synthesis/index.ts b/src/synthesis/index.ts index 26cf6e9b..dad4733a 100644 --- a/src/synthesis/index.ts +++ b/src/synthesis/index.ts @@ -1,5 +1,4 @@ import { Readable } from "node:stream"; -import * as util from "node:util"; import { AudioResource, StreamType, @@ -19,12 +18,8 @@ class SynthesizeWorkerPool extends WorkerPool { inputText: string, option: SynthesisOption, ): Promise { - const result = await util.promisify(this.runTask.bind(this))({ - inputText, - option, - }); - if (result) return result?.data; - else throw new Error("Task returned error!"); + const result = await this.runTask({ inputText, option }); + return result.data; } } diff --git a/src/synthesis/worker-pool.ts b/src/synthesis/worker-pool.ts index a0ab9548..f359d0b5 100644 --- a/src/synthesis/worker-pool.ts +++ b/src/synthesis/worker-pool.ts @@ -89,7 +89,19 @@ export default class WorkerPool extends EventEmitter { this.emit(kWorkerFreedEvent); } - protected runTask(task: T, callback: Callback) { + protected runTask(task: T): Promise; + protected runTask(task: T, callback: Callback): void; + protected runTask(task: T, callback?: Callback): Promise | undefined { + if (!callback) { + return new Promise((resolve, reject) => { + this.runTask(task, (err, result) => { + if (err) reject(err); + else if (result) resolve(result); + else reject(new Error("Task failed for unknown reason")); + }); + }); + } + const worker = this.freeWorkers.pop(); if (!worker) { // No free threads, wait until a worker thread becomes free.