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

This commit is contained in:
Eric van der Vlist 2022-12-25 21:37:59 +01:00
parent a36c8a28a4
commit 5ca12b6780
5 changed files with 67 additions and 7 deletions

View File

@ -1,11 +1,17 @@
import { getFamily, put } from './lib'; import { getFamily, put } from './lib';
import getUri from '../lib/ids'; import getUri from '../lib/ids';
export const initialAccount = {
id: 'initial',
name: '???',
localDb: 'dyomedea',
};
export const getAccounts = async () => { export const getAccounts = async () => {
return await getFamily('account', {}, true); return await getFamily('account', {}, true);
}; };
export const putAccount = async (name: string, account: any) => { export const putAccount = async (id: string, account: any) => {
const uri = getUri('account', name); const uri = getUri('account', { account: id });
return await put(uri, 'account', (_: any) => account, {}, true); return await put(uri, 'account', (_: any) => account, {}, true);
}; };

View File

@ -1,6 +1,7 @@
import _ from 'lodash'; import _ from 'lodash';
import PouchDB from 'pouchdb'; import PouchDB from 'pouchdb';
import uri from '../lib/ids'; import uri from '../lib/ids';
import { getAccounts, initialAccount, putAccount } from './account';
import changeHandler from './change-handler'; import changeHandler from './change-handler';
const dbDefinitionId = uri('dbdef', {}); const dbDefinitionId = uri('dbdef', {});
@ -27,6 +28,14 @@ export const initDb = async (params: any) => {
}); });
} }
const accounts = (await getAccounts()).rows;
console.log({ caller: 'initDb', accounts });
if (accounts.length === 0) {
accounts[0] = initialAccount;
await putAccount(initialAccount.id, initialAccount);
}
if (globalThis.db === undefined) { if (globalThis.db === undefined) {
globalThis.db = new PouchDB('dyomedea', { globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false, auto_compaction: false,
@ -56,8 +65,7 @@ export const initDb = async (params: any) => {
} }
//await await db.compact(); //await await db.compact();
globalThis.dbReady = true;
const sync = PouchDB.sync( const sync = PouchDB.sync(
'dyomedea', 'dyomedea',
'http://admin:password@localhost:5984/dyomedea', 'http://admin:password@localhost:5984/dyomedea',
@ -106,4 +114,7 @@ export const initDb = async (params: any) => {
// console.log({ caller: 'initDb / back from db.changes', changes }); // console.log({ caller: 'initDb / back from db.changes', changes });
// changes.cancel(); // changes.cancel();
globalThis.dbReady = true;
}; };

View File

@ -1,11 +1,13 @@
import { get, put } from './lib'; import { get, put } from './lib';
const emptySettings = {};
export const getSettings = async () => { export const getSettings = async () => {
try { try {
return await get('settings', true); return await get('settings', true);
} catch (err) { } catch (err) {
console.error({ caller: 'getSettings', err }); console.error({ caller: 'getSettings', err });
return undefined; return emptySettings;
} }
}; };

11
src/lib/async-wait.ts Normal file
View File

@ -0,0 +1,11 @@
// See https://stackoverflow.com/questions/22125865/how-to-wait-until-a-predicate-condition-becomes-true-in-javascript
export const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const until = async (fn: any, ms: number = 0) => {
while (!fn()) {
await sleep(ms);
}
};

View File

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