@usefy/use-debounce 0.0.21 → 0.0.23

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/README.md CHANGED
@@ -31,6 +31,12 @@
31
31
  <a href="#license">License</a>
32
32
  </p>
33
33
 
34
+ <p align="center">
35
+ <a href="https://geon0529.github.io/usefy/?path=/docs/hooks-usedebounce--docs" target="_blank" rel="noopener noreferrer">
36
+ <strong>📚 View Storybook Demo</strong>
37
+ </a>
38
+ </p>
39
+
34
40
  ---
35
41
 
36
42
  ## Overview
@@ -349,12 +355,7 @@ This package maintains comprehensive test coverage to ensure reliability and sta
349
355
 
350
356
  ### Test Coverage
351
357
 
352
- | Category | Coverage |
353
- | ---------- | -------------- |
354
- | Statements | 91.01% (81/89) |
355
- | Branches | 90.47% (38/42) |
356
- | Functions | 66.66% (14/21) |
357
- | Lines | 93.02% (80/86) |
358
+ 📊 <a href="https://geon0529.github.io/usefy/coverage/use-debounce/src/index.html" target="_blank" rel="noopener noreferrer"><strong>View Detailed Coverage Report</strong></a> (GitHub Pages)
358
359
 
359
360
  ### Test Categories
360
361
 
package/dist/index.js CHANGED
@@ -140,7 +140,9 @@ function useDebounce(value, delay = 500, options = {}) {
140
140
  () => timerExpiredRef.current(),
141
141
  waitRef.current
142
142
  );
143
- invokeRef.current(time);
143
+ if (leadingRef.current || trailingRef.current) {
144
+ invokeRef.current(time);
145
+ }
144
146
  }
145
147
  } else {
146
148
  if (timerIdRef.current === void 0) {
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\";\n","import { useEffect, useRef, useState } from \"react\";\n\n/**\n * Options for useDebounce hook\n */\nexport interface UseDebounceOptions {\n /**\n * Maximum time the debounced value can be delayed\n * @default undefined (no maximum)\n */\n maxWait?: number;\n /**\n * Whether to update the debounced value on the leading edge\n * @default false\n */\n leading?: boolean;\n /**\n * Whether to update the debounced value on the trailing edge\n * @default true\n */\n trailing?: boolean;\n}\n\n/**\n * Debounces a value by delaying updates until after a specified delay period has elapsed\n * since the last time the value changed. Useful for search inputs and API calls.\n *\n * @template T - The type of the value to debounce\n * @param value - The value to debounce\n * @param delay - The delay in milliseconds (default: 500ms)\n * @param options - Additional options for controlling debounce behavior\n * @returns The debounced value\n *\n * @example\n * ```tsx\n * function SearchInput() {\n * const [searchTerm, setSearchTerm] = useState('');\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\n *\n * useEffect(() => {\n * if (debouncedSearchTerm) {\n * // API call with debounced value\n * searchAPI(debouncedSearchTerm);\n * }\n * }, [debouncedSearchTerm]);\n *\n * return (\n * <input\n * type=\"text\"\n * value={searchTerm}\n * onChange={(e) => setSearchTerm(e.target.value)}\n * placeholder=\"Search...\"\n * />\n * );\n * }\n * ```\n *\n * @example\n * ```tsx\n * // With leading edge update\n * const debouncedValue = useDebounce(value, 300, { leading: true });\n * ```\n *\n * @example\n * ```tsx\n * // With maximum wait time\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\n * ```\n */\nexport function useDebounce<T>(\n value: T,\n delay: number = 500,\n options: UseDebounceOptions = {}\n): T {\n // Parse options\n const wait = delay || 0;\n const leading = options.leading ?? false;\n const trailing = options.trailing !== undefined ? options.trailing : true;\n const maxing = \"maxWait\" in options;\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\n\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\n undefined\n );\n const lastCallTimeRef = useRef<number | undefined>(undefined);\n const lastInvokeTimeRef = useRef<number>(0);\n const lastValueRef = useRef<T>(value);\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\n\n // Store options in refs to access latest values in timer callbacks\n const waitRef = useRef(wait);\n const leadingRef = useRef(leading);\n const trailingRef = useRef(trailing);\n const maxingRef = useRef(maxing);\n const maxWaitRef = useRef(maxWait);\n\n // Update refs when options change\n waitRef.current = wait;\n leadingRef.current = leading;\n trailingRef.current = trailing;\n maxingRef.current = maxing;\n maxWaitRef.current = maxWait;\n\n // Helper function to get current time\n const now = () => Date.now();\n\n // Define helper functions using refs for latest values\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\n const invokeRef = useRef<(time: number) => void>(() => {});\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\n const timerExpiredRef = useRef<() => void>(() => {});\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\n\n // Helper function: shouldInvoke\n shouldInvokeRef.current = (time: number): boolean => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return true; // First call\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n\n return (\n timeSinceLastCall >= waitRef.current ||\n timeSinceLastCall < 0 || // System time went backwards\n (maxingRef.current &&\n timeSinceLastInvoke >= (maxWaitRef.current as number))\n );\n };\n\n // Helper function: invokeFunc\n invokeRef.current = (time: number): void => {\n setDebouncedValue(lastValueRef.current);\n lastInvokeTimeRef.current = time;\n };\n\n // Helper function: remainingWait\n remainingWaitRef.current = (time: number): number => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return waitRef.current;\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n const timeWaiting = waitRef.current - timeSinceLastCall;\n\n return maxingRef.current\n ? Math.min(\n timeWaiting,\n (maxWaitRef.current as number) - timeSinceLastInvoke\n )\n : timeWaiting;\n };\n\n // Helper function: trailingEdge\n trailingEdgeRef.current = (time: number): void => {\n timerIdRef.current = undefined;\n\n // Only invoke if we have `lastValueRef.current` which means `value` has been\n // debounced at least once.\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\n invokeRef.current(time);\n }\n };\n\n // Helper function: timerExpired\n timerExpiredRef.current = (): void => {\n const time = now();\n if (shouldInvokeRef.current(time)) {\n trailingEdgeRef.current(time);\n } else {\n // Restart the timer.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n remainingWaitRef.current(time)\n );\n }\n };\n\n // Helper function: leadingEdge\n leadingEdgeRef.current = (time: number): void => {\n // Reset any `maxWait` timer.\n lastInvokeTimeRef.current = time;\n // Start the timer for the trailing edge.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n // Invoke the leading edge.\n if (leadingRef.current) {\n invokeRef.current(time);\n }\n };\n\n // Cleanup on unmount only\n useEffect(() => {\n return () => {\n if (timerIdRef.current !== undefined) {\n clearTimeout(timerIdRef.current);\n }\n };\n }, []);\n\n // Main debounce effect - runs when value changes\n useEffect(() => {\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\n if (Object.is(prevValueRef.current, value)) {\n return;\n }\n prevValueRef.current = value;\n\n const time = now();\n const isInvoking = shouldInvokeRef.current(time);\n\n // Update lastValueRef with current value\n lastValueRef.current = value;\n lastCallTimeRef.current = time;\n\n if (isInvoking) {\n if (timerIdRef.current === undefined) {\n leadingEdgeRef.current(time);\n } else if (maxingRef.current) {\n // Handle invocations in a tight loop.\n clearTimeout(timerIdRef.current);\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n invokeRef.current(time);\n }\n } else {\n if (timerIdRef.current === undefined) {\n // Start timer with wait\n // remainingWait is only used inside timerExpired for restarting\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n }\n }\n // No cleanup here - timer should persist across value changes\n // This is the key difference from the previous implementation\n }, [value]);\n\n return debouncedValue;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA4C;AAqErC,SAAS,YACd,OACA,QAAgB,KAChB,UAA8B,CAAC,GAC5B;AAEH,QAAM,OAAO,SAAS;AACt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Aa,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\";\n","import { useEffect, useRef, useState } from \"react\";\n\n/**\n * Options for useDebounce hook\n */\nexport interface UseDebounceOptions {\n /**\n * Maximum time the debounced value can be delayed\n * @default undefined (no maximum)\n */\n maxWait?: number;\n /**\n * Whether to update the debounced value on the leading edge\n * @default false\n */\n leading?: boolean;\n /**\n * Whether to update the debounced value on the trailing edge\n * @default true\n */\n trailing?: boolean;\n}\n\n/**\n * Debounces a value by delaying updates until after a specified delay period has elapsed\n * since the last time the value changed. Useful for search inputs and API calls.\n *\n * @template T - The type of the value to debounce\n * @param value - The value to debounce\n * @param delay - The delay in milliseconds (default: 500ms)\n * @param options - Additional options for controlling debounce behavior\n * @returns The debounced value\n *\n * @example\n * ```tsx\n * function SearchInput() {\n * const [searchTerm, setSearchTerm] = useState('');\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\n *\n * useEffect(() => {\n * if (debouncedSearchTerm) {\n * // API call with debounced value\n * searchAPI(debouncedSearchTerm);\n * }\n * }, [debouncedSearchTerm]);\n *\n * return (\n * <input\n * type=\"text\"\n * value={searchTerm}\n * onChange={(e) => setSearchTerm(e.target.value)}\n * placeholder=\"Search...\"\n * />\n * );\n * }\n * ```\n *\n * @example\n * ```tsx\n * // With leading edge update\n * const debouncedValue = useDebounce(value, 300, { leading: true });\n * ```\n *\n * @example\n * ```tsx\n * // With maximum wait time\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\n * ```\n */\nexport function useDebounce<T>(\n value: T,\n delay: number = 500,\n options: UseDebounceOptions = {}\n): T {\n // Parse options\n const wait = delay || 0;\n const leading = options.leading ?? false;\n const trailing = options.trailing !== undefined ? options.trailing : true;\n const maxing = \"maxWait\" in options;\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\n\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\n undefined\n );\n const lastCallTimeRef = useRef<number | undefined>(undefined);\n const lastInvokeTimeRef = useRef<number>(0);\n const lastValueRef = useRef<T>(value);\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\n\n // Store options in refs to access latest values in timer callbacks\n const waitRef = useRef(wait);\n const leadingRef = useRef(leading);\n const trailingRef = useRef(trailing);\n const maxingRef = useRef(maxing);\n const maxWaitRef = useRef(maxWait);\n\n // Update refs when options change\n waitRef.current = wait;\n leadingRef.current = leading;\n trailingRef.current = trailing;\n maxingRef.current = maxing;\n maxWaitRef.current = maxWait;\n\n // Helper function to get current time\n const now = () => Date.now();\n\n // Define helper functions using refs for latest values\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\n const invokeRef = useRef<(time: number) => void>(() => {});\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\n const timerExpiredRef = useRef<() => void>(() => {});\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\n\n // Helper function: shouldInvoke\n shouldInvokeRef.current = (time: number): boolean => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return true; // First call\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n\n return (\n timeSinceLastCall >= waitRef.current ||\n timeSinceLastCall < 0 || // System time went backwards\n (maxingRef.current &&\n timeSinceLastInvoke >= (maxWaitRef.current as number))\n );\n };\n\n // Helper function: invokeFunc\n invokeRef.current = (time: number): void => {\n setDebouncedValue(lastValueRef.current);\n lastInvokeTimeRef.current = time;\n };\n\n // Helper function: remainingWait\n remainingWaitRef.current = (time: number): number => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return waitRef.current;\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n const timeWaiting = waitRef.current - timeSinceLastCall;\n\n return maxingRef.current\n ? Math.min(\n timeWaiting,\n (maxWaitRef.current as number) - timeSinceLastInvoke\n )\n : timeWaiting;\n };\n\n // Helper function: trailingEdge\n trailingEdgeRef.current = (time: number): void => {\n timerIdRef.current = undefined;\n\n // Only invoke if we have `lastValueRef.current` which means `value` has been\n // debounced at least once.\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\n invokeRef.current(time);\n }\n };\n\n // Helper function: timerExpired\n timerExpiredRef.current = (): void => {\n const time = now();\n if (shouldInvokeRef.current(time)) {\n trailingEdgeRef.current(time);\n } else {\n // Restart the timer.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n remainingWaitRef.current(time)\n );\n }\n };\n\n // Helper function: leadingEdge\n leadingEdgeRef.current = (time: number): void => {\n // Reset any `maxWait` timer.\n lastInvokeTimeRef.current = time;\n // Start the timer for the trailing edge.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n // Invoke the leading edge.\n if (leadingRef.current) {\n invokeRef.current(time);\n }\n };\n\n // Cleanup on unmount only\n useEffect(() => {\n return () => {\n if (timerIdRef.current !== undefined) {\n clearTimeout(timerIdRef.current);\n }\n };\n }, []);\n\n // Main debounce effect - runs when value changes\n useEffect(() => {\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\n if (Object.is(prevValueRef.current, value)) {\n return;\n }\n prevValueRef.current = value;\n\n const time = now();\n const isInvoking = shouldInvokeRef.current(time);\n\n // Update lastValueRef with current value\n lastValueRef.current = value;\n lastCallTimeRef.current = time;\n\n if (isInvoking) {\n if (timerIdRef.current === undefined) {\n leadingEdgeRef.current(time);\n } else if (maxingRef.current) {\n // Handle invocations in a tight loop.\n clearTimeout(timerIdRef.current);\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n // Only invoke if at least one edge is enabled (matches lodash behavior)\n if (leadingRef.current || trailingRef.current) {\n invokeRef.current(time);\n }\n }\n } else {\n if (timerIdRef.current === undefined) {\n // Start timer with wait\n // remainingWait is only used inside timerExpired for restarting\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n }\n }\n // No cleanup here - timer should persist across value changes\n // This is the key difference from the previous implementation\n }, [value]);\n\n return debouncedValue;\n}\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;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AAE5B,qBAAa,WAAW,OAAO;AAC/B,mBAAW,UAAU;AAAA,UACnB,MAAM,gBAAgB,QAAQ;AAAA,UAC9B,QAAQ;AAAA,QACV;AAEA,YAAI,WAAW,WAAW,YAAY,SAAS;AAC7C,oBAAU,QAAQ,IAAI;AAAA,QACxB;AAAA,MACF;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":[]}
package/dist/index.mjs CHANGED
@@ -114,7 +114,9 @@ function useDebounce(value, delay = 500, options = {}) {
114
114
  () => timerExpiredRef.current(),
115
115
  waitRef.current
116
116
  );
117
- invokeRef.current(time);
117
+ if (leadingRef.current || trailingRef.current) {
118
+ invokeRef.current(time);
119
+ }
118
120
  }
119
121
  } else {
120
122
  if (timerIdRef.current === void 0) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useDebounce.ts"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\n\n/**\n * Options for useDebounce hook\n */\nexport interface UseDebounceOptions {\n /**\n * Maximum time the debounced value can be delayed\n * @default undefined (no maximum)\n */\n maxWait?: number;\n /**\n * Whether to update the debounced value on the leading edge\n * @default false\n */\n leading?: boolean;\n /**\n * Whether to update the debounced value on the trailing edge\n * @default true\n */\n trailing?: boolean;\n}\n\n/**\n * Debounces a value by delaying updates until after a specified delay period has elapsed\n * since the last time the value changed. Useful for search inputs and API calls.\n *\n * @template T - The type of the value to debounce\n * @param value - The value to debounce\n * @param delay - The delay in milliseconds (default: 500ms)\n * @param options - Additional options for controlling debounce behavior\n * @returns The debounced value\n *\n * @example\n * ```tsx\n * function SearchInput() {\n * const [searchTerm, setSearchTerm] = useState('');\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\n *\n * useEffect(() => {\n * if (debouncedSearchTerm) {\n * // API call with debounced value\n * searchAPI(debouncedSearchTerm);\n * }\n * }, [debouncedSearchTerm]);\n *\n * return (\n * <input\n * type=\"text\"\n * value={searchTerm}\n * onChange={(e) => setSearchTerm(e.target.value)}\n * placeholder=\"Search...\"\n * />\n * );\n * }\n * ```\n *\n * @example\n * ```tsx\n * // With leading edge update\n * const debouncedValue = useDebounce(value, 300, { leading: true });\n * ```\n *\n * @example\n * ```tsx\n * // With maximum wait time\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\n * ```\n */\nexport function useDebounce<T>(\n value: T,\n delay: number = 500,\n options: UseDebounceOptions = {}\n): T {\n // Parse options\n const wait = delay || 0;\n const leading = options.leading ?? false;\n const trailing = options.trailing !== undefined ? options.trailing : true;\n const maxing = \"maxWait\" in options;\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\n\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\n undefined\n );\n const lastCallTimeRef = useRef<number | undefined>(undefined);\n const lastInvokeTimeRef = useRef<number>(0);\n const lastValueRef = useRef<T>(value);\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\n\n // Store options in refs to access latest values in timer callbacks\n const waitRef = useRef(wait);\n const leadingRef = useRef(leading);\n const trailingRef = useRef(trailing);\n const maxingRef = useRef(maxing);\n const maxWaitRef = useRef(maxWait);\n\n // Update refs when options change\n waitRef.current = wait;\n leadingRef.current = leading;\n trailingRef.current = trailing;\n maxingRef.current = maxing;\n maxWaitRef.current = maxWait;\n\n // Helper function to get current time\n const now = () => Date.now();\n\n // Define helper functions using refs for latest values\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\n const invokeRef = useRef<(time: number) => void>(() => {});\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\n const timerExpiredRef = useRef<() => void>(() => {});\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\n\n // Helper function: shouldInvoke\n shouldInvokeRef.current = (time: number): boolean => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return true; // First call\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n\n return (\n timeSinceLastCall >= waitRef.current ||\n timeSinceLastCall < 0 || // System time went backwards\n (maxingRef.current &&\n timeSinceLastInvoke >= (maxWaitRef.current as number))\n );\n };\n\n // Helper function: invokeFunc\n invokeRef.current = (time: number): void => {\n setDebouncedValue(lastValueRef.current);\n lastInvokeTimeRef.current = time;\n };\n\n // Helper function: remainingWait\n remainingWaitRef.current = (time: number): number => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return waitRef.current;\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n const timeWaiting = waitRef.current - timeSinceLastCall;\n\n return maxingRef.current\n ? Math.min(\n timeWaiting,\n (maxWaitRef.current as number) - timeSinceLastInvoke\n )\n : timeWaiting;\n };\n\n // Helper function: trailingEdge\n trailingEdgeRef.current = (time: number): void => {\n timerIdRef.current = undefined;\n\n // Only invoke if we have `lastValueRef.current` which means `value` has been\n // debounced at least once.\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\n invokeRef.current(time);\n }\n };\n\n // Helper function: timerExpired\n timerExpiredRef.current = (): void => {\n const time = now();\n if (shouldInvokeRef.current(time)) {\n trailingEdgeRef.current(time);\n } else {\n // Restart the timer.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n remainingWaitRef.current(time)\n );\n }\n };\n\n // Helper function: leadingEdge\n leadingEdgeRef.current = (time: number): void => {\n // Reset any `maxWait` timer.\n lastInvokeTimeRef.current = time;\n // Start the timer for the trailing edge.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n // Invoke the leading edge.\n if (leadingRef.current) {\n invokeRef.current(time);\n }\n };\n\n // Cleanup on unmount only\n useEffect(() => {\n return () => {\n if (timerIdRef.current !== undefined) {\n clearTimeout(timerIdRef.current);\n }\n };\n }, []);\n\n // Main debounce effect - runs when value changes\n useEffect(() => {\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\n if (Object.is(prevValueRef.current, value)) {\n return;\n }\n prevValueRef.current = value;\n\n const time = now();\n const isInvoking = shouldInvokeRef.current(time);\n\n // Update lastValueRef with current value\n lastValueRef.current = value;\n lastCallTimeRef.current = time;\n\n if (isInvoking) {\n if (timerIdRef.current === undefined) {\n leadingEdgeRef.current(time);\n } else if (maxingRef.current) {\n // Handle invocations in a tight loop.\n clearTimeout(timerIdRef.current);\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n invokeRef.current(time);\n }\n } else {\n if (timerIdRef.current === undefined) {\n // Start timer with wait\n // remainingWait is only used inside timerExpired for restarting\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n }\n }\n // No cleanup here - timer should persist across value changes\n // This is the key difference from the previous implementation\n }, [value]);\n\n return debouncedValue;\n}\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;A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F,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/useDebounce.ts"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\n\n/**\n * Options for useDebounce hook\n */\nexport interface UseDebounceOptions {\n /**\n * Maximum time the debounced value can be delayed\n * @default undefined (no maximum)\n */\n maxWait?: number;\n /**\n * Whether to update the debounced value on the leading edge\n * @default false\n */\n leading?: boolean;\n /**\n * Whether to update the debounced value on the trailing edge\n * @default true\n */\n trailing?: boolean;\n}\n\n/**\n * Debounces a value by delaying updates until after a specified delay period has elapsed\n * since the last time the value changed. Useful for search inputs and API calls.\n *\n * @template T - The type of the value to debounce\n * @param value - The value to debounce\n * @param delay - The delay in milliseconds (default: 500ms)\n * @param options - Additional options for controlling debounce behavior\n * @returns The debounced value\n *\n * @example\n * ```tsx\n * function SearchInput() {\n * const [searchTerm, setSearchTerm] = useState('');\n * const debouncedSearchTerm = useDebounce(searchTerm, 500);\n *\n * useEffect(() => {\n * if (debouncedSearchTerm) {\n * // API call with debounced value\n * searchAPI(debouncedSearchTerm);\n * }\n * }, [debouncedSearchTerm]);\n *\n * return (\n * <input\n * type=\"text\"\n * value={searchTerm}\n * onChange={(e) => setSearchTerm(e.target.value)}\n * placeholder=\"Search...\"\n * />\n * );\n * }\n * ```\n *\n * @example\n * ```tsx\n * // With leading edge update\n * const debouncedValue = useDebounce(value, 300, { leading: true });\n * ```\n *\n * @example\n * ```tsx\n * // With maximum wait time\n * const debouncedValue = useDebounce(value, 500, { maxWait: 2000 });\n * ```\n */\nexport function useDebounce<T>(\n value: T,\n delay: number = 500,\n options: UseDebounceOptions = {}\n): T {\n // Parse options\n const wait = delay || 0;\n const leading = options.leading ?? false;\n const trailing = options.trailing !== undefined ? options.trailing : true;\n const maxing = \"maxWait\" in options;\n const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;\n\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\n const timerIdRef = useRef<ReturnType<typeof setTimeout> | undefined>(\n undefined\n );\n const lastCallTimeRef = useRef<number | undefined>(undefined);\n const lastInvokeTimeRef = useRef<number>(0);\n const lastValueRef = useRef<T>(value);\n const prevValueRef = useRef<T>(value); // Track previous value to detect actual changes\n\n // Store options in refs to access latest values in timer callbacks\n const waitRef = useRef(wait);\n const leadingRef = useRef(leading);\n const trailingRef = useRef(trailing);\n const maxingRef = useRef(maxing);\n const maxWaitRef = useRef(maxWait);\n\n // Update refs when options change\n waitRef.current = wait;\n leadingRef.current = leading;\n trailingRef.current = trailing;\n maxingRef.current = maxing;\n maxWaitRef.current = maxWait;\n\n // Helper function to get current time\n const now = () => Date.now();\n\n // Define helper functions using refs for latest values\n const shouldInvokeRef = useRef<(time: number) => boolean>(() => false);\n const invokeRef = useRef<(time: number) => void>(() => {});\n const remainingWaitRef = useRef<(time: number) => number>(() => 0);\n const timerExpiredRef = useRef<() => void>(() => {});\n const trailingEdgeRef = useRef<(time: number) => void>(() => {});\n const leadingEdgeRef = useRef<(time: number) => void>(() => {});\n\n // Helper function: shouldInvoke\n shouldInvokeRef.current = (time: number): boolean => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return true; // First call\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n\n return (\n timeSinceLastCall >= waitRef.current ||\n timeSinceLastCall < 0 || // System time went backwards\n (maxingRef.current &&\n timeSinceLastInvoke >= (maxWaitRef.current as number))\n );\n };\n\n // Helper function: invokeFunc\n invokeRef.current = (time: number): void => {\n setDebouncedValue(lastValueRef.current);\n lastInvokeTimeRef.current = time;\n };\n\n // Helper function: remainingWait\n remainingWaitRef.current = (time: number): number => {\n const lastCallTime = lastCallTimeRef.current;\n if (lastCallTime === undefined) {\n return waitRef.current;\n }\n\n const timeSinceLastCall = time - lastCallTime;\n const timeSinceLastInvoke = time - lastInvokeTimeRef.current;\n const timeWaiting = waitRef.current - timeSinceLastCall;\n\n return maxingRef.current\n ? Math.min(\n timeWaiting,\n (maxWaitRef.current as number) - timeSinceLastInvoke\n )\n : timeWaiting;\n };\n\n // Helper function: trailingEdge\n trailingEdgeRef.current = (time: number): void => {\n timerIdRef.current = undefined;\n\n // Only invoke if we have `lastValueRef.current` which means `value` has been\n // debounced at least once.\n if (trailingRef.current && lastCallTimeRef.current !== undefined) {\n invokeRef.current(time);\n }\n };\n\n // Helper function: timerExpired\n timerExpiredRef.current = (): void => {\n const time = now();\n if (shouldInvokeRef.current(time)) {\n trailingEdgeRef.current(time);\n } else {\n // Restart the timer.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n remainingWaitRef.current(time)\n );\n }\n };\n\n // Helper function: leadingEdge\n leadingEdgeRef.current = (time: number): void => {\n // Reset any `maxWait` timer.\n lastInvokeTimeRef.current = time;\n // Start the timer for the trailing edge.\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n // Invoke the leading edge.\n if (leadingRef.current) {\n invokeRef.current(time);\n }\n };\n\n // Cleanup on unmount only\n useEffect(() => {\n return () => {\n if (timerIdRef.current !== undefined) {\n clearTimeout(timerIdRef.current);\n }\n };\n }, []);\n\n // Main debounce effect - runs when value changes\n useEffect(() => {\n // Skip if value hasn't actually changed (prevents initial render from consuming leading edge)\n if (Object.is(prevValueRef.current, value)) {\n return;\n }\n prevValueRef.current = value;\n\n const time = now();\n const isInvoking = shouldInvokeRef.current(time);\n\n // Update lastValueRef with current value\n lastValueRef.current = value;\n lastCallTimeRef.current = time;\n\n if (isInvoking) {\n if (timerIdRef.current === undefined) {\n leadingEdgeRef.current(time);\n } else if (maxingRef.current) {\n // Handle invocations in a tight loop.\n clearTimeout(timerIdRef.current);\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n // Only invoke if at least one edge is enabled (matches lodash behavior)\n if (leadingRef.current || trailingRef.current) {\n invokeRef.current(time);\n }\n }\n } else {\n if (timerIdRef.current === undefined) {\n // Start timer with wait\n // remainingWait is only used inside timerExpired for restarting\n timerIdRef.current = setTimeout(\n () => timerExpiredRef.current(),\n waitRef.current\n );\n }\n }\n // No cleanup here - timer should persist across value changes\n // This is the key difference from the previous implementation\n }, [value]);\n\n return debouncedValue;\n}\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,aAAW,UAAU;AACrB,cAAY,UAAU;AACtB,YAAU,UAAU;AACpB,aAAW,UAAU;AAGrB,QAAM,MAAM,MAAM,KAAK,IAAI;AAG3B,QAAM,kBAAkB,OAAkC,MAAM,KAAK;AACrE,QAAM,YAAY,OAA+B,MAAM;AAAA,EAAC,CAAC;AACzD,QAAM,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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,YAAU,MAAM;AACd,WAAO,MAAM;AACX,UAAI,WAAW,YAAY,QAAW;AACpC,qBAAa,WAAW,OAAO;AAAA,MACjC;AAAA,IACF;AAAA,EACF,GAAG,CAAC,CAAC;AAGL,YAAU,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;AAEA,YAAI,WAAW,WAAW,YAAY,SAAS;AAC7C,oBAAU,QAAQ,IAAI;AAAA,QACxB;AAAA,MACF;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":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usefy/use-debounce",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
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",