@usefy/use-debounce 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -20,7 +20,7 @@ interface UseDebounceOptions {
20
20
  }
21
21
  /**
22
22
  * Debounces a value by delaying updates until after a specified delay period has elapsed
23
- * since the last time the value changed.
23
+ * since the last time the value changed. Useful for search inputs and API calls.
24
24
  *
25
25
  * @template T - The type of the value to debounce
26
26
  * @param value - The value to debounce
package/dist/index.d.ts CHANGED
@@ -20,7 +20,7 @@ interface UseDebounceOptions {
20
20
  }
21
21
  /**
22
22
  * Debounces a value by delaying updates until after a specified delay period has elapsed
23
- * since the last time the value changed.
23
+ * since the last time the value changed. Useful for search inputs and API calls.
24
24
  *
25
25
  * @template T - The type of the value to debounce
26
26
  * @param value - The value to debounce
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/useDebounce.ts"],"sourcesContent":["export { useDebounce, type UseDebounceOptions } from \"./useDebounce\";\r\n","import { useEffect, useRef, useState } from \"react\";\r\n\r\n/**\r\n * Options for useDebounce hook\r\n */\r\nexport interface UseDebounceOptions {\r\n /**\r\n * Maximum time the debounced value can be delayed\r\n * @default undefined (no maximum)\r\n */\r\n maxWait?: number;\r\n /**\r\n * Whether to update the debounced value on the leading edge\r\n * @default false\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to update the debounced value on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Debounces a value by delaying updates until after a specified delay period has elapsed\r\n * since the last time the value changed.\r\n *\r\n * @template T - The type of the value to debounce\r\n * @param value - The value to debounce\r\n * @param delay - The delay in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling debounce behavior\r\n * @returns The debounced value\r\n *\r\n * @example\r\n * ```tsx\r\n * function SearchInput() {\r\n * const [searchTerm, setSearchTerm] = useState('');\r\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\r\n *\r\n * useEffect(() => {\r\n * if (debouncedSearchTerm) {\r\n * // API call with debounced value\r\n * searchAPI(debouncedSearchTerm);\r\n * }\r\n * }, [debouncedSearchTerm]);\r\n *\r\n * return (\r\n * <input\r\n * type=\"text\"\r\n * value={searchTerm}\r\n * onChange={(e) => setSearchTerm(e.target.value)}\r\n * placeholder=\"Search...\"\r\n * />\r\n * );\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge update\r\n * const debouncedValue = useDebounce(value, 300, { leading: true });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With maximum wait time\r\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\r\n * ```\r\n */\r\nexport function useDebounce<T>(\r\n value: T,\r\n delay: number = 500,\r\n options: UseDebounceOptions = {}\r\n): T {\r\n // Parse options\r\n const wait = delay || 0;\r\n const leading = options.leading ?? false;\r\n const trailing = options.trailing !== undefined ? options.trailing : true;\r\n const maxing = \"maxWait\" in options;\r\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\r\n\r\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\r\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\r\n undefined\r\n );\r\n const lastCallTimeRef = useRef<number | undefined>(undefined);\r\n const lastInvokeTimeRef = useRef<number>(0);\r\n const lastValueRef = useRef<T>(value);\r\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\r\n\r\n // Store options in refs to access latest values in timer callbacks\r\n const waitRef = useRef(wait);\r\n const leadingRef = useRef(leading);\r\n const trailingRef = useRef(trailing);\r\n const maxingRef = useRef(maxing);\r\n const maxWaitRef = useRef(maxWait);\r\n\r\n // Update refs when options change\r\n waitRef.current = wait;\r\n leadingRef.current = leading;\r\n trailingRef.current = trailing;\r\n maxingRef.current = maxing;\r\n maxWaitRef.current = maxWait;\r\n\r\n // Helper function to get current time\r\n const now = () => Date.now();\r\n\r\n // Define helper functions using refs for latest values\r\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\r\n const invokeRef = useRef<(time: number) => void>(() => {});\r\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\r\n const timerExpiredRef = useRef<() => void>(() => {});\r\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\r\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\r\n\r\n // Helper function: shouldInvoke\r\n shouldInvokeRef.current = (time: number): boolean => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return true; // First call\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n\r\n return (\r\n timeSinceLastCall >= waitRef.current ||\r\n timeSinceLastCall < 0 || // System time went backwards\r\n (maxingRef.current &&\r\n timeSinceLastInvoke >= (maxWaitRef.current as number))\r\n );\r\n };\r\n\r\n // Helper function: invokeFunc\r\n invokeRef.current = (time: number): void => {\r\n setDebouncedValue(lastValueRef.current);\r\n lastInvokeTimeRef.current = time;\r\n };\r\n\r\n // Helper function: remainingWait\r\n remainingWaitRef.current = (time: number): number => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return waitRef.current;\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n const timeWaiting = waitRef.current - timeSinceLastCall;\r\n\r\n return maxingRef.current\r\n ? Math.min(\r\n timeWaiting,\r\n (maxWaitRef.current as number) - timeSinceLastInvoke\r\n )\r\n : timeWaiting;\r\n };\r\n\r\n // Helper function: trailingEdge\r\n trailingEdgeRef.current = (time: number): void => {\r\n timerIdRef.current = undefined;\r\n\r\n // Only invoke if we have `lastValueRef.current` which means `value` has been\r\n // debounced at least once.\r\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Helper function: timerExpired\r\n timerExpiredRef.current = (): void => {\r\n const time = now();\r\n if (shouldInvokeRef.current(time)) {\r\n trailingEdgeRef.current(time);\r\n } else {\r\n // Restart the timer.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n remainingWaitRef.current(time)\r\n );\r\n }\r\n };\r\n\r\n // Helper function: leadingEdge\r\n leadingEdgeRef.current = (time: number): void => {\r\n // Reset any `maxWait` timer.\r\n lastInvokeTimeRef.current = time;\r\n // Start the timer for the trailing edge.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n // Invoke the leading edge.\r\n if (leadingRef.current) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Cleanup on unmount only\r\n useEffect(() => {\r\n return () => {\r\n if (timerIdRef.current !== undefined) {\r\n clearTimeout(timerIdRef.current);\r\n }\r\n };\r\n }, []);\r\n\r\n // Main debounce effect - runs when value changes\r\n useEffect(() => {\r\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\r\n if (Object.is(prevValueRef.current, value)) {\r\n return;\r\n }\r\n prevValueRef.current = value;\r\n\r\n const time = now();\r\n const isInvoking = shouldInvokeRef.current(time);\r\n\r\n // Update lastValueRef with current value\r\n lastValueRef.current = value;\r\n lastCallTimeRef.current = time;\r\n\r\n if (isInvoking) {\r\n if (timerIdRef.current === undefined) {\r\n leadingEdgeRef.current(time);\r\n } else if (maxingRef.current) {\r\n // Handle invocations in a tight loop.\r\n clearTimeout(timerIdRef.current);\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n invokeRef.current(time);\r\n }\r\n } else {\r\n if (timerIdRef.current === undefined) {\r\n // Start timer with wait\r\n // remainingWait is only used inside timerExpired for restarting\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n }\r\n }\r\n // No cleanup here - timer should persist across value changes\r\n // This is the key difference from the previous implementation\r\n }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;AAAA,EACF;AAGA,8BAAU,MAAM;AACd,WAAO,MAAM;AACX,UAAI,WAAW,YAAY,QAAW;AACpC,qBAAa,WAAW,OAAO;AAAA,MACjC;AAAA,IACF;AAAA,EACF,GAAG,CAAC,CAAC;AAGL,8BAAU,MAAM;AAEd,QAAI,OAAO,GAAG,aAAa,SAAS,KAAK,GAAG;AAC1C;AAAA,IACF;AACA,iBAAa,UAAU;AAEvB,UAAM,OAAO,IAAI;AACjB,UAAM,aAAa,gBAAgB,QAAQ,IAAI;AAG/C,iBAAa,UAAU;AACvB,oBAAgB,UAAU;AAE1B,QAAI,YAAY;AACd,UAAI,WAAW,YAAY,QAAW;AACpC,uBAAe,QAAQ,IAAI;AAAA,MAC7B,WAAW,UAAU,SAAS;AAE5B,qBAAa,WAAW,OAAO;AAC/B,mBAAW,UAAU;AAAA,UACnB,MAAM,gBAAgB,QAAQ;AAAA,UAC9B,QAAQ;AAAA,QACV;AACA,kBAAU,QAAQ,IAAI;AAAA,MACxB;AAAA,IACF,OAAO;AACL,UAAI,WAAW,YAAY,QAAW;AAGpC,mBAAW,UAAU;AAAA,UACnB,MAAM,gBAAgB,QAAQ;AAAA,UAC9B,QAAQ;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EAGF,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/useDebounce.ts"],"sourcesContent":["export { useDebounce, type UseDebounceOptions } from \"./useDebounce\";\r\n","import { useEffect, useRef, useState } from \"react\";\r\n\r\n/**\r\n * Options for useDebounce hook\r\n */\r\nexport interface UseDebounceOptions {\r\n /**\r\n * Maximum time the debounced value can be delayed\r\n * @default undefined (no maximum)\r\n */\r\n maxWait?: number;\r\n /**\r\n * Whether to update the debounced value on the leading edge\r\n * @default false\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to update the debounced value on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Debounces a value by delaying updates until after a specified delay period has elapsed\r\n * since the last time the value changed. Useful for search inputs and API calls.\r\n *\r\n * @template T - The type of the value to debounce\r\n * @param value - The value to debounce\r\n * @param delay - The delay in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling debounce behavior\r\n * @returns The debounced value\r\n *\r\n * @example\r\n * ```tsx\r\n * function SearchInput() {\r\n * const [searchTerm, setSearchTerm] = useState('');\r\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\r\n *\r\n * useEffect(() => {\r\n * if (debouncedSearchTerm) {\r\n * // API call with debounced value\r\n * searchAPI(debouncedSearchTerm);\r\n * }\r\n * }, [debouncedSearchTerm]);\r\n *\r\n * return (\r\n * <input\r\n * type=\"text\"\r\n * value={searchTerm}\r\n * onChange={(e) => setSearchTerm(e.target.value)}\r\n * placeholder=\"Search...\"\r\n * />\r\n * );\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge update\r\n * const debouncedValue = useDebounce(value, 300, { leading: true });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With maximum wait time\r\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\r\n * ```\r\n */\r\nexport function useDebounce<T>(\r\n value: T,\r\n delay: number = 500,\r\n options: UseDebounceOptions = {}\r\n): T {\r\n // Parse options\r\n const wait = delay || 0;\r\n const leading = options.leading ?? false;\r\n const trailing = options.trailing !== undefined ? options.trailing : true;\r\n const maxing = \"maxWait\" in options;\r\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\r\n\r\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\r\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\r\n undefined\r\n );\r\n const lastCallTimeRef = useRef<number | undefined>(undefined);\r\n const lastInvokeTimeRef = useRef<number>(0);\r\n const lastValueRef = useRef<T>(value);\r\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\r\n\r\n // Store options in refs to access latest values in timer callbacks\r\n const waitRef = useRef(wait);\r\n const leadingRef = useRef(leading);\r\n const trailingRef = useRef(trailing);\r\n const maxingRef = useRef(maxing);\r\n const maxWaitRef = useRef(maxWait);\r\n\r\n // Update refs when options change\r\n waitRef.current = wait;\r\n leadingRef.current = leading;\r\n trailingRef.current = trailing;\r\n maxingRef.current = maxing;\r\n maxWaitRef.current = maxWait;\r\n\r\n // Helper function to get current time\r\n const now = () => Date.now();\r\n\r\n // Define helper functions using refs for latest values\r\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\r\n const invokeRef = useRef<(time: number) => void>(() => {});\r\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\r\n const timerExpiredRef = useRef<() => void>(() => {});\r\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\r\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\r\n\r\n // Helper function: shouldInvoke\r\n shouldInvokeRef.current = (time: number): boolean => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return true; // First call\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n\r\n return (\r\n timeSinceLastCall >= waitRef.current ||\r\n timeSinceLastCall < 0 || // System time went backwards\r\n (maxingRef.current &&\r\n timeSinceLastInvoke >= (maxWaitRef.current as number))\r\n );\r\n };\r\n\r\n // Helper function: invokeFunc\r\n invokeRef.current = (time: number): void => {\r\n setDebouncedValue(lastValueRef.current);\r\n lastInvokeTimeRef.current = time;\r\n };\r\n\r\n // Helper function: remainingWait\r\n remainingWaitRef.current = (time: number): number => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return waitRef.current;\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n const timeWaiting = waitRef.current - timeSinceLastCall;\r\n\r\n return maxingRef.current\r\n ? Math.min(\r\n timeWaiting,\r\n (maxWaitRef.current as number) - timeSinceLastInvoke\r\n )\r\n : timeWaiting;\r\n };\r\n\r\n // Helper function: trailingEdge\r\n trailingEdgeRef.current = (time: number): void => {\r\n timerIdRef.current = undefined;\r\n\r\n // Only invoke if we have `lastValueRef.current` which means `value` has been\r\n // debounced at least once.\r\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Helper function: timerExpired\r\n timerExpiredRef.current = (): void => {\r\n const time = now();\r\n if (shouldInvokeRef.current(time)) {\r\n trailingEdgeRef.current(time);\r\n } else {\r\n // Restart the timer.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n remainingWaitRef.current(time)\r\n );\r\n }\r\n };\r\n\r\n // Helper function: leadingEdge\r\n leadingEdgeRef.current = (time: number): void => {\r\n // Reset any `maxWait` timer.\r\n lastInvokeTimeRef.current = time;\r\n // Start the timer for the trailing edge.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n // Invoke the leading edge.\r\n if (leadingRef.current) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Cleanup on unmount only\r\n useEffect(() => {\r\n return () => {\r\n if (timerIdRef.current !== undefined) {\r\n clearTimeout(timerIdRef.current);\r\n }\r\n };\r\n }, []);\r\n\r\n // Main debounce effect - runs when value changes\r\n useEffect(() => {\r\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\r\n if (Object.is(prevValueRef.current, value)) {\r\n return;\r\n }\r\n prevValueRef.current = value;\r\n\r\n const time = now();\r\n const isInvoking = shouldInvokeRef.current(time);\r\n\r\n // Update lastValueRef with current value\r\n lastValueRef.current = value;\r\n lastCallTimeRef.current = time;\r\n\r\n if (isInvoking) {\r\n if (timerIdRef.current === undefined) {\r\n leadingEdgeRef.current(time);\r\n } else if (maxingRef.current) {\r\n // Handle invocations in a tight loop.\r\n clearTimeout(timerIdRef.current);\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n invokeRef.current(time);\r\n }\r\n } else {\r\n if (timerIdRef.current === undefined) {\r\n // Start timer with wait\r\n // remainingWait is only used inside timerExpired for restarting\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n }\r\n }\r\n // No cleanup here - timer should persist across value changes\r\n // This is the key difference from the previous implementation\r\n }, [value]);\r\n\r\n return debouncedValue;\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA4C;AAqErC,SAAS,YACd,OACA,QAAgB,KAChB,UAA8B,CAAC,GAC5B;AAEH,QAAM,OAAO,SAAS;AACtB,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,WAAW,QAAQ,aAAa,SAAY,QAAQ,WAAW;AACrE,QAAM,SAAS,aAAa;AAC5B,QAAM,UAAU,SAAS,KAAK,IAAI,QAAQ,WAAW,GAAG,IAAI,IAAI;AAEhE,QAAM,CAAC,gBAAgB,iBAAiB,QAAI,uBAAY,KAAK;AAC7D,QAAM,iBAAa;AAAA,IACjB;AAAA,EACF;AACA,QAAM,sBAAkB,qBAA2B,MAAS;AAC5D,QAAM,wBAAoB,qBAAe,CAAC;AAC1C,QAAM,mBAAe,qBAAU,KAAK;AACpC,QAAM,mBAAe,qBAAU,KAAK;AAGpC,QAAM,cAAU,qBAAO,IAAI;AAC3B,QAAM,iBAAa,qBAAO,OAAO;AACjC,QAAM,kBAAc,qBAAO,QAAQ;AACnC,QAAM,gBAAY,qBAAO,MAAM;AAC/B,QAAM,iBAAa,qBAAO,OAAO;AAGjC,UAAQ,UAAU;AAClB,aAAW,UAAU;AACrB,cAAY,UAAU;AACtB,YAAU,UAAU;AACpB,aAAW,UAAU;AAGrB,QAAM,MAAM,MAAM,KAAK,IAAI;AAG3B,QAAM,sBAAkB,qBAAkC,MAAM,KAAK;AACrE,QAAM,gBAAY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;AAAA,QAC9B,iBAAiB,QAAQ,IAAI;AAAA,MAC/B;AAAA,IACF;AAAA,EACF;AAGA,iBAAe,UAAU,CAAC,SAAuB;AAE/C,sBAAkB,UAAU;AAE5B,eAAW,UAAU;AAAA,MACnB,MAAM,gBAAgB,QAAQ;AAAA,MAC9B,QAAQ;AAAA,IACV;AAEA,QAAI,WAAW,SAAS;AACtB,gBAAU,QAAQ,IAAI;AAAA,IACxB;AAAA,EACF;AAGA,8BAAU,MAAM;AACd,WAAO,MAAM;AACX,UAAI,WAAW,YAAY,QAAW;AACpC,qBAAa,WAAW,OAAO;AAAA,MACjC;AAAA,IACF;AAAA,EACF,GAAG,CAAC,CAAC;AAGL,8BAAU,MAAM;AAEd,QAAI,OAAO,GAAG,aAAa,SAAS,KAAK,GAAG;AAC1C;AAAA,IACF;AACA,iBAAa,UAAU;AAEvB,UAAM,OAAO,IAAI;AACjB,UAAM,aAAa,gBAAgB,QAAQ,IAAI;AAG/C,iBAAa,UAAU;AACvB,oBAAgB,UAAU;AAE1B,QAAI,YAAY;AACd,UAAI,WAAW,YAAY,QAAW;AACpC,uBAAe,QAAQ,IAAI;AAAA,MAC7B,WAAW,UAAU,SAAS;AAE5B,qBAAa,WAAW,OAAO;AAC/B,mBAAW,UAAU;AAAA,UACnB,MAAM,gBAAgB,QAAQ;AAAA,UAC9B,QAAQ;AAAA,QACV;AACA,kBAAU,QAAQ,IAAI;AAAA,MACxB;AAAA,IACF,OAAO;AACL,UAAI,WAAW,YAAY,QAAW;AAGpC,mBAAW,UAAU;AAAA,UACnB,MAAM,gBAAgB,QAAQ;AAAA,UAC9B,QAAQ;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EAGF,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useDebounce.ts"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\r\n\r\n/**\r\n * Options for useDebounce hook\r\n */\r\nexport interface UseDebounceOptions {\r\n /**\r\n * Maximum time the debounced value can be delayed\r\n * @default undefined (no maximum)\r\n */\r\n maxWait?: number;\r\n /**\r\n * Whether to update the debounced value on the leading edge\r\n * @default false\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to update the debounced value on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Debounces a value by delaying updates until after a specified delay period has elapsed\r\n * since the last time the value changed.\r\n *\r\n * @template T - The type of the value to debounce\r\n * @param value - The value to debounce\r\n * @param delay - The delay in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling debounce behavior\r\n * @returns The debounced value\r\n *\r\n * @example\r\n * ```tsx\r\n * function SearchInput() {\r\n * const [searchTerm, setSearchTerm] = useState('');\r\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\r\n *\r\n * useEffect(() => {\r\n * if (debouncedSearchTerm) {\r\n * // API call with debounced value\r\n * searchAPI(debouncedSearchTerm);\r\n * }\r\n * }, [debouncedSearchTerm]);\r\n *\r\n * return (\r\n * <input\r\n * type=\"text\"\r\n * value={searchTerm}\r\n * onChange={(e) => setSearchTerm(e.target.value)}\r\n * placeholder=\"Search...\"\r\n * />\r\n * );\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge update\r\n * const debouncedValue = useDebounce(value, 300, { leading: true });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With maximum wait time\r\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\r\n * ```\r\n */\r\nexport function useDebounce<T>(\r\n value: T,\r\n delay: number = 500,\r\n options: UseDebounceOptions = {}\r\n): T {\r\n // Parse options\r\n const wait = delay || 0;\r\n const leading = options.leading ?? false;\r\n const trailing = options.trailing !== undefined ? options.trailing : true;\r\n const maxing = \"maxWait\" in options;\r\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\r\n\r\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\r\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\r\n undefined\r\n );\r\n const lastCallTimeRef = useRef<number | undefined>(undefined);\r\n const lastInvokeTimeRef = useRef<number>(0);\r\n const lastValueRef = useRef<T>(value);\r\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\r\n\r\n // Store options in refs to access latest values in timer callbacks\r\n const waitRef = useRef(wait);\r\n const leadingRef = useRef(leading);\r\n const trailingRef = useRef(trailing);\r\n const maxingRef = useRef(maxing);\r\n const maxWaitRef = useRef(maxWait);\r\n\r\n // Update refs when options change\r\n waitRef.current = wait;\r\n leadingRef.current = leading;\r\n trailingRef.current = trailing;\r\n maxingRef.current = maxing;\r\n maxWaitRef.current = maxWait;\r\n\r\n // Helper function to get current time\r\n const now = () => Date.now();\r\n\r\n // Define helper functions using refs for latest values\r\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\r\n const invokeRef = useRef<(time: number) => void>(() => {});\r\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\r\n const timerExpiredRef = useRef<() => void>(() => {});\r\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\r\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\r\n\r\n // Helper function: shouldInvoke\r\n shouldInvokeRef.current = (time: number): boolean => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return true; // First call\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n\r\n return (\r\n timeSinceLastCall >= waitRef.current ||\r\n timeSinceLastCall < 0 || // System time went backwards\r\n (maxingRef.current &&\r\n timeSinceLastInvoke >= (maxWaitRef.current as number))\r\n );\r\n };\r\n\r\n // Helper function: invokeFunc\r\n invokeRef.current = (time: number): void => {\r\n setDebouncedValue(lastValueRef.current);\r\n lastInvokeTimeRef.current = time;\r\n };\r\n\r\n // Helper function: remainingWait\r\n remainingWaitRef.current = (time: number): number => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return waitRef.current;\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n const timeWaiting = waitRef.current - timeSinceLastCall;\r\n\r\n return maxingRef.current\r\n ? Math.min(\r\n timeWaiting,\r\n (maxWaitRef.current as number) - timeSinceLastInvoke\r\n )\r\n : timeWaiting;\r\n };\r\n\r\n // Helper function: trailingEdge\r\n trailingEdgeRef.current = (time: number): void => {\r\n timerIdRef.current = undefined;\r\n\r\n // Only invoke if we have `lastValueRef.current` which means `value` has been\r\n // debounced at least once.\r\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Helper function: timerExpired\r\n timerExpiredRef.current = (): void => {\r\n const time = now();\r\n if (shouldInvokeRef.current(time)) {\r\n trailingEdgeRef.current(time);\r\n } else {\r\n // Restart the timer.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n remainingWaitRef.current(time)\r\n );\r\n }\r\n };\r\n\r\n // Helper function: leadingEdge\r\n leadingEdgeRef.current = (time: number): void => {\r\n // Reset any `maxWait` timer.\r\n lastInvokeTimeRef.current = time;\r\n // Start the timer for the trailing edge.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n // Invoke the leading edge.\r\n if (leadingRef.current) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Cleanup on unmount only\r\n useEffect(() => {\r\n return () => {\r\n if (timerIdRef.current !== undefined) {\r\n clearTimeout(timerIdRef.current);\r\n }\r\n };\r\n }, []);\r\n\r\n // Main debounce effect - runs when value changes\r\n useEffect(() => {\r\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\r\n if (Object.is(prevValueRef.current, value)) {\r\n return;\r\n }\r\n prevValueRef.current = value;\r\n\r\n const time = now();\r\n const isInvoking = shouldInvokeRef.current(time);\r\n\r\n // Update lastValueRef with current value\r\n lastValueRef.current = value;\r\n lastCallTimeRef.current = time;\r\n\r\n if (isInvoking) {\r\n if (timerIdRef.current === undefined) {\r\n leadingEdgeRef.current(time);\r\n } else if (maxingRef.current) {\r\n // Handle invocations in a tight loop.\r\n clearTimeout(timerIdRef.current);\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n invokeRef.current(time);\r\n }\r\n } else {\r\n if (timerIdRef.current === undefined) {\r\n // Start timer with wait\r\n // remainingWait is only used inside timerExpired for restarting\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n }\r\n }\r\n // No cleanup here - timer should persist across value changes\r\n // This is the key difference from the previous implementation\r\n }, [value]);\r\n\r\n return debouncedValue;\r\n}\r\n"],"mappings":";AAAA,SAAS,WAAW,QAAQ,gBAAgB;AAqErC,SAAS,YACd,OACA,QAAgB,KAChB,UAA8B,CAAC,GAC5B;AAEH,QAAM,OAAO,SAAS;AACtB,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,WAAW,QAAQ,aAAa,SAAY,QAAQ,WAAW;AACrE,QAAM,SAAS,aAAa;AAC5B,QAAM,UAAU,SAAS,KAAK,IAAI,QAAQ,WAAW,GAAG,IAAI,IAAI;AAEhE,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAY,KAAK;AAC7D,QAAM,aAAa;AAAA,IACjB;AAAA,EACF;AACA,QAAM,kBAAkB,OAA2B,MAAS;AAC5D,QAAM,oBAAoB,OAAe,CAAC;AAC1C,QAAM,eAAe,OAAU,KAAK;AACpC,QAAM,eAAe,OAAU,KAAK;AAGpC,QAAM,UAAU,OAAO,IAAI;AAC3B,QAAM,aAAa,OAAO,OAAO;AACjC,QAAM,cAAc,OAAO,QAAQ;AACnC,QAAM,YAAY,OAAO,MAAM;AAC/B,QAAM,aAAa,OAAO,OAAO;AAGjC,UAAQ,UAAU;AAClB,aA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EV,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/useDebounce.ts"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\r\n\r\n/**\r\n * Options for useDebounce hook\r\n */\r\nexport interface UseDebounceOptions {\r\n /**\r\n * Maximum time the debounced value can be delayed\r\n * @default undefined (no maximum)\r\n */\r\n maxWait?: number;\r\n /**\r\n * Whether to update the debounced value on the leading edge\r\n * @default false\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to update the debounced value on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Debounces a value by delaying updates until after a specified delay period has elapsed\r\n * since the last time the value changed. Useful for search inputs and API calls.\r\n *\r\n * @template T - The type of the value to debounce\r\n * @param value - The value to debounce\r\n * @param delay - The delay in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling debounce behavior\r\n * @returns The debounced value\r\n *\r\n * @example\r\n * ```tsx\r\n * function SearchInput() {\r\n * const [searchTerm, setSearchTerm] = useState('');\r\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\r\n *\r\n * useEffect(() => {\r\n * if (debouncedSearchTerm) {\r\n * // API call with debounced value\r\n * searchAPI(debouncedSearchTerm);\r\n * }\r\n * }, [debouncedSearchTerm]);\r\n *\r\n * return (\r\n * <input\r\n * type=\"text\"\r\n * value={searchTerm}\r\n * onChange={(e) => setSearchTerm(e.target.value)}\r\n * placeholder=\"Search...\"\r\n * />\r\n * );\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge update\r\n * const debouncedValue = useDebounce(value, 300, { leading: true });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With maximum wait time\r\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\r\n * ```\r\n */\r\nexport function useDebounce<T>(\r\n value: T,\r\n delay: number = 500,\r\n options: UseDebounceOptions = {}\r\n): T {\r\n // Parse options\r\n const wait = delay || 0;\r\n const leading = options.leading ?? false;\r\n const trailing = options.trailing !== undefined ? options.trailing : true;\r\n const maxing = \"maxWait\" in options;\r\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\r\n\r\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\r\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\r\n undefined\r\n );\r\n const lastCallTimeRef = useRef<number | undefined>(undefined);\r\n const lastInvokeTimeRef = useRef<number>(0);\r\n const lastValueRef = useRef<T>(value);\r\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\r\n\r\n // Store options in refs to access latest values in timer callbacks\r\n const waitRef = useRef(wait);\r\n const leadingRef = useRef(leading);\r\n const trailingRef = useRef(trailing);\r\n const maxingRef = useRef(maxing);\r\n const maxWaitRef = useRef(maxWait);\r\n\r\n // Update refs when options change\r\n waitRef.current = wait;\r\n leadingRef.current = leading;\r\n trailingRef.current = trailing;\r\n maxingRef.current = maxing;\r\n maxWaitRef.current = maxWait;\r\n\r\n // Helper function to get current time\r\n const now = () => Date.now();\r\n\r\n // Define helper functions using refs for latest values\r\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\r\n const invokeRef = useRef<(time: number) => void>(() => {});\r\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\r\n const timerExpiredRef = useRef<() => void>(() => {});\r\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\r\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\r\n\r\n // Helper function: shouldInvoke\r\n shouldInvokeRef.current = (time: number): boolean => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return true; // First call\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n\r\n return (\r\n timeSinceLastCall >= waitRef.current ||\r\n timeSinceLastCall < 0 || // System time went backwards\r\n (maxingRef.current &&\r\n timeSinceLastInvoke >= (maxWaitRef.current as number))\r\n );\r\n };\r\n\r\n // Helper function: invokeFunc\r\n invokeRef.current = (time: number): void => {\r\n setDebouncedValue(lastValueRef.current);\r\n lastInvokeTimeRef.current = time;\r\n };\r\n\r\n // Helper function: remainingWait\r\n remainingWaitRef.current = (time: number): number => {\r\n const lastCallTime = lastCallTimeRef.current;\r\n if (lastCallTime === undefined) {\r\n return waitRef.current;\r\n }\r\n\r\n const timeSinceLastCall = time - lastCallTime;\r\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\r\n const timeWaiting = waitRef.current - timeSinceLastCall;\r\n\r\n return maxingRef.current\r\n ? Math.min(\r\n timeWaiting,\r\n (maxWaitRef.current as number) - timeSinceLastInvoke\r\n )\r\n : timeWaiting;\r\n };\r\n\r\n // Helper function: trailingEdge\r\n trailingEdgeRef.current = (time: number): void => {\r\n timerIdRef.current = undefined;\r\n\r\n // Only invoke if we have `lastValueRef.current` which means `value` has been\r\n // debounced at least once.\r\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Helper function: timerExpired\r\n timerExpiredRef.current = (): void => {\r\n const time = now();\r\n if (shouldInvokeRef.current(time)) {\r\n trailingEdgeRef.current(time);\r\n } else {\r\n // Restart the timer.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n remainingWaitRef.current(time)\r\n );\r\n }\r\n };\r\n\r\n // Helper function: leadingEdge\r\n leadingEdgeRef.current = (time: number): void => {\r\n // Reset any `maxWait` timer.\r\n lastInvokeTimeRef.current = time;\r\n // Start the timer for the trailing edge.\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n // Invoke the leading edge.\r\n if (leadingRef.current) {\r\n invokeRef.current(time);\r\n }\r\n };\r\n\r\n // Cleanup on unmount only\r\n useEffect(() => {\r\n return () => {\r\n if (timerIdRef.current !== undefined) {\r\n clearTimeout(timerIdRef.current);\r\n }\r\n };\r\n }, []);\r\n\r\n // Main debounce effect - runs when value changes\r\n useEffect(() => {\r\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\r\n if (Object.is(prevValueRef.current, value)) {\r\n return;\r\n }\r\n prevValueRef.current = value;\r\n\r\n const time = now();\r\n const isInvoking = shouldInvokeRef.current(time);\r\n\r\n // Update lastValueRef with current value\r\n lastValueRef.current = value;\r\n lastCallTimeRef.current = time;\r\n\r\n if (isInvoking) {\r\n if (timerIdRef.current === undefined) {\r\n leadingEdgeRef.current(time);\r\n } else if (maxingRef.current) {\r\n // Handle invocations in a tight loop.\r\n clearTimeout(timerIdRef.current);\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n invokeRef.current(time);\r\n }\r\n } else {\r\n if (timerIdRef.current === undefined) {\r\n // Start timer with wait\r\n // remainingWait is only used inside timerExpired for restarting\r\n timerIdRef.current = setTimeout(\r\n () => timerExpiredRef.current(),\r\n waitRef.current\r\n );\r\n }\r\n }\r\n // No cleanup here - timer should persist across value changes\r\n // This is the key difference from the previous implementation\r\n }, [value]);\r\n\r\n return debouncedValue;\r\n}\r\n"],"mappings":";AAAA,SAAS,WAAW,QAAQ,gBAAgB;AAqErC,SAAS,YACd,OACA,QAAgB,KAChB,UAA8B,CAAC,GAC5B;AAEH,QAAM,OAAO,SAAS;AACtB,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,WAAW,QAAQ,aAAa,SAAY,QAAQ,WAAW;AACrE,QAAM,SAAS,aAAa;AAC5B,QAAM,UAAU,SAAS,KAAK,IAAI,QAAQ,WAAW,GAAG,IAAI,IAAI;AAEhE,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAY,KAAK;AAC7D,QAAM,aAAa;AAAA,IACjB;AAAA,EACF;AACA,QAAM,kBAAkB,OAA2B,MAAS;AAC5D,QAAM,oBAAoB,OAAe,CAAC;AAC1C,QAAM,eAAe,OAAU,KAAK;AACpC,QAAM,eAAe,OAAU,KAAK;AAGpC,QAAM,UAAU,OAAO,IAAI;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,QAAW;AAGpC,mBAAW,UAAU;AAAA,UACnB,MAAM,gBAAgB,QAAQ;AAAA,UAC9B,QAAQ;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAAA,EAGF,GAAG,CAAC,KAAK,CAAC;AAEV,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,15 +1,27 @@
1
1
  {
2
2
  "name": "@usefy/use-debounce",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "A React hook for value debouncing with leading/trailing edge and maxWait options",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
7
  "types": "./dist/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
- "types": "./dist/index.d.ts",
11
- "import": "./dist/index.mjs",
12
- "require": "./dist/index.js"
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ }
18
+ }
19
+ },
20
+ "typesVersions": {
21
+ "*": {
22
+ "*": [
23
+ "./dist/*"
24
+ ]
13
25
  }
14
26
  },
15
27
  "files": [