114 lines
2.9 KiB
TypeScript
114 lines
2.9 KiB
TypeScript
import { debounce, slice } from 'lodash';
|
|
import { getWpt } from '../db/wpt';
|
|
import { getParentId } from '../lib/docuri';
|
|
import { returnAgain } from '../workers/dispatcher-worker';
|
|
import { getAllGpxes, getGpx } from './gpx';
|
|
import { put } from './lib';
|
|
import { getOverlays } from './overlays';
|
|
import { getRte } from './rte';
|
|
import { getTrk } from './trk';
|
|
import { getTrkseg } from './trkseg';
|
|
|
|
declare global {
|
|
var watches: Map<string, any>;
|
|
}
|
|
|
|
const methods = {
|
|
getAllGpxes,
|
|
getGpx,
|
|
getTrk,
|
|
getTrkseg,
|
|
getWpt,
|
|
getRte,
|
|
getOverlays,
|
|
};
|
|
|
|
const sendUpdate = async (params: any) => {
|
|
if (params) {
|
|
console.log({ caller: 'ChangeHandler / sendUpdate', params });
|
|
try {
|
|
const { method, _dispatchId, id, ...otherParams } = params;
|
|
const returnValue = await methods[<keyof typeof methods>method](params);
|
|
if (returnValue) {
|
|
returnAgain(_dispatchId, returnValue);
|
|
}
|
|
} catch (error) {
|
|
console.error({ caller: 'ChangeHandler / sendUpdate', params, error });
|
|
}
|
|
}
|
|
};
|
|
|
|
let debouncedSendUpdates = <any>{};
|
|
|
|
for (const property in methods) {
|
|
debouncedSendUpdates[<keyof typeof debouncedSendUpdates>property] = debounce(
|
|
sendUpdate,
|
|
500
|
|
);
|
|
}
|
|
|
|
const changeHandler = async (change: any) => {
|
|
// console.log({ caller: 'ChangeHandler', change, watches: globalThis.watches });
|
|
const { id } = change;
|
|
if (!globalThis.watches) {
|
|
globalThis.watches = new Map();
|
|
}
|
|
sendUpdate(globalThis.watches.get(id));
|
|
|
|
const parentId = getParentId(id);
|
|
|
|
if (parentId === 'gpx') {
|
|
const gpxes = await getAllGpxes();
|
|
// console.log({ caller: 'changeHandler / gpxes', gpxes, id });
|
|
if (!gpxes.includes(id)) {
|
|
await put(
|
|
parentId,
|
|
`idx-${parentId}`,
|
|
(doc) => {
|
|
// console.log({ caller: 'changeHandler / gpxes', doc });
|
|
doc.push(id);
|
|
doc.reverse().sort().reverse();
|
|
return doc;
|
|
},
|
|
[]
|
|
);
|
|
}
|
|
}
|
|
|
|
const parentParams = globalThis.watches.get(parentId);
|
|
if (parentParams) {
|
|
debouncedSendUpdates[parentParams.method](parentParams);
|
|
}
|
|
};
|
|
|
|
export default changeHandler;
|
|
|
|
export const getAndWatch = async (params: any) => {
|
|
// console.log({
|
|
// caller: 'ChangeHandler / getAndWatch',
|
|
// params,
|
|
// watches: globalThis.watches,
|
|
// });
|
|
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);
|
|
// console.log({
|
|
// caller: 'ChangeHandler / getAndWatch',
|
|
// params,
|
|
// returnValue,
|
|
// watches: globalThis.watches,
|
|
// });
|
|
return returnValue;
|
|
};
|
|
|
|
export const cancelWatch = (params: any) => {
|
|
if (!globalThis.watches) {
|
|
globalThis.watches = new Map();
|
|
}
|
|
const { id } = params;
|
|
globalThis.watches.delete(id);
|
|
};
|