From 0b7b72c8ef39daa04e659af7a044f17e3b163473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B2=81=E6=A0=91=E4=BA=BA?= Date: Thu, 18 May 2023 00:21:11 +0100 Subject: [PATCH] refactor: simplify logic handling worker message --- src/util/WorkerEventBus.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/util/WorkerEventBus.ts b/src/util/WorkerEventBus.ts index 8565d5d..e94b46a 100644 --- a/src/util/WorkerEventBus.ts +++ b/src/util/WorkerEventBus.ts @@ -68,18 +68,20 @@ export class WorkerServerBus { onmessage = async (e: MessageEvent) => { const { id, action, payload } = e.data; const handler = this.handlers.get(action); + + let result = null; + let error = null; + if (!handler) { - postMessage({ id, result: null, error: new Error('Handler missing for action ' + action) }); - return; + error = new Error('Handler missing for action ' + action); + } else { + try { + result = await handler(payload); + } catch (e: unknown) { + error = e; + } } - let result = undefined; - let error = undefined; - try { - result = await handler(payload); - } catch (e: unknown) { - error = e; - } postMessage({ id, result, error }); }; }