@tamagui/use-debounce 1.0.1-beta.59 → 1.0.1-beta.60
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/package.json +3 -2
- package/src/index.ts +89 -0
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/use-debounce",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.60",
|
|
4
4
|
"types": "./types/index.d.ts",
|
|
5
5
|
"main": "dist/cjs",
|
|
6
6
|
"module": "dist/esm",
|
|
7
7
|
"module:jsx": "dist/jsx",
|
|
8
8
|
"files": [
|
|
9
|
+
"src",
|
|
9
10
|
"types",
|
|
10
11
|
"dist"
|
|
11
12
|
],
|
|
@@ -16,7 +17,7 @@
|
|
|
16
17
|
"clean:build": "tamagui-build clean:build"
|
|
17
18
|
},
|
|
18
19
|
"devDependencies": {
|
|
19
|
-
"@tamagui/build": "^1.0.1-beta.
|
|
20
|
+
"@tamagui/build": "^1.0.1-beta.60",
|
|
20
21
|
"react": "*"
|
|
21
22
|
},
|
|
22
23
|
"peerDependencies": {
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
}
|