2022-12-28 17:50:09 +00:00
|
|
|
import { debounce, slice } from 'lodash';
|
2022-12-06 13:36:50 +00:00
|
|
|
import { getWpt } from '../db/wpt';
|
|
|
|
import { returnAgain } from '../workers/dispatcher-worker';
|
2022-12-09 14:18:14 +00:00
|
|
|
import { getAllGpxes, getGpx } from './gpx';
|
2022-12-11 09:45:08 +00:00
|
|
|
import { put } from './lib';
|
2022-12-09 16:31:56 +00:00
|
|
|
import { getRte } from './rte';
|
2022-12-09 14:18:14 +00:00
|
|
|
import { getTrk } from './trk';
|
|
|
|
import { getTrkseg } from './trkseg';
|
2022-12-05 17:49:36 +00:00
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
declare global {
|
|
|
|
var watches: Map<string, any>;
|
2022-12-05 17:49:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
const methods = {
|
2022-12-09 13:57:54 +00:00
|
|
|
getAllGpxes,
|
2022-12-09 14:18:14 +00:00
|
|
|
getGpx,
|
|
|
|
getTrk,
|
|
|
|
getTrkseg,
|
2022-12-06 13:36:50 +00:00
|
|
|
getWpt,
|
2022-12-09 16:31:56 +00:00
|
|
|
getRte,
|
2022-12-06 13:36:50 +00:00
|
|
|
};
|
|
|
|
|
2022-12-09 13:57:54 +00:00
|
|
|
const sendUpdate = async (params: any) => {
|
2022-12-06 13:36:50 +00:00
|
|
|
if (params) {
|
2022-12-09 13:57:54 +00:00
|
|
|
console.log({ caller: 'ChangeHandler / sendUpdate', params });
|
2022-12-06 13:36:50 +00:00
|
|
|
const { method, _dispatchId, id, ...otherParams } = params;
|
|
|
|
const returnValue = await methods[<keyof typeof methods>method](params);
|
2022-12-06 20:27:25 +00:00
|
|
|
if (returnValue) {
|
2022-12-06 13:36:50 +00:00
|
|
|
returnAgain(_dispatchId, returnValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-28 17:50:09 +00:00
|
|
|
let debouncedSendUpdates = <any>{};
|
|
|
|
|
|
|
|
for (const property in methods) {
|
|
|
|
debouncedSendUpdates[<keyof typeof debouncedSendUpdates>property] = debounce(
|
|
|
|
sendUpdate,
|
|
|
|
500
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-09 13:57:54 +00:00
|
|
|
const changeHandler = async (change: any) => {
|
2022-12-13 19:59:39 +00:00
|
|
|
console.log({ caller: 'ChangeHandler', change, watches: globalThis.watches });
|
2022-12-09 13:57:54 +00:00
|
|
|
const { id } = change;
|
|
|
|
if (!globalThis.watches) {
|
|
|
|
globalThis.watches = new Map();
|
|
|
|
}
|
|
|
|
sendUpdate(globalThis.watches.get(id));
|
|
|
|
|
2022-12-13 19:59:39 +00:00
|
|
|
const tokens = id.split('/').slice(0, -1);
|
|
|
|
if (['1wpt', '2rte', '3trk'].includes(tokens.at(-1))) {
|
|
|
|
tokens.splice(-1);
|
|
|
|
}
|
|
|
|
const parentId = tokens.join('/');
|
|
|
|
console.log({
|
|
|
|
caller: 'ChangeHandler',
|
|
|
|
change,
|
|
|
|
watches: globalThis.watches,
|
|
|
|
parentId,
|
|
|
|
});
|
2022-12-11 09:45:08 +00:00
|
|
|
|
|
|
|
if (parentId === 'gpx') {
|
|
|
|
const gpxes = await getAllGpxes();
|
|
|
|
console.log({ caller: 'changeHandler / gpxes', gpxes, id });
|
2022-12-12 19:57:25 +00:00
|
|
|
if (!gpxes.includes(id)) {
|
2022-12-11 09:45:08 +00:00
|
|
|
await put(
|
|
|
|
parentId,
|
|
|
|
`idx-${parentId}`,
|
|
|
|
(doc) => {
|
|
|
|
console.log({ caller: 'changeHandler / gpxes', doc });
|
|
|
|
doc.push(id);
|
2022-12-11 15:20:34 +00:00
|
|
|
doc.reverse().sort().reverse();
|
2022-12-11 09:45:08 +00:00
|
|
|
return doc;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-28 17:50:09 +00:00
|
|
|
const parentParams = globalThis.watches.get(parentId);
|
|
|
|
debouncedSendUpdates[parentParams.method](parentParams);
|
2022-12-09 13:57:54 +00:00
|
|
|
};
|
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
export default changeHandler;
|
|
|
|
|
|
|
|
export const getAndWatch = async (params: any) => {
|
2022-12-13 19:59:39 +00:00
|
|
|
console.log({
|
|
|
|
caller: 'ChangeHandler / getAndWatch',
|
|
|
|
params,
|
|
|
|
watches: globalThis.watches,
|
|
|
|
});
|
2022-12-06 13:36:50 +00:00
|
|
|
const { method, _dispatchId, id, ...otherParams } = params;
|
|
|
|
if (!globalThis.watches) {
|
|
|
|
globalThis.watches = new Map();
|
|
|
|
}
|
|
|
|
globalThis.watches.set(id, params);
|
|
|
|
const returnValue = await methods[<keyof typeof methods>method](params);
|
2022-12-13 19:59:39 +00:00
|
|
|
console.log({
|
|
|
|
caller: 'ChangeHandler / getAndWatch',
|
|
|
|
params,
|
|
|
|
returnValue,
|
|
|
|
watches: globalThis.watches,
|
|
|
|
});
|
2022-12-06 13:36:50 +00:00
|
|
|
return returnValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const cancelWatch = (params: any) => {
|
|
|
|
if (!globalThis.watches) {
|
|
|
|
globalThis.watches = new Map();
|
|
|
|
}
|
|
|
|
const { id } = params;
|
|
|
|
globalThis.watches.delete(id);
|
|
|
|
};
|