diff --git a/src/core/internal/createWebWorkerPromise.ts b/src/core/internal/createWebWorkerPromise.ts index ac943817b..b7129df07 100644 --- a/src/core/internal/createWebWorkerPromise.ts +++ b/src/core/internal/createWebWorkerPromise.ts @@ -8,11 +8,24 @@ interface createWebWorkerPromiseResult { worker: Worker } +interface itkWorker extends Worker { + workerPromise?: typeof WebworkerPromise +} + // Internal function to create a web worker promise async function createWebWorkerPromise (existingWorker: Worker | null): Promise { + let workerPromise: typeof WebworkerPromise if (existingWorker != null) { - const webworkerPromise = new WebworkerPromise(existingWorker) - return await Promise.resolve({ webworkerPromise, worker: existingWorker }) + // See if we have a worker promise attached the worker, if so reuse it. This ensures + // that we can safely reuse the worker without issues. + const itkWebWorker = existingWorker as itkWorker; + if (itkWebWorker.workerPromise !== undefined) { + workerPromise = itkWebWorker.workerPromise; + } else { + workerPromise = new WebworkerPromise(existingWorker); + } + + return await Promise.resolve({ webworkerPromise: workerPromise, worker: existingWorker }) } let worker = null @@ -49,8 +62,13 @@ async function createWebWorkerPromise (existingWorker: Worker | null): Promise