Refactoring the worker / db init flow.

This commit is contained in:
Eric van der Vlist 2023-02-13 15:44:02 +01:00
parent cfaf7ad72f
commit 6dfe98b655
5 changed files with 57 additions and 48 deletions

View File

@ -439,12 +439,16 @@ export const putGpx = async (params: any) => {
await put(id, 'gpx', (doc) => gpx, gpx);
if (extensions !== undefined) {
await put(
`${id}/4extensions`,
'extensions',
(doc) => extensions,
extensions
);
try {
await put(
`${id}/4extensions`,
'extensions',
(doc) => extensions,
extensions
);
} catch (error) {
console.error({ caller: 'putGpx / extensions', error });
}
}
if (previousShared !== extensions?.shared) {

View File

@ -99,5 +99,7 @@ export const initDb = async () => {
globalThis.dbReady = true;
watchDbLegacy();
console.log({ caller: 'initDb / done' });
return;
}
};

View File

@ -1,18 +1,13 @@
/* @refresh reload */
import { render } from 'solid-js/web';
import { Router, hashIntegration } from '@solidjs/router';
import { Router } from '@solidjs/router';
import {
I18nContext,
createI18nContext,
useI18n,
} from '@solid-primitives/i18n';
import dict from './i18n';
import App from './App';
import { Language } from '@suid/icons-material';
import { createWorkerSignal } from './solid-workers/solid-worker-main';
import { createEffect } from 'solid-js';
import dispatch from './workers/dispatcher-main';
// See https://stackoverflow.com/questions/71538643/property-wakelock-does-not-exist-on-type-navigator
const requestWakeLock = async () => {

View File

@ -1,23 +1,21 @@
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 {
var dispatcherQueue: { index: number; queue: Map<number, any> };
var dbReady: boolean;
var worker: any;
}
export var worker: any;
export const init = () => {
console.log({ caller: 'dispatcher-main / init' });
// console.log({ caller: 'dispatcher-main / init' });
globalThis.dispatcherQueue = { index: 0, queue: new Map() };
console.log({ caller: 'dispatcher-main / init', globalThis });
// console.log({ caller: 'dispatcher-main / init', globalThis });
worker = getWorker();
console.log({ caller: 'dispatcher-main / init', worker });
globalThis.worker = getWorker();
// console.log({ caller: 'dispatcher-main / init', worker });
worker.onmessage = (event: any) => {
globalThis.worker.onmessage = (event: any) => {
const { id, payload } = event.data;
// console.log({
// caller: 'dispatcher-main / message received',
@ -25,35 +23,42 @@ export const init = () => {
// payload,
// dispatcherQueue: JSON.parse(JSON.stringify(dispatcherQueue)),
// });
dispatcherQueue.queue.get(id).callBack(null, payload, id);
if (!dispatcherQueue.queue.get(id).live) {
globalThis.dispatcherQueue.queue.get(id).callBack(null, payload, id);
if (!globalThis.dispatcherQueue.queue.get(id).live) {
cancelDispatch(id);
}
};
};
const [watchDbStatus] = createWorkerSignal({
provider: 'watchDb',
});
const dbReady = () => {
// console.log({
// caller: 'dispatcher-main / waiting',
// dbStatus: watchDbStatus(),
// dbReady: watchDbStatus()?.opened,
// });
return watchDbStatus()?.opened;
};
const dispatch = async (
payload: any,
callBack?: (error: any, result: any, id?: number | undefined) => void,
live?: boolean
) => {
// console.log({ caller: 'dispatcher-main / dispatch', payload, callBack });
if (worker === undefined) {
let initializing = false;
console.log({
caller: 'dispatcher-main / dispatch',
payload,
callBack,
initializing,
worker: globalThis.worker,
});
if (globalThis.worker === undefined && !initializing) {
initializing = true;
init();
await until(() => !!globalThis.worker, 100); // wait until the worker is initialized
dispatch({ action: 'initDb' }, (error, result) => {
if (error) {
console.error({ caller: 'worker-main / init', error });
return;
}
globalThis.dbReady = true;
});
initializing = false;
}
if (payload.action !== 'initDb') {
await until(() => globalThis.dbReady, 100); // Wait until databases have been initialized
}
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) => {
@ -67,21 +72,24 @@ const dispatch = async (
});
}
/** Otherwise, use the callback function */
dispatcherQueue.queue.set(dispatcherQueue.index, { callBack, live });
globalThis.dispatcherQueue.queue.set(globalThis.dispatcherQueue.index, {
callBack,
live,
});
const message = {
id: dispatcherQueue.index++,
id: globalThis.dispatcherQueue.index++,
payload: payload,
};
worker.postMessage(message);
// console.log({
// caller: 'dispatcher-main / message sent',
// message,
// dispatcherQueue,
// });
console.log({
caller: 'dispatcher-main / message sent',
message,
dispatcherQueue: globalThis.dispatcherQueue,
});
};
export const cancelDispatch = (id: number) => {
dispatcherQueue.queue.delete(id);
globalThis.dispatcherQueue.queue.delete(id);
};
export default dispatch;

View File

@ -68,9 +68,9 @@ onmessage = async function (e) {
};
// console.log({ caller: 'dispatcher-worker / onmessage', e });
initDb();
// initDb();
await until(() => globalThis.dbReady, 100); // Wait until databases have been initialized in this worker
// 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) {