refactor: simplify logic handling worker message
This commit is contained in:
parent
fb14eabaf1
commit
4e9e360751
@ -68,18 +68,20 @@ export class WorkerServerBus {
|
||||
onmessage = async (e: MessageEvent<WorkerClientRequestPayload>) => {
|
||||
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 });
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user