Adding a mechanism to postpone DB access before the DB is ready.
This commit is contained in:
parent
a36c8a28a4
commit
5ca12b6780
|
@ -1,11 +1,17 @@
|
|||
import { getFamily, put } from './lib';
|
||||
import getUri from '../lib/ids';
|
||||
|
||||
export const initialAccount = {
|
||||
id: 'initial',
|
||||
name: '???',
|
||||
localDb: 'dyomedea',
|
||||
};
|
||||
|
||||
export const getAccounts = async () => {
|
||||
return await getFamily('account', {}, true);
|
||||
};
|
||||
|
||||
export const putAccount = async (name: string, account: any) => {
|
||||
const uri = getUri('account', name);
|
||||
export const putAccount = async (id: string, account: any) => {
|
||||
const uri = getUri('account', { account: id });
|
||||
return await put(uri, 'account', (_: any) => account, {}, true);
|
||||
};
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import _ from 'lodash';
|
||||
import PouchDB from 'pouchdb';
|
||||
import uri from '../lib/ids';
|
||||
import { getAccounts, initialAccount, putAccount } from './account';
|
||||
import changeHandler from './change-handler';
|
||||
|
||||
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) {
|
||||
globalThis.db = new PouchDB('dyomedea', {
|
||||
auto_compaction: false,
|
||||
|
@ -56,7 +65,6 @@ export const initDb = async (params: any) => {
|
|||
}
|
||||
//await await db.compact();
|
||||
|
||||
globalThis.dbReady = true;
|
||||
|
||||
const sync = PouchDB.sync(
|
||||
'dyomedea',
|
||||
|
@ -106,4 +114,7 @@ export const initDb = async (params: any) => {
|
|||
// console.log({ caller: 'initDb / back from db.changes', changes });
|
||||
|
||||
// changes.cancel();
|
||||
|
||||
globalThis.dbReady = true;
|
||||
|
||||
};
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
import { get, put } from './lib';
|
||||
|
||||
const emptySettings = {};
|
||||
|
||||
export const getSettings = async () => {
|
||||
try {
|
||||
return await get('settings', true);
|
||||
} catch (err) {
|
||||
console.error({ caller: 'getSettings', err });
|
||||
return undefined;
|
||||
return emptySettings;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
|
@ -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) => {
|
||||
|
|
Loading…
Reference in New Issue