import { getWpt } from '../db/wpt'; import { returnAgain } from '../workers/dispatcher-worker'; declare global { var watches: Map; } const methods = { getWpt, }; const changeHandler = async (change: any) => { console.log({ caller: 'ChangeHandler', change }); const { id } = change.id; if (!globalThis.watches) { globalThis.watches = new Map(); } const params = globalThis.watches.get(id); if (params) { const { method, _dispatchId, id, ...otherParams } = params; const returnValue = await methods[method](params); if (false) { returnAgain(_dispatchId, returnValue); } } }; 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[method](params); return returnValue; }; export const cancelWatch = (params: any) => { if (!globalThis.watches) { globalThis.watches = new Map(); } const { id } = params; globalThis.watches.delete(id); };