@aiszlab/relax 1.2.20 → 1.2.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.
- package/dist/dom/contains.d.ts +1 -0
- package/dist/dom/contains.mjs +20 -0
- package/dist/dom/index.d.ts +2 -1
- package/dist/dom/index.mjs +1 -0
- package/dist/hooks/use-counter.mjs +1 -1
- package/dist/hooks/use-debounce-callback.d.ts +6 -1
- package/dist/hooks/use-debounce-callback.mjs +25 -33
- package/dist/hooks/use-event.d.ts +1 -3
- package/dist/hooks/use-event.mjs +4 -4
- package/dist/hooks/use-throttle-callback.d.ts +6 -1
- package/dist/hooks/use-throttle-callback.mjs +28 -36
- package/dist/utils/arguments.d.ts +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const contains: (root: Node | null | undefined, n?: Node | null) => boolean;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const contains = (root, n) => {
|
|
2
|
+
if (!root) {
|
|
3
|
+
return false;
|
|
4
|
+
}
|
|
5
|
+
// Use native if support
|
|
6
|
+
if (root.contains) {
|
|
7
|
+
return root.contains(n !== null && n !== void 0 ? n : null);
|
|
8
|
+
}
|
|
9
|
+
// `document.contains` not support with IE11
|
|
10
|
+
let node = n;
|
|
11
|
+
while (node) {
|
|
12
|
+
if (node === root) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
node = node.parentNode;
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { contains };
|
package/dist/dom/index.d.ts
CHANGED
package/dist/dom/index.mjs
CHANGED
|
@@ -16,7 +16,7 @@ const useCounter = (_a = { initialValue: 0, max: Infinity, min: 0 }) => {
|
|
|
16
16
|
setCount((prev) => Math.min(max, prev + step));
|
|
17
17
|
}, [max]);
|
|
18
18
|
const prev = useCallback((step = 1) => {
|
|
19
|
-
setCount((prev) => Math.max(prev - step));
|
|
19
|
+
setCount((prev) => Math.max(min, prev - step));
|
|
20
20
|
}, [min]);
|
|
21
21
|
const first = useCallback(() => {
|
|
22
22
|
setCount(min);
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
interface Options {
|
|
2
2
|
/**
|
|
3
|
+
* @description
|
|
3
4
|
* The delay time (in milliseconds) until the debounce function is called.
|
|
4
5
|
* default 1000
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* 2000
|
|
5
9
|
*/
|
|
6
10
|
readonly delay: number;
|
|
7
11
|
}
|
|
12
|
+
type Callable<T, R> = (value: T) => R;
|
|
8
13
|
/**
|
|
9
14
|
* @author murukal
|
|
10
15
|
*
|
|
11
16
|
* @description
|
|
12
17
|
* debounce callback
|
|
13
18
|
*/
|
|
14
|
-
export declare const useDebounceCallback: <T>(
|
|
19
|
+
export declare const useDebounceCallback: <T, R>(_callable: Callable<T, R>, options?: Options) => {
|
|
15
20
|
next: (value: T) => void;
|
|
16
21
|
complete: () => void;
|
|
17
22
|
cancel: () => void;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useRef, useCallback, useEffect } from 'react';
|
|
2
2
|
import { Observable, debounceTime } from 'rxjs';
|
|
3
|
+
import { useEvent } from './use-event.mjs';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* @author murukal
|
|
@@ -7,54 +8,45 @@ import { Observable, debounceTime } from 'rxjs';
|
|
|
7
8
|
* @description
|
|
8
9
|
* debounce callback
|
|
9
10
|
*/
|
|
10
|
-
const useDebounceCallback = (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
const initialize = useCallback(() => {
|
|
19
|
-
listener.current = new Observable((subscriber) => {
|
|
11
|
+
const useDebounceCallback = (_callable, options) => {
|
|
12
|
+
var _a;
|
|
13
|
+
const trigger = useRef(null);
|
|
14
|
+
const listener = useRef(null);
|
|
15
|
+
const delay = (_a = options === null || options === void 0 ? void 0 : options.delay) !== null && _a !== void 0 ? _a : 1000;
|
|
16
|
+
const callable = useEvent(_callable);
|
|
17
|
+
const listen = useCallback(() => {
|
|
18
|
+
const listened = new Observable((subscriber) => {
|
|
20
19
|
trigger.current = subscriber;
|
|
21
20
|
})
|
|
22
21
|
.pipe(debounceTime(delay))
|
|
23
22
|
.subscribe({
|
|
24
|
-
next:
|
|
25
|
-
|
|
26
|
-
},
|
|
27
|
-
complete: () => {
|
|
28
|
-
initialize();
|
|
29
|
-
}
|
|
23
|
+
next: callable,
|
|
24
|
+
complete: listen
|
|
30
25
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
listener.current = listened;
|
|
27
|
+
return listened;
|
|
28
|
+
}, [delay]);
|
|
34
29
|
useEffect(() => {
|
|
35
|
-
|
|
30
|
+
const listened = listen();
|
|
36
31
|
// dispose
|
|
37
32
|
return () => {
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
listened.unsubscribe();
|
|
34
|
+
listener.current = null;
|
|
35
|
+
trigger.current = null;
|
|
40
36
|
};
|
|
41
|
-
}, [
|
|
42
|
-
|
|
43
|
-
const next = useCallback((value) => {
|
|
37
|
+
}, [listen]);
|
|
38
|
+
const next = useEvent((value) => {
|
|
44
39
|
var _a;
|
|
45
40
|
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(value);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const complete = useCallback(() => {
|
|
41
|
+
});
|
|
42
|
+
const complete = useEvent(() => {
|
|
49
43
|
var _a;
|
|
50
44
|
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/// if the callback has been called, it can not be canceled
|
|
54
|
-
const cancel = useCallback(() => {
|
|
45
|
+
});
|
|
46
|
+
const cancel = useEvent(() => {
|
|
55
47
|
var _a;
|
|
56
48
|
(_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
57
|
-
}
|
|
49
|
+
});
|
|
58
50
|
return {
|
|
59
51
|
next,
|
|
60
52
|
complete,
|
package/dist/hooks/use-event.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useRef, useCallback } from 'react';
|
|
2
2
|
|
|
3
|
-
const useEvent = (
|
|
4
|
-
const ref = useRef();
|
|
5
|
-
ref.current =
|
|
6
|
-
return useCallback((...args) => ref.current(...args), []);
|
|
3
|
+
const useEvent = (callable) => {
|
|
4
|
+
const ref = useRef(callable);
|
|
5
|
+
ref.current = callable;
|
|
6
|
+
return useCallback(((...args) => ref.current(...args)), []);
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
export { useEvent };
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
interface Options {
|
|
2
2
|
/**
|
|
3
|
+
* @description
|
|
3
4
|
* The delay time (in milliseconds) until the throttle function is called.
|
|
4
5
|
* default 1000
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* 2000
|
|
5
9
|
*/
|
|
6
10
|
readonly duration: number;
|
|
7
11
|
}
|
|
12
|
+
type Callable<T, R> = (value: T) => R;
|
|
8
13
|
/**
|
|
9
14
|
* @author murukal
|
|
10
15
|
*
|
|
11
16
|
* @description
|
|
12
17
|
* throttle callback
|
|
13
18
|
*/
|
|
14
|
-
export declare const useThrottleCallback: <T>(
|
|
19
|
+
export declare const useThrottleCallback: <T, R>(_callable: Callable<T, R>, options?: Options) => {
|
|
15
20
|
next: (value: T) => void;
|
|
16
21
|
complete: () => void;
|
|
17
22
|
cancel: () => void;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useRef, useCallback, useEffect } from 'react';
|
|
2
2
|
import { Observable, throttleTime } from 'rxjs';
|
|
3
|
+
import { useEvent } from './use-event.mjs';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* @author murukal
|
|
@@ -7,54 +8,45 @@ import { Observable, throttleTime } from 'rxjs';
|
|
|
7
8
|
* @description
|
|
8
9
|
* throttle callback
|
|
9
10
|
*/
|
|
10
|
-
const useThrottleCallback = (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
listener.current = new Observable((subscriber) => {
|
|
20
|
-
runner.current = subscriber;
|
|
11
|
+
const useThrottleCallback = (_callable, options) => {
|
|
12
|
+
var _a;
|
|
13
|
+
const trigger = useRef(null);
|
|
14
|
+
const listener = useRef(null);
|
|
15
|
+
const duration = (_a = options === null || options === void 0 ? void 0 : options.duration) !== null && _a !== void 0 ? _a : 1000;
|
|
16
|
+
const callable = useEvent(_callable);
|
|
17
|
+
const listen = useCallback(() => {
|
|
18
|
+
const listened = new Observable((subscriber) => {
|
|
19
|
+
trigger.current = subscriber;
|
|
21
20
|
})
|
|
22
21
|
.pipe(throttleTime(duration))
|
|
23
22
|
.subscribe({
|
|
24
|
-
next:
|
|
25
|
-
|
|
26
|
-
},
|
|
27
|
-
complete: () => {
|
|
28
|
-
initialize();
|
|
29
|
-
}
|
|
23
|
+
next: callable,
|
|
24
|
+
complete: listen
|
|
30
25
|
});
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
26
|
+
listener.current = listened;
|
|
27
|
+
return listened;
|
|
28
|
+
}, [duration]);
|
|
34
29
|
useEffect(() => {
|
|
35
|
-
|
|
30
|
+
const listened = listen();
|
|
36
31
|
// dispose
|
|
37
32
|
return () => {
|
|
38
|
-
|
|
39
|
-
|
|
33
|
+
listened.unsubscribe();
|
|
34
|
+
listener.current = null;
|
|
35
|
+
trigger.current = null;
|
|
40
36
|
};
|
|
41
|
-
}, [
|
|
42
|
-
|
|
43
|
-
const next = useCallback((value) => {
|
|
37
|
+
}, [listen]);
|
|
38
|
+
const next = useEvent((value) => {
|
|
44
39
|
var _a;
|
|
45
|
-
(_a =
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const complete = useCallback(() => {
|
|
40
|
+
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(value);
|
|
41
|
+
});
|
|
42
|
+
const complete = useEvent(() => {
|
|
49
43
|
var _a;
|
|
50
|
-
(_a =
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/// if the callback has been called, it can not be canceled
|
|
54
|
-
const cancel = useCallback(() => {
|
|
44
|
+
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete();
|
|
45
|
+
});
|
|
46
|
+
const cancel = useEvent(() => {
|
|
55
47
|
var _a;
|
|
56
48
|
(_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
57
|
-
}
|
|
49
|
+
});
|
|
58
50
|
return {
|
|
59
51
|
next,
|
|
60
52
|
complete,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Arguments<T extends Function> = T extends (...args: infer A) => unknown ? A : never;
|