From db2d6f4ca6279b1c3ed5af5252973ec4a3beb513 Mon Sep 17 00:00:00 2001 From: Jake Archibald Date: Tue, 29 Sep 2020 16:44:58 +0100 Subject: [PATCH] Fix worker bridge code --- src/client/lazy-app/worker-bridge/index.ts | 26 +++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/client/lazy-app/worker-bridge/index.ts b/src/client/lazy-app/worker-bridge/index.ts index 2085ec3d..e7cd10cc 100644 --- a/src/client/lazy-app/worker-bridge/index.ts +++ b/src/client/lazy-app/worker-bridge/index.ts @@ -15,6 +15,8 @@ class WorkerBridge { protected _worker?: Worker; /** Comlinked worker API. */ protected _workerApi?: ProcessorWorkerApi; + /** ID from setTimeout */ + protected _workerTimeout?: number; protected _terminateWorker() { if (!this._worker) return; @@ -36,30 +38,32 @@ for (const methodName of methodNames) { ...args: any ) { this._queue = this._queue + // Ignore any errors in the queue .catch(() => {}) .then(async () => { if (signal.aborted) throw new DOMException('AbortError', 'AbortError'); - let done = false; - - signal.addEventListener('abort', () => { - if (done) return; - this._terminateWorker(); - }); + clearTimeout(this._workerTimeout); if (!this._worker) this._startWorker(); - const timeoutId = setTimeout(() => { - this._terminateWorker(); - }, workerTimeout); + const onAbort = () => this._terminateWorker(); + signal.addEventListener('abort', onAbort); return abortable( signal, this._workerApi![methodName](...args) as any, ).finally(() => { - done = true; - clearTimeout(timeoutId); + // No longer care about aborting - this task is complete. + signal.removeEventListener('abort', onAbort); + + // Start a timer to clear up the worker. + this._workerTimeout = setTimeout(() => { + this._terminateWorker(); + }, workerTimeout); }); }); + + return this._queue; } as any; }