Adding a mechanism to postpone DB access before the DB is ready.

This commit is contained in:
vdv committed 2022-12-25 21:37:59 +01:00
1 parent a36c8a28a4
commit 5ca12b6780
5 files changed
+67 -7

No files matched your search

+32 -2
View File
@@ -17,11 +17,18 @@ import { getState, setState } from '../db/state';
import { getTrk, putNewTrk } from '../db/trk';
import { getTrkseg, appendTrkpt } from '../db/trkseg';
import { getWpt, putWpt } from '../db/wpt';
import { until } from '../lib/async-wait';
//const self = globalThis as unknown as WorkerGlobalScope;
console.log({ caller: 'dispatcher-worker' });
declare global {
var dbReady: boolean;
}
globalThis.dbReady = false;
onmessage = async function (e) {
const actions = {
initDb,
@@ -57,14 +64,37 @@ onmessage = async function (e) {
const { id, payload } = e.data;
var returnValue: any = 'unknownAction';
if (payload.action in actions) {
console.log({ caller: 'dispatcher-worker / awaiting', id, payload });
console.log({
caller: 'dispatcher-worker / awaiting',
id,
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,
});
}
postMessage({ id: id, payload: returnValue });
console.log({ caller: 'dispatcher-worker / response sent', id, returnValue });
console.log({
caller: 'dispatcher-worker / response sent',
id,
returnValue,
dbReady: globalThis.dbReady,
});
};
export const returnAgain = (id: number, returnValue: any) => {