Skip to content

Commit

Permalink
remove promisify
Browse files Browse the repository at this point in the history
  • Loading branch information
phenylshima committed Oct 21, 2023
1 parent fc27331 commit 7de4b13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/synthesis/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Readable } from "node:stream";
import * as util from "node:util";
import {
AudioResource,
StreamType,
Expand All @@ -19,12 +18,8 @@ class SynthesizeWorkerPool extends WorkerPool<Task, Result, AltJTalkConfig> {
inputText: string,
option: SynthesisOption,
): Promise<Int16Array> {
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;
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/synthesis/worker-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,19 @@ export default class WorkerPool<T, R, W> extends EventEmitter {
this.emit(kWorkerFreedEvent);
}

protected runTask(task: T, callback: Callback<R>) {
protected runTask(task: T): Promise<R>;
protected runTask(task: T, callback: Callback<R>): void;
protected runTask(task: T, callback?: Callback<R>): Promise<R> | 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.
Expand Down

0 comments on commit 7de4b13

Please sign in to comment.