From 4e9e360751c59f4010bc4055ca33e09313ad634e Mon Sep 17 00:00:00 2001 From: Jixun Wu 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 }); }; }