diff --git a/src/db/account.ts b/src/db/account.ts index b1bb32d..b4b8a2d 100644 --- a/src/db/account.ts +++ b/src/db/account.ts @@ -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); }; diff --git a/src/db/index.ts b/src/db/index.ts index dea544c..677af6a 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -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,8 +65,7 @@ export const initDb = async (params: any) => { } //await await db.compact(); - globalThis.dbReady = true; - + const sync = PouchDB.sync( '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 }); // changes.cancel(); + + globalThis.dbReady = true; + }; diff --git a/src/db/settings.ts b/src/db/settings.ts index 8ff1b6f..c64a700 100644 --- a/src/db/settings.ts +++ b/src/db/settings.ts @@ -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; } }; diff --git a/src/lib/async-wait.ts b/src/lib/async-wait.ts new file mode 100644 index 0000000..1880057 --- /dev/null +++ b/src/lib/async-wait.ts @@ -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); + } +}; diff --git a/src/workers/dispatcher-worker.ts b/src/workers/dispatcher-worker.ts index 5baac38..8f6498d 100644 --- a/src/workers/dispatcher-worker.ts +++ b/src/workers/dispatcher-worker.ts @@ -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[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) => {