2022-12-06 13:36:50 +00:00
|
|
|
import { getWpt } from '../db/wpt';
|
|
|
|
import { returnAgain } from '../workers/dispatcher-worker';
|
2022-12-09 13:57:54 +00:00
|
|
|
import { getAllGpxes } from './gpx';
|
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-06 13:36:50 +00:00
|
|
|
getWpt,
|
|
|
|
};
|
|
|
|
|
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-09 13:57:54 +00:00
|
|
|
const changeHandler = async (change: any) => {
|
|
|
|
console.log({ caller: 'ChangeHandler', change });
|
|
|
|
const { id } = change;
|
|
|
|
if (!globalThis.watches) {
|
|
|
|
globalThis.watches = new Map();
|
|
|
|
}
|
|
|
|
sendUpdate(globalThis.watches.get(id));
|
|
|
|
|
|
|
|
const parentId = id.substring(0, id.lastIndexOf('/'));
|
|
|
|
sendUpdate(globalThis.watches.get(parentId));
|
|
|
|
};
|
|
|
|
|
2022-12-06 13:36:50 +00:00
|
|
|
export default changeHandler;
|
|
|
|
|
|
|
|
export const getAndWatch = async (params: any) => {
|
|
|
|
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);
|
|
|
|
return returnValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const cancelWatch = (params: any) => {
|
|
|
|
if (!globalThis.watches) {
|
|
|
|
globalThis.watches = new Map();
|
|
|
|
}
|
|
|
|
const { id } = params;
|
|
|
|
globalThis.watches.delete(id);
|
|
|
|
};
|