2022-11-26 18:36:55 +00:00
|
|
|
/// <reference lib="webworker" />
|
|
|
|
import { initDb } from '../db';
|
2022-12-25 15:30:47 +00:00
|
|
|
import { getAccounts, putAccount } from '../db/account';
|
2022-12-06 13:36:50 +00:00
|
|
|
import { cancelWatch, getAndWatch } from '../db/change-handler';
|
2022-11-26 18:36:55 +00:00
|
|
|
import {
|
|
|
|
putNewGpx,
|
|
|
|
existsGpx,
|
|
|
|
pruneAndSaveImportedGpx,
|
|
|
|
getGpx,
|
2022-12-12 17:55:14 +00:00
|
|
|
putGpx,
|
2022-12-13 19:59:39 +00:00
|
|
|
appendTrk,
|
2022-11-27 13:32:21 +00:00
|
|
|
getAllGpxes,
|
2022-12-12 10:41:09 +00:00
|
|
|
getAllGpxesWithSummary,
|
2022-11-26 18:36:55 +00:00
|
|
|
} from '../db/gpx';
|
2023-01-05 17:27:20 +00:00
|
|
|
import { putRte } from '../db/rte';
|
2023-01-07 14:41:16 +00:00
|
|
|
import { putRtept } from '../db/rtept';
|
2022-12-25 15:30:47 +00:00
|
|
|
import { getSettings, putSettings } from '../db/settings';
|
2022-12-13 21:26:17 +00:00
|
|
|
import { getState, setState } from '../db/state';
|
2023-01-08 21:05:08 +00:00
|
|
|
import { deleteTrk, getTrk, putNewTrk } from '../db/trk';
|
2023-01-07 17:46:14 +00:00
|
|
|
import { putTrkpt } from '../db/trkpt';
|
2022-12-13 19:59:39 +00:00
|
|
|
import { getTrkseg, appendTrkpt } from '../db/trkseg';
|
2022-12-06 20:27:25 +00:00
|
|
|
import { getWpt, putWpt } from '../db/wpt';
|
2022-12-25 20:37:59 +00:00
|
|
|
import { until } from '../lib/async-wait';
|
2022-11-26 18:36:55 +00:00
|
|
|
|
2022-11-27 13:32:21 +00:00
|
|
|
//const self = globalThis as unknown as WorkerGlobalScope;
|
2022-11-26 18:36:55 +00:00
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
console.log({ caller: 'dispatcher-worker' });
|
2022-11-26 18:36:55 +00:00
|
|
|
|
2022-12-25 20:37:59 +00:00
|
|
|
declare global {
|
|
|
|
var dbReady: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
globalThis.dbReady = false;
|
|
|
|
|
2022-11-26 18:36:55 +00:00
|
|
|
onmessage = async function (e) {
|
2022-12-06 13:36:50 +00:00
|
|
|
const actions = {
|
|
|
|
initDb,
|
|
|
|
putNewGpx,
|
|
|
|
putNewTrk,
|
|
|
|
existsGpx,
|
|
|
|
pruneAndSaveImportedGpx,
|
|
|
|
getAllGpxes,
|
2022-12-12 10:41:09 +00:00
|
|
|
getAllGpxesWithSummary,
|
2022-12-06 13:36:50 +00:00
|
|
|
getGpx,
|
2022-12-12 17:55:14 +00:00
|
|
|
putGpx,
|
2022-12-13 19:59:39 +00:00
|
|
|
appendTrk,
|
2022-12-06 13:36:50 +00:00
|
|
|
getTrk,
|
|
|
|
getTrkseg,
|
2022-12-13 19:59:39 +00:00
|
|
|
appendTrkpt,
|
2022-12-06 13:36:50 +00:00
|
|
|
getWpt,
|
|
|
|
|
2022-12-06 20:27:25 +00:00
|
|
|
putWpt,
|
2023-01-05 17:27:20 +00:00
|
|
|
putRte,
|
2023-01-07 14:41:16 +00:00
|
|
|
putRtept,
|
2023-01-07 17:46:14 +00:00
|
|
|
putTrkpt,
|
2023-01-07 14:41:16 +00:00
|
|
|
|
2023-01-08 21:05:08 +00:00
|
|
|
deleteTrk,
|
|
|
|
|
2022-12-13 21:26:17 +00:00
|
|
|
getState,
|
|
|
|
setState,
|
2022-12-06 20:27:25 +00:00
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
getAndWatch,
|
|
|
|
cancelWatch,
|
2022-12-25 15:30:47 +00:00
|
|
|
|
|
|
|
getSettings,
|
|
|
|
putSettings,
|
|
|
|
|
|
|
|
getAccounts,
|
|
|
|
putAccount,
|
2022-12-06 13:36:50 +00:00
|
|
|
};
|
|
|
|
|
2022-11-26 18:36:55 +00:00
|
|
|
console.log({ caller: 'dispatcher-worker / onmessage', e });
|
|
|
|
const { id, payload } = e.data;
|
|
|
|
var returnValue: any = 'unknownAction';
|
|
|
|
if (payload.action in actions) {
|
2022-12-25 20:37:59 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
2022-12-06 13:36:50 +00:00
|
|
|
returnValue = await actions[<keyof typeof actions>payload.action]({
|
|
|
|
...payload.params,
|
|
|
|
_dispatchId: id,
|
|
|
|
});
|
2022-11-26 18:36:55 +00:00
|
|
|
}
|
|
|
|
postMessage({ id: id, payload: returnValue });
|
2022-12-25 20:37:59 +00:00
|
|
|
console.log({
|
|
|
|
caller: 'dispatcher-worker / response sent',
|
|
|
|
id,
|
|
|
|
returnValue,
|
|
|
|
dbReady: globalThis.dbReady,
|
|
|
|
});
|
2022-11-26 18:36:55 +00:00
|
|
|
};
|
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
export const returnAgain = (id: number, returnValue: any) => {
|
|
|
|
console.log({ caller: 'dispatcher-worker / returnAgain', id, returnValue });
|
|
|
|
postMessage({ id: id, payload: returnValue });
|
|
|
|
};
|
|
|
|
|
2022-11-26 18:36:55 +00:00
|
|
|
//export default self;
|