@tanstack/db
Version:
A reactive client store for building super fast apps on sync
1 lines • 2.37 kB
Source Map (JSON)
{"version":3,"file":"debounceStrategy.cjs","sources":["../../../src/strategies/debounceStrategy.ts"],"sourcesContent":["import { LiteDebouncer } from '@tanstack/pacer-lite/lite-debouncer'\nimport type { DebounceStrategy, DebounceStrategyOptions } from './types'\nimport type { Transaction } from '../transactions'\n\n/**\n * Creates a debounce strategy that delays transaction execution until after\n * a period of inactivity.\n *\n * Ideal for scenarios like search inputs or auto-save fields where you want\n * to wait for the user to stop typing before persisting changes.\n *\n * @param options - Configuration for the debounce behavior\n * @returns A debounce strategy instance\n *\n * @example\n * ```ts\n * const mutate = usePacedMutations({\n * onMutate: (value) => {\n * collection.update(id, draft => { draft.value = value })\n * },\n * mutationFn: async ({ transaction }) => {\n * await api.save(transaction.mutations)\n * },\n * strategy: debounceStrategy({ wait: 500 })\n * })\n * ```\n */\nexport function debounceStrategy(\n options: DebounceStrategyOptions,\n): DebounceStrategy {\n const trailing = options.trailing ?? true\n const debouncer = new LiteDebouncer(\n (callback: () => Transaction) => callback(),\n {\n ...options,\n leading: options.leading ?? false,\n trailing,\n },\n )\n\n return {\n _type: `debounce`,\n options,\n execute: <T extends object = Record<string, unknown>>(\n fn: () => Transaction<T>,\n ) => {\n const execution = { happened: false }\n debouncer.maybeExecute(() => {\n execution.happened = true\n return (fn as () => Transaction)()\n })\n if (!trailing && !execution.happened) return false\n return\n },\n cleanup: () => {\n // Keep pending work scheduled until its quiet-period callback runs.\n },\n }\n}\n"],"names":["LiteDebouncer"],"mappings":";;;AA2BO,SAAS,iBACd,SACkB;AAClB,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,YAAY,IAAIA,cAAAA;AAAAA,IACpB,CAAC,aAAgC,SAAA;AAAA,IACjC;AAAA,MACE,GAAG;AAAA,MACH,SAAS,QAAQ,WAAW;AAAA,MAC5B;AAAA,IAAA;AAAA,EACF;AAGF,SAAO;AAAA,IACL,OAAO;AAAA,IACP;AAAA,IACA,SAAS,CACP,OACG;AACH,YAAM,YAAY,EAAE,UAAU,MAAA;AAC9B,gBAAU,aAAa,MAAM;AAC3B,kBAAU,WAAW;AACrB,eAAQ,GAAA;AAAA,MACV,CAAC;AACD,UAAI,CAAC,YAAY,CAAC,UAAU,SAAU,QAAO;AAC7C;AAAA,IACF;AAAA,IACA,SAAS,MAAM;AAAA,IAEf;AAAA,EAAA;AAEJ;;"}