refactor: simplify logic handling worker message
This commit is contained in:
parent
7df50c7c75
commit
0b7b72c8ef
@ -68,18 +68,20 @@ export class WorkerServerBus {
|
|||||||
onmessage = async (e: MessageEvent<WorkerClientRequestPayload>) => {
|
onmessage = async (e: MessageEvent<WorkerClientRequestPayload>) => {
|
||||||
const { id, action, payload } = e.data;
|
const { id, action, payload } = e.data;
|
||||||
const handler = this.handlers.get(action);
|
const handler = this.handlers.get(action);
|
||||||
if (!handler) {
|
|
||||||
postMessage({ id, result: null, error: new Error('Handler missing for action ' + action) });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = undefined;
|
let result = null;
|
||||||
let error = undefined;
|
let error = null;
|
||||||
|
|
||||||
|
if (!handler) {
|
||||||
|
error = new Error('Handler missing for action ' + action);
|
||||||
|
} else {
|
||||||
try {
|
try {
|
||||||
result = await handler(payload);
|
result = await handler(payload);
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
error = e;
|
error = e;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
postMessage({ id, result, error });
|
postMessage({ id, result, error });
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user