Skip to content

Commit

Permalink
fix(io): terminate web workers for vtk async read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed Dec 29, 2023
1 parent cb5791e commit 30c7109
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/io/readWriteImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const readImage = async (file: File) => {
if (file.name.endsWith('.vti'))
return (await vtiReader(file)) as vtkImageData;

const { image } = await readImageItk(null, file);
const { image, webWorker } = await readImageItk(null, file);
webWorker.terminate();
return vtkITKHelper.convertItkToVtkImage(image);
};

Expand Down
18 changes: 12 additions & 6 deletions src/io/vtk/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ interface FailResult {
type ReadResult = SuccessReadResult | FailResult;

export const runAsyncVTKReader = (readerName: string) => async (file: File) => {
const worker = new PromiseWorker(
new Worker(new URL('./async.reader.worker.ts', import.meta.url), {
const asyncWorker = new Worker(
new URL('./async.reader.worker.ts', import.meta.url),
{
type: 'module',
})
}
);
const worker = new PromiseWorker(asyncWorker);
const data = (await worker.postMessage({
file,
readerName,
})) as ReadResult;
asyncWorker.terminate();
if (data.status === 'success') {
return vtk(data.obj) as vtkObject;
}
Expand All @@ -41,15 +44,18 @@ type WriteResult = SuccessWriteResult | FailResult;

export const runAsyncVTKWriter =
(writerName: string) => async (dataSet: vtkDataSet) => {
const worker = new PromiseWorker(
new Worker(new URL('./async.writer.worker.ts', import.meta.url), {
const asyncWorker = new Worker(
new URL('./async.writer.worker.ts', import.meta.url),
{
type: 'module',
})
}
);
const worker = new PromiseWorker(asyncWorker);
const result = (await worker.postMessage({
obj: dataSet.getState(),
writerName,
})) as WriteResult;
asyncWorker.terminate();
if (result.status === 'success') {
return result.data;
}
Expand Down

0 comments on commit 30c7109

Please sign in to comment.