Searching a new balance between the two web workers...

This commit is contained in:
vdv committed 2023-02-11 15:55:49 +01:00
1 parent 71bd820da9
commit 0e714ac361
8 files changed
+247 -201

No files matched your search

+39 -23
View File
@@ -1,3 +1,6 @@
import { createEffect } from 'solid-js';
import { until } from '../lib/async-wait';
import { createWorkerSignal } from '../solid-workers/solid-worker-main';
import { getWorker } from './get-worker';
declare global {
@@ -27,42 +30,55 @@ export const init = () => {
cancelDispatch(id);
}
};
};
dispatch({ action: 'initDb' });
const [watchDbStatus] = createWorkerSignal({
provider: 'watchDb',
});
const dbReady = () => {
console.log({
caller: 'dispatcher-main / waiting',
dbStatus: watchDbStatus(),
dbReady: watchDbStatus()?.opened,
});
return watchDbStatus()?.opened;
};
const dispatch = (
payload: any,
callBack?: (error: any, result: any, id?: number|undefined) => void,
callBack?: (error: any, result: any, id?: number | undefined) => void,
live?: boolean
) => {
console.log({ caller: 'dispatcher-main / dispatch', payload });
if (worker === undefined) {
init();
}
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);
}
until(dbReady, 1000).then(() => {
// 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);
}
});
});
}
/** 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,
});
}
/** 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,
});
};
+3 -19
View File
@@ -27,12 +27,6 @@ import { until } from '../lib/async-wait';
console.log({ caller: 'dispatcher-worker' });
declare global {
var dbReady: boolean;
}
globalThis.dbReady = false;
onmessage = async function (e) {
const actions = {
initDb,
@@ -74,6 +68,9 @@ onmessage = async function (e) {
};
console.log({ caller: 'dispatcher-worker / onmessage', e });
initDb();
await until(() => globalThis.dbReady, 100); // Wait until databases have been initialized in this worker
const { id, payload } = e.data;
var returnValue: any = 'unknownAction';
if (payload.action in actions) {
@@ -83,19 +80,6 @@ onmessage = async function (e) {
payload,
dbReady: globalThis.dbReady,
});
if (payload.action !== 'initDb' && !globalThis.dbReady) {
console.log({
caller: 'dispatcher-worker / waiting for dbReady',
id,
dbReady: globalThis.dbReady,
});
await until(() => globalThis.dbReady, 10);
console.log({
caller: 'dispatcher-worker / dbReady',
id,
dbReady: globalThis.dbReady,
});
}
returnValue = await actions[<keyof typeof actions>payload.action]({
...payload.params,
_dispatchId: id,