@peerbit/document-react 0.1.0
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/LICENSE +202 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +6 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/useCount.d.ts +12 -0
- package/dist/src/useCount.d.ts.map +1 -0
- package/dist/src/useCount.js +41 -0
- package/dist/src/useCount.js.map +1 -0
- package/dist/src/useLocal.d.ts +22 -0
- package/dist/src/useLocal.d.ts.map +1 -0
- package/dist/src/useLocal.js +74 -0
- package/dist/src/useLocal.js.map +1 -0
- package/dist/src/useMount.d.ts +3 -0
- package/dist/src/useMount.d.ts.map +1 -0
- package/dist/src/useMount.js +12 -0
- package/dist/src/useMount.js.map +1 -0
- package/dist/src/useQuery.d.ts +52 -0
- package/dist/src/useQuery.d.ts.map +1 -0
- package/dist/src/useQuery.js +433 -0
- package/dist/src/useQuery.js.map +1 -0
- package/dist/src/utils.d.ts +4 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +41 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +75 -0
- package/src/index.ts +5 -0
- package/src/useCount.tsx +57 -0
- package/src/useLocal.tsx +120 -0
- package/src/useMount.tsx +15 -0
- package/src/useQuery.tsx +561 -0
- package/src/utils.ts +55 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export function debounceLeadingTrailing<
|
|
2
|
+
T extends (this: any, ...args: any[]) => void,
|
|
3
|
+
>(
|
|
4
|
+
func: T,
|
|
5
|
+
delay: number,
|
|
6
|
+
): ((this: ThisParameterType<T>, ...args: Parameters<T>) => void) & {
|
|
7
|
+
cancel: () => void;
|
|
8
|
+
} {
|
|
9
|
+
let timeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
10
|
+
let lastArgs: Parameters<T> | null = null;
|
|
11
|
+
let lastThis: any;
|
|
12
|
+
let pendingTrailing = false;
|
|
13
|
+
|
|
14
|
+
const debounced = function (
|
|
15
|
+
this: ThisParameterType<T>,
|
|
16
|
+
...args: Parameters<T>
|
|
17
|
+
) {
|
|
18
|
+
if (!timeoutId) {
|
|
19
|
+
// Leading call: no timer means this is the first call in this period.
|
|
20
|
+
func.apply(this, args);
|
|
21
|
+
} else {
|
|
22
|
+
// Subsequent calls during the delay mark that a trailing call is needed.
|
|
23
|
+
pendingTrailing = true;
|
|
24
|
+
}
|
|
25
|
+
// Always update with the most recent context and arguments.
|
|
26
|
+
lastArgs = args;
|
|
27
|
+
lastThis = this;
|
|
28
|
+
|
|
29
|
+
// Reset the timer.
|
|
30
|
+
if (timeoutId) {
|
|
31
|
+
clearTimeout(timeoutId);
|
|
32
|
+
}
|
|
33
|
+
timeoutId = setTimeout(() => {
|
|
34
|
+
timeoutId = null;
|
|
35
|
+
// If there were any calls during the delay, call the function on the trailing edge.
|
|
36
|
+
if (pendingTrailing && lastArgs) {
|
|
37
|
+
func.apply(lastThis, lastArgs);
|
|
38
|
+
}
|
|
39
|
+
// Reset the trailing flag after the trailing call.
|
|
40
|
+
pendingTrailing = false;
|
|
41
|
+
}, delay);
|
|
42
|
+
} as ((this: ThisParameterType<T>, ...args: Parameters<T>) => void) & {
|
|
43
|
+
cancel: () => void;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
debounced.cancel = () => {
|
|
47
|
+
if (timeoutId) {
|
|
48
|
+
clearTimeout(timeoutId);
|
|
49
|
+
timeoutId = null;
|
|
50
|
+
}
|
|
51
|
+
pendingTrailing = false;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
return debounced;
|
|
55
|
+
}
|