@applicaster/zapp-react-native-utils 13.0.0-rc.54 → 13.0.0-rc.55
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/functionUtils/index.ts +21 -8
- package/package.json +2 -2
package/functionUtils/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as R from "ramda";
|
|
2
|
-
import { useRef } from "react";
|
|
2
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
*
|
|
@@ -9,31 +9,44 @@ import { useRef } from "react";
|
|
|
9
9
|
*/
|
|
10
10
|
export const useThrottle = (callback: () => void, delay: number) => {
|
|
11
11
|
const lastCallRef = useRef<number>(0);
|
|
12
|
-
const timeoutRef = useRef<
|
|
12
|
+
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
13
|
+
const callbackRef = useRef<() => void>(callback);
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
callbackRef.current = callback;
|
|
17
|
+
}, [callback]);
|
|
18
|
+
|
|
19
|
+
const throttledFunction = useCallback(() => {
|
|
15
20
|
const now = Date.now();
|
|
16
21
|
|
|
17
22
|
if (now - lastCallRef.current >= delay) {
|
|
18
|
-
|
|
23
|
+
callbackRef.current();
|
|
19
24
|
lastCallRef.current = now;
|
|
20
25
|
} else if (!timeoutRef.current) {
|
|
21
26
|
const remainingTime = delay - (now - lastCallRef.current);
|
|
22
27
|
|
|
23
28
|
timeoutRef.current = setTimeout(() => {
|
|
24
|
-
|
|
29
|
+
callbackRef.current();
|
|
25
30
|
lastCallRef.current = Date.now();
|
|
26
31
|
timeoutRef.current = null;
|
|
27
32
|
}, remainingTime);
|
|
28
33
|
}
|
|
29
|
-
};
|
|
34
|
+
}, [delay]);
|
|
30
35
|
|
|
31
|
-
const cancel = () => {
|
|
36
|
+
const cancel = useCallback(() => {
|
|
32
37
|
if (timeoutRef.current) {
|
|
33
38
|
clearTimeout(timeoutRef.current);
|
|
34
39
|
timeoutRef.current = null;
|
|
35
40
|
}
|
|
36
|
-
};
|
|
41
|
+
}, []);
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
return () => {
|
|
45
|
+
if (timeoutRef.current) {
|
|
46
|
+
clearTimeout(timeoutRef.current);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}, []);
|
|
37
50
|
|
|
38
51
|
return { throttledFunction, cancel };
|
|
39
52
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "13.0.0-rc.
|
|
3
|
+
"version": "13.0.0-rc.55",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@applicaster/applicaster-types": "13.0.0-rc.
|
|
31
|
+
"@applicaster/applicaster-types": "13.0.0-rc.55",
|
|
32
32
|
"buffer": "^5.2.1",
|
|
33
33
|
"camelize": "^1.0.0",
|
|
34
34
|
"dayjs": "^1.11.10",
|