Skip to content

Commit f73fccb

Browse files
committed
fix(browser): implement terminateWorker correctly
1 parent 6de0ddb commit f73fccb

4 files changed

Lines changed: 28 additions & 13 deletions

File tree

src/Ework.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
makeWorkerCode,
66
spawnWorker,
77
addWorkerListener,
8+
terminateWorker,
89
} from './worker';
910

1011
export type Eworker<Input, Output> = (value: Input) => Output | Promise<Output>;
@@ -29,7 +30,7 @@ export interface IEworkOptions {
2930
type WorkerResolveFn<Output> = (result: Output) => void;
3031
type WorkerRejectFn = (reason: unknown) => void;
3132

32-
interface IWorker<Input, Output> {
33+
export interface IWorker<Input, Output> {
3334
worker: Worker;
3435
isWorking: boolean;
3536
job: IWorkerJob<Input, Output> | null;
@@ -135,17 +136,7 @@ export class Ework<Input, Output> {
135136
}
136137

137138
public async terminate(): Promise<void> {
138-
await Promise.all(
139-
this.workers.map(
140-
(w) =>
141-
new Promise((resolve) => {
142-
w.worker.terminate(() => resolve());
143-
if (w.job !== null) {
144-
w.job.reject(new Error('worker terminated'));
145-
}
146-
}),
147-
),
148-
);
139+
await Promise.all(this.workers.map(terminateWorker));
149140
this.freeWorkers = 0;
150141
this.queue = [];
151142
this.workers = [];

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './Ework';
1+
export { Ework, Eworker, IEworkOptions } from './Ework';

src/worker.browser.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IWorker } from './Ework';
2+
13
/* eslint-env browser */
24

35
export const numCpus = navigator.hardwareConcurrency || 1;
@@ -8,6 +10,15 @@ export function spawnWorker(workerCode: string): Worker {
810
return new Worker(url);
911
}
1012

13+
export function terminateWorker<Input, Output>(
14+
worker: IWorker<Input, Output>,
15+
): void {
16+
worker.worker.terminate();
17+
if (worker.job !== null) {
18+
worker.job.reject(new Error('worker terminated'));
19+
}
20+
}
21+
1122
export function addWorkerListener(
1223
worker: Worker,
1324
type: string,

src/worker.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { cpus } from 'os';
22

33
import { Worker } from 'worker_threads';
44

5+
import { IWorker } from './Ework';
6+
57
const cpuList = cpus();
68

79
export const numCpus = cpuList ? cpuList.length : 1;
@@ -10,6 +12,17 @@ export function spawnWorker(workerCode: string): Worker {
1012
return new Worker(workerCode, { eval: true });
1113
}
1214

15+
export function terminateWorker<Input, Output>(
16+
worker: IWorker<Input, Output>,
17+
): Promise<void> {
18+
return new Promise((resolve) => {
19+
worker.worker.terminate(() => resolve());
20+
if (worker.job !== null) {
21+
worker.job.reject(new Error('worker terminated'));
22+
}
23+
});
24+
}
25+
1326
export function addWorkerListener(
1427
worker: Worker,
1528
type: string,

0 commit comments

Comments
 (0)