Bug fix (bad promise magic!)

This commit is contained in:
Eric van der Vlist 2023-02-11 18:21:25 +01:00
parent 853b158dcf
commit 51250c1880
1 changed files with 25 additions and 32 deletions

View File

@ -44,47 +44,40 @@ const dbReady = () => {
return watchDbStatus()?.opened;
};
const dispatch = (
const dispatch = async (
payload: any,
callBack?: (error: any, result: any, id?: number | undefined) => void,
live?: boolean
) => {
console.log({ caller: 'dispatcher-main / dispatch', payload });
console.log({ caller: 'dispatcher-main / dispatch', payload, callBack });
if (worker === undefined) {
init();
}
let returnValue;
until(dbReady, 100).then(() => {
// Wait until databases have been created by health.ts
if (callBack === undefined) {
/** If a callback function is not provided, return a promise */
returnValue = new Promise((resolve, reject) => {
dispatch(payload, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
await until(dbReady, 100); // Wait until databases have been created by health.ts
if (callBack === undefined) {
/** If a callback function is not provided, return a promise */
return new Promise((resolve, reject) => {
dispatch(payload, (error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
});
} else {
/** Otherwise, use the callback function */
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
const message = {
id: dispatcherQueue.index++,
payload: payload,
};
worker.postMessage(message);
console.log({
caller: 'dispatcher-main / message sent',
message,
dispatcherQueue,
});
}
});
if (returnValue) {
return returnValue;
});
}
/** Otherwise, use the callback function */
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
const message = {
id: dispatcherQueue.index++,
payload: payload,
};
worker.postMessage(message);
console.log({
caller: 'dispatcher-main / message sent',
message,
dispatcherQueue,
});
};
export const cancelDispatch = (id: number) => {