Skip to content

Commit b4d617e

Browse files
committed
Using FinalizationRegistry to always terminate workers
1 parent 006a974 commit b4d617e

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/pool.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class WorkerWrapper {
7575
}
7676
}
7777

78+
const finalizationRegistry = new FinalizationRegistry((worker) => {
79+
worker.terminate();
80+
});
81+
7882
/**
7983
* Pool for workers to decode chunks of the images.
8084
*/
@@ -114,7 +118,10 @@ class Pool {
114118
this.workerWrappers = (async () => {
115119
const workerWrappers = [];
116120
for (let i = 0; i < size; i++) {
117-
workerWrappers.push(new WorkerWrapper(createWorker()));
121+
const worker = createWorker();
122+
const wrapper = new WorkerWrapper(worker);
123+
workerWrappers.push(wrapper);
124+
finalizationRegistry.register(wrapper, worker, wrapper);
118125
}
119126
return workerWrappers;
120127
})();

src/worker/create.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The contents of this file may be overridded for some build processes
2+
3+
import Worker from 'web-worker';
4+
5+
export default function create() {
6+
return new Worker(new URL('./decoder.js', import.meta.url), {
7+
type: 'module',
8+
});
9+
}

0 commit comments

Comments
 (0)