@tamagui/use-debounce 1.0.0-alpha.22 → 1.0.0-alpha.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.
Files changed (2) hide show
  1. package/package.json +2 -3
  2. package/src/index.ts +0 -89
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "@tamagui/use-debounce",
3
- "version": "1.0.0-alpha.22",
3
+ "version": "1.0.0-alpha.23",
4
4
  "typings": "types",
5
5
  "main": "dist/cjs",
6
6
  "module": "dist/esm",
7
7
  "module:jsx": "dist/jsx",
8
8
  "files": [
9
9
  "types",
10
- "src",
11
10
  "dist"
12
11
  ],
13
12
  "scripts": {
@@ -23,5 +22,5 @@
23
22
  "publishConfig": {
24
23
  "access": "public"
25
24
  },
26
- "gitHead": "f247aca6f00c201efbc317bb24b22a62b08d1033"
25
+ "gitHead": "69fb1d82868a6525fee6b0750cb2113d88ffea79"
27
26
  }
package/src/index.ts DELETED
@@ -1,89 +0,0 @@
1
- import { useEffect, useMemo, useRef, useState } from 'react'
2
-
3
- type DebounceSettings = {
4
- leading?: boolean
5
- }
6
-
7
- export function debounce<A extends Function>(
8
- func: A,
9
- wait?: number,
10
- leading?: boolean
11
- ): A & {
12
- cancel: () => void
13
- } {
14
- let timeout: any
15
- let isCancelled = false
16
-
17
- function debounced(this: any) {
18
- isCancelled = false
19
- let context = this
20
- let args = arguments
21
- if (leading && !timeout) {
22
- func.apply(context, args)
23
- }
24
- clearTimeout(timeout)
25
- timeout = setTimeout(function () {
26
- timeout = null
27
- if (!leading && !isCancelled) {
28
- func.apply(context, args)
29
- }
30
- isCancelled = false
31
- }, wait)
32
- }
33
-
34
- debounced.cancel = () => {
35
- isCancelled = true
36
- }
37
-
38
- return debounced as any
39
- }
40
-
41
- const defaultOpts = { leading: false }
42
-
43
- export function useDebounce<
44
- A extends (...args: any) => any | undefined | null,
45
- DebouncedFn extends A & {
46
- cancel: () => void
47
- }
48
- >(
49
- fn: A,
50
- wait: number,
51
- options: DebounceSettings = defaultOpts,
52
- mountArgs: any[] = []
53
- ): DebouncedFn {
54
- const dbEffect = useRef<DebouncedFn | null>(null)
55
-
56
- useEffect(() => {
57
- return () => {
58
- dbEffect.current?.cancel()
59
- }
60
- }, [])
61
-
62
- return useMemo(() => {
63
- dbEffect.current = debounce(fn, wait, options.leading) as unknown as DebouncedFn
64
- return dbEffect.current
65
- }, [options.leading, ...mountArgs])
66
- }
67
-
68
- /**
69
- * Returns a value once it stops changing after "amt" time.
70
- * Note: you may need to memo or this will keep re-rendering
71
- */
72
- export function useDebounceValue<A>(val: A, amt = 0): A {
73
- const [state, setState] = useState(val)
74
-
75
- useEffect(() => {
76
- let tm = setTimeout(() => {
77
- setState((prev) => {
78
- if (prev === val) return prev
79
- return val
80
- })
81
- }, amt)
82
-
83
- return () => {
84
- clearTimeout(tm)
85
- }
86
- }, [val])
87
-
88
- return state
89
- }