diff --git a/src/db/change-handler.ts b/src/db/change-handler.ts index 91be022..75018de 100644 --- a/src/db/change-handler.ts +++ b/src/db/change-handler.ts @@ -1,4 +1,4 @@ -import { slice } from 'lodash'; +import { debounce, slice } from 'lodash'; import { getWpt } from '../db/wpt'; import { returnAgain } from '../workers/dispatcher-worker'; import { getAllGpxes, getGpx } from './gpx'; @@ -31,6 +31,15 @@ const sendUpdate = async (params: any) => { } }; +let debouncedSendUpdates = {}; + +for (const property in methods) { + debouncedSendUpdates[property] = debounce( + sendUpdate, + 500 + ); +} + const changeHandler = async (change: any) => { console.log({ caller: 'ChangeHandler', change, watches: globalThis.watches }); const { id } = change; @@ -69,7 +78,8 @@ const changeHandler = async (change: any) => { } } - sendUpdate(globalThis.watches.get(parentId)); + const parentParams = globalThis.watches.get(parentId); + debouncedSendUpdates[parentParams.method](parentParams); }; export default changeHandler;