esoftplay 0.0.135 → 0.0.136

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/timeout.ts ADDED
@@ -0,0 +1,91 @@
1
+ import { useEffect, useRef } from "react";
2
+
3
+ export function createTimeout() {
4
+ let timeoutId: any = undefined;
5
+ function set(callback: Function, delay: number) {
6
+ clear();
7
+ timeoutId = setTimeout(() => {
8
+ callback();
9
+ clear()
10
+ }, delay);
11
+ }
12
+
13
+ function clear() {
14
+ if (timeoutId !== undefined) {
15
+ clearTimeout(timeoutId);
16
+ timeoutId = undefined;
17
+ }
18
+ }
19
+
20
+ return { set, clear };
21
+ }
22
+
23
+ export function useTimeout() {
24
+ const smartTimeout = useRef(createTimeout());
25
+
26
+ useEffect(() => {
27
+ return () => smartTimeout.current?.clear();
28
+ }, [smartTimeout]);
29
+
30
+ return smartTimeout.current?.set;
31
+ }
32
+
33
+
34
+ export function createInterval() {
35
+ let timeoutId: any = undefined;
36
+ function set(callback: Function, delay: number) {
37
+ clear();
38
+ timeoutId = setInterval(() => {
39
+ callback();
40
+ }, delay);
41
+ }
42
+
43
+ function clear() {
44
+ if (timeoutId !== undefined) {
45
+ clearInterval(timeoutId);
46
+ timeoutId = undefined;
47
+ }
48
+ }
49
+
50
+ return { set, clear };
51
+ }
52
+
53
+ export function useInterval() {
54
+ const smartInterval = useRef(createInterval());
55
+
56
+ useEffect(() => {
57
+ return () => smartInterval.current?.clear();
58
+ }, [smartInterval]);
59
+
60
+ return smartInterval.current?.set;
61
+ }
62
+
63
+ export function createDebounce() {
64
+ let timeoutId: any = undefined;
65
+ function set(callback: Function, delay: number) {
66
+ clear();
67
+ timeoutId = setTimeout(() => {
68
+ callback();
69
+ clear()
70
+ }, delay);
71
+ }
72
+
73
+ function clear() {
74
+ if (timeoutId !== undefined) {
75
+ clearTimeout(timeoutId);
76
+ timeoutId = undefined;
77
+ }
78
+ }
79
+
80
+ return { set, clear };
81
+ }
82
+
83
+ export function useDebounce() {
84
+ const smartDebounce = useRef(createDebounce());
85
+
86
+ useEffect(() => {
87
+ return () => smartDebounce.current?.clear();
88
+ }, [smartDebounce]);
89
+
90
+ return smartDebounce.current?.set;
91
+ }