@aiszlab/relax 1.2.24 → 1.2.26
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/hooks/use-boolean.d.ts +14 -9
- package/dist/hooks/use-boolean.mjs +9 -10
- package/dist/hooks/use-controlled-state.d.ts +1 -1
- package/dist/hooks/use-counter.d.ts +19 -15
- package/dist/hooks/use-counter.mjs +21 -23
- package/dist/hooks/use-debounce-callback.d.ts +11 -18
- package/dist/hooks/use-debounce-callback.mjs +23 -40
- package/dist/hooks/use-default.d.ts +1 -1
- package/dist/hooks/use-default.mjs +2 -9
- package/dist/hooks/use-focus.d.ts +9 -0
- package/dist/hooks/use-hover.d.ts +9 -0
- package/dist/hooks/use-hover.mjs +15 -0
- package/dist/hooks/use-image-loader.d.ts +2 -2
- package/dist/hooks/use-mount.d.ts +2 -2
- package/dist/hooks/use-mount.mjs +2 -2
- package/dist/hooks/use-mounted.d.ts +2 -2
- package/dist/hooks/use-mounted.mjs +2 -2
- package/dist/hooks/use-reactive.d.ts +1 -1
- package/dist/hooks/use-scroll-locker.mjs +9 -10
- package/dist/hooks/use-throttle-callback.d.ts +11 -18
- package/dist/hooks/use-throttle-callback.mjs +23 -40
- package/dist/hooks/use-timeout.d.ts +1 -1
- package/dist/hooks/use-timeout.mjs +4 -5
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +4 -0
- package/dist/is/is-state-getter.d.ts +1 -9
- package/dist/types/arguments.d.ts +1 -1
- package/dist/types/first.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/state.d.ts +10 -0
- package/dist/utils/debounce.d.ts +29 -0
- package/dist/utils/debounce.mjs +52 -0
- package/dist/utils/throttle.d.ts +3 -0
- package/dist/utils/throttle.mjs +23 -0
- package/dist/utils/to-array.d.ts +1 -1
- package/dist/utils/to-array.mjs +12 -0
- package/dist/utils/to-function.d.ts +1 -0
- package/dist/utils/to-function.mjs +11 -0
- package/package.json +2 -2
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
|
+
import type { State } from "../types";
|
|
3
|
+
type UsedBoolean = [
|
|
4
|
+
boolean,
|
|
5
|
+
{
|
|
6
|
+
turnOn: () => void;
|
|
7
|
+
turnOff: () => void;
|
|
8
|
+
toggle: () => void;
|
|
9
|
+
setBoolean: Dispatch<SetStateAction<boolean>>;
|
|
10
|
+
}
|
|
11
|
+
];
|
|
2
12
|
/**
|
|
3
13
|
* @author murukal
|
|
4
14
|
*
|
|
5
15
|
* @description
|
|
6
|
-
* boolean state
|
|
16
|
+
* boolean state, in relax, we already create some api to easy use
|
|
7
17
|
*/
|
|
8
|
-
export declare const useBoolean: (
|
|
9
|
-
|
|
10
|
-
turnOn: () => void;
|
|
11
|
-
turnOff: () => void;
|
|
12
|
-
toggle: () => void;
|
|
13
|
-
setIsOn: import("react").Dispatch<import("react").SetStateAction<boolean>>;
|
|
14
|
-
};
|
|
18
|
+
export declare const useBoolean: (initialState?: State<boolean>) => UsedBoolean;
|
|
19
|
+
export {};
|
|
@@ -4,20 +4,19 @@ import { useState, useCallback } from 'react';
|
|
|
4
4
|
* @author murukal
|
|
5
5
|
*
|
|
6
6
|
* @description
|
|
7
|
-
* boolean state
|
|
7
|
+
* boolean state, in relax, we already create some api to easy use
|
|
8
8
|
*/
|
|
9
|
-
const useBoolean = (
|
|
10
|
-
const [isOn, setIsOn] = useState(
|
|
9
|
+
const useBoolean = (initialState) => {
|
|
10
|
+
const [isOn, setIsOn] = useState(initialState !== null && initialState !== void 0 ? initialState : false);
|
|
11
11
|
const turnOn = useCallback(() => setIsOn(true), []);
|
|
12
12
|
const turnOff = useCallback(() => setIsOn(false), []);
|
|
13
13
|
const toggle = useCallback(() => setIsOn((_isOn) => !_isOn), []);
|
|
14
|
-
return {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
14
|
+
return [isOn, {
|
|
15
|
+
turnOn,
|
|
16
|
+
turnOff,
|
|
17
|
+
toggle,
|
|
18
|
+
setBoolean: setIsOn
|
|
19
|
+
}];
|
|
21
20
|
};
|
|
22
21
|
|
|
23
22
|
export { useBoolean };
|
|
@@ -1,34 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { type Dispatch, type SetStateAction } from 'react';
|
|
2
|
+
import type { State } from '../types';
|
|
3
|
+
type Props = {
|
|
3
4
|
/**
|
|
4
5
|
* @description
|
|
5
6
|
* initial value
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
+
initialState?: State<number>;
|
|
8
9
|
/**
|
|
9
10
|
* @description
|
|
10
|
-
* max
|
|
11
|
+
* max: count will not be greater than max
|
|
11
12
|
*/
|
|
12
13
|
max?: number;
|
|
13
14
|
/**
|
|
14
15
|
* @description
|
|
15
|
-
* min
|
|
16
|
+
* min: count will not be smaller than min
|
|
16
17
|
*/
|
|
17
18
|
min?: number;
|
|
18
|
-
}
|
|
19
|
+
};
|
|
20
|
+
type UsedCounter = [
|
|
21
|
+
number,
|
|
22
|
+
{
|
|
23
|
+
add: (step?: number) => void;
|
|
24
|
+
subtract: (step?: number) => void;
|
|
25
|
+
first: () => void;
|
|
26
|
+
last: () => void;
|
|
27
|
+
reset: () => void;
|
|
28
|
+
setCount: Dispatch<SetStateAction<number>>;
|
|
29
|
+
}
|
|
30
|
+
];
|
|
19
31
|
/**
|
|
20
32
|
* @author murukal
|
|
21
33
|
*
|
|
22
34
|
* @description
|
|
23
35
|
* a number counter with some useful apis
|
|
24
36
|
*/
|
|
25
|
-
export declare const useCounter: ({ max, min
|
|
26
|
-
count: number;
|
|
27
|
-
next: (step?: number) => void;
|
|
28
|
-
prev: (step?: number) => void;
|
|
29
|
-
first: () => void;
|
|
30
|
-
last: () => void;
|
|
31
|
-
reset: () => void;
|
|
32
|
-
setCount: import("react").Dispatch<import("react").SetStateAction<number>>;
|
|
33
|
-
};
|
|
37
|
+
export declare const useCounter: (initialState?: State<number>, { max, min }?: Props) => UsedCounter;
|
|
34
38
|
export {};
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { useMemo, useState, useCallback } from 'react';
|
|
1
|
+
import { useState, useCallback, useMemo } from 'react';
|
|
3
2
|
import { clamp } from '../utils/clamp.mjs';
|
|
3
|
+
import '../utils/debounce.mjs';
|
|
4
|
+
import 'rxjs';
|
|
5
|
+
import { useDefault } from './use-default.mjs';
|
|
6
|
+
import './use-scroll-locker.mjs';
|
|
7
|
+
import '../dom/scroll-to.mjs';
|
|
8
|
+
import './use-toggleable.mjs';
|
|
9
|
+
import 'react-is';
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
12
|
* @author murukal
|
|
@@ -8,34 +14,26 @@ import { clamp } from '../utils/clamp.mjs';
|
|
|
8
14
|
* @description
|
|
9
15
|
* a number counter with some useful apis
|
|
10
16
|
*/
|
|
11
|
-
const useCounter = (
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
setCount((prev) => Math.min(max, prev + step));
|
|
17
|
+
const useCounter = (initialState, { max = Infinity, min = 0 } = { max: Infinity, min: 0 }) => {
|
|
18
|
+
const defaultState = useDefault(initialState !== null && initialState !== void 0 ? initialState : 0);
|
|
19
|
+
const [_count, _setCount] = useState(defaultState);
|
|
20
|
+
const add = useCallback((step = 1) => {
|
|
21
|
+
_setCount((prev) => Math.min(max, prev + step));
|
|
17
22
|
}, [max]);
|
|
18
|
-
const
|
|
19
|
-
|
|
23
|
+
const subtract = useCallback((step = 1) => {
|
|
24
|
+
_setCount((prev) => Math.max(min, prev - step));
|
|
20
25
|
}, [min]);
|
|
21
26
|
const first = useCallback(() => {
|
|
22
|
-
|
|
27
|
+
_setCount(min);
|
|
23
28
|
}, [min]);
|
|
24
29
|
const last = useCallback(() => {
|
|
25
|
-
|
|
30
|
+
_setCount(max);
|
|
26
31
|
}, [max]);
|
|
27
32
|
const reset = useCallback(() => {
|
|
28
|
-
|
|
29
|
-
}, [
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
next,
|
|
33
|
-
prev,
|
|
34
|
-
first,
|
|
35
|
-
last,
|
|
36
|
-
reset,
|
|
37
|
-
setCount
|
|
38
|
-
};
|
|
33
|
+
_setCount(defaultState);
|
|
34
|
+
}, []);
|
|
35
|
+
const count = useMemo(() => clamp(_count, min, max), [min, max]);
|
|
36
|
+
return [count, { add, subtract, first, last, reset, setCount: _setCount }];
|
|
39
37
|
};
|
|
40
38
|
|
|
41
39
|
export { useCounter };
|
|
@@ -1,24 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @description
|
|
4
|
-
* The delay time (in milliseconds) until the debounce function is called.
|
|
5
|
-
* default 1000
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* 2000
|
|
9
|
-
*/
|
|
10
|
-
readonly delay: number;
|
|
11
|
-
}
|
|
12
|
-
type Callable<T, R> = (value: T) => R;
|
|
1
|
+
import { type Debounced } from '../utils/debounce';
|
|
13
2
|
/**
|
|
14
3
|
* @author murukal
|
|
15
4
|
*
|
|
16
5
|
* @description
|
|
17
6
|
* debounce callback
|
|
7
|
+
*
|
|
8
|
+
* @param callback
|
|
9
|
+
* @param wait number
|
|
10
|
+
* @description
|
|
11
|
+
* The delay time (in milliseconds) until the debounce function is called.
|
|
12
|
+
* default 1000
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* 1000
|
|
18
16
|
*/
|
|
19
|
-
export declare const useDebounceCallback: <T
|
|
20
|
-
next: (value: T) => void;
|
|
21
|
-
complete: () => void;
|
|
22
|
-
cancel: () => void;
|
|
23
|
-
};
|
|
24
|
-
export {};
|
|
17
|
+
export declare const useDebounceCallback: <T extends Function>(callback: T, wait?: number) => Debounced<T>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useRef,
|
|
2
|
-
import {
|
|
1
|
+
import { useRef, useEffect, useMemo } from 'react';
|
|
2
|
+
import { debounce } from '../utils/debounce.mjs';
|
|
3
3
|
import { useEvent } from './use-event.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,51 +7,34 @@ import { useEvent } from './use-event.mjs';
|
|
|
7
7
|
*
|
|
8
8
|
* @description
|
|
9
9
|
* debounce callback
|
|
10
|
+
*
|
|
11
|
+
* @param callback
|
|
12
|
+
* @param wait number
|
|
13
|
+
* @description
|
|
14
|
+
* The delay time (in milliseconds) until the debounce function is called.
|
|
15
|
+
* default 1000
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* 1000
|
|
10
19
|
*/
|
|
11
|
-
const useDebounceCallback = (
|
|
12
|
-
var _a;
|
|
20
|
+
const useDebounceCallback = (callback, wait = 1000) => {
|
|
13
21
|
const trigger = useRef(null);
|
|
14
|
-
const
|
|
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) => {
|
|
19
|
-
trigger.current = subscriber;
|
|
20
|
-
})
|
|
21
|
-
.pipe(debounceTime(delay))
|
|
22
|
-
.subscribe({
|
|
23
|
-
next: callable,
|
|
24
|
-
complete: listen
|
|
25
|
-
});
|
|
26
|
-
listener.current = listened;
|
|
27
|
-
return listened;
|
|
28
|
-
}, [delay]);
|
|
22
|
+
const callable = useEvent(callback);
|
|
29
23
|
useEffect(() => {
|
|
30
|
-
const
|
|
24
|
+
const debounced = debounce(callable, wait);
|
|
25
|
+
trigger.current = debounced;
|
|
31
26
|
// dispose
|
|
32
27
|
return () => {
|
|
33
|
-
|
|
34
|
-
listener.current = null;
|
|
28
|
+
debounced.cancel();
|
|
35
29
|
trigger.current = null;
|
|
36
30
|
};
|
|
37
|
-
}, [
|
|
38
|
-
const
|
|
39
|
-
var _a;
|
|
40
|
-
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete();
|
|
45
|
-
});
|
|
46
|
-
const cancel = useEvent(() => {
|
|
47
|
-
var _a;
|
|
48
|
-
(_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
49
|
-
});
|
|
50
|
-
return {
|
|
51
|
-
next,
|
|
52
|
-
complete,
|
|
53
|
-
cancel
|
|
54
|
-
};
|
|
31
|
+
}, [wait]);
|
|
32
|
+
const debounced = useMemo(() => ({
|
|
33
|
+
next: ((...args) => { var _a; return (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(...args); }),
|
|
34
|
+
complete: () => { var _a; return (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete(); },
|
|
35
|
+
cancel: () => { var _a; return (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.cancel(); }
|
|
36
|
+
}), []);
|
|
37
|
+
return debounced;
|
|
55
38
|
};
|
|
56
39
|
|
|
57
40
|
export { useDebounceCallback };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useMemo } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { toFunction } from '../utils/to-function.mjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @author murukal
|
|
@@ -8,14 +8,7 @@ import { isStateGetter } from '../is/is-state-getter.mjs';
|
|
|
8
8
|
* state always be same after first render
|
|
9
9
|
*/
|
|
10
10
|
const useDefault = (initialState) => {
|
|
11
|
-
return useMemo(()
|
|
12
|
-
if (isStateGetter(initialState)) {
|
|
13
|
-
return initialState();
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
return initialState;
|
|
17
|
-
}
|
|
18
|
-
}, []);
|
|
11
|
+
return useMemo(toFunction(initialState), []);
|
|
19
12
|
};
|
|
20
13
|
|
|
21
14
|
export { useDefault };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useCallback } from 'react';
|
|
2
|
+
import { useBoolean } from './use-boolean.mjs';
|
|
3
|
+
|
|
4
|
+
const useHover = () => {
|
|
5
|
+
const [isHovered, { turnOn, turnOff }] = useBoolean(false);
|
|
6
|
+
const onPointerEnter = useCallback(() => {
|
|
7
|
+
turnOn();
|
|
8
|
+
}, []);
|
|
9
|
+
const onPointerLeave = useCallback(() => {
|
|
10
|
+
turnOff();
|
|
11
|
+
}, []);
|
|
12
|
+
return [isHovered, { onPointerEnter, onPointerLeave }];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { useHover };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
type Status = 'none' | 'loading' | 'error' | 'loaded';
|
|
2
|
-
interface
|
|
2
|
+
interface Props {
|
|
3
3
|
src: string;
|
|
4
4
|
}
|
|
5
5
|
/**
|
|
@@ -8,5 +8,5 @@ interface Dependencies {
|
|
|
8
8
|
* @description
|
|
9
9
|
* image loader
|
|
10
10
|
*/
|
|
11
|
-
export declare const useImageLoader: ({ src }:
|
|
11
|
+
export declare const useImageLoader: ({ src }: Props) => Status;
|
|
12
12
|
export {};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ThenableEffectCallback } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* @author murukal
|
|
4
4
|
*
|
|
5
5
|
* @description
|
|
6
6
|
* when components will mount
|
|
7
7
|
*/
|
|
8
|
-
export declare const useMount: (
|
|
8
|
+
export declare const useMount: (callback: ThenableEffectCallback) => void;
|
package/dist/hooks/use-mount.mjs
CHANGED
|
@@ -7,9 +7,9 @@ import { effect } from '../utils/effect.mjs';
|
|
|
7
7
|
* @description
|
|
8
8
|
* when components will mount
|
|
9
9
|
*/
|
|
10
|
-
const useMount = (
|
|
10
|
+
const useMount = (callback) => {
|
|
11
11
|
useLayoutEffect(() => {
|
|
12
|
-
return effect(
|
|
12
|
+
return effect(callback);
|
|
13
13
|
}, []);
|
|
14
14
|
};
|
|
15
15
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ThenableEffectCallback } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* @author murukal
|
|
4
4
|
*
|
|
5
5
|
* @description
|
|
6
6
|
* when components mounted
|
|
7
7
|
*/
|
|
8
|
-
export declare const useMounted: (
|
|
8
|
+
export declare const useMounted: (callback: ThenableEffectCallback) => void;
|
|
@@ -7,9 +7,9 @@ import { effect } from '../utils/effect.mjs';
|
|
|
7
7
|
* @description
|
|
8
8
|
* when components mounted
|
|
9
9
|
*/
|
|
10
|
-
const useMounted = (
|
|
10
|
+
const useMounted = (callback) => {
|
|
11
11
|
useEffect(() => {
|
|
12
|
-
return effect(
|
|
12
|
+
return effect(callback);
|
|
13
13
|
}, []);
|
|
14
14
|
};
|
|
15
15
|
|
|
@@ -3,25 +3,22 @@ import { useId, useLayoutEffect } from 'react';
|
|
|
3
3
|
import { isOverflow } from '../is/is-overflow.mjs';
|
|
4
4
|
import { isStyleElement } from '../is/is-style-element.mjs';
|
|
5
5
|
|
|
6
|
-
var _ScrollLocker_scrollLocker, _ScrollLocker_barSize;
|
|
6
|
+
var _a, _ScrollLocker_scrollLocker, _ScrollLocker_barSize;
|
|
7
7
|
const isComputable = (value) => /^(.*)px$/.test(value);
|
|
8
8
|
class ScrollLocker {
|
|
9
9
|
constructor() {
|
|
10
|
-
var
|
|
11
|
-
|
|
12
|
-
_ScrollLocker_scrollLocker.set(this, null
|
|
13
|
-
// bar size
|
|
14
|
-
);
|
|
10
|
+
var _b;
|
|
11
|
+
var _c;
|
|
15
12
|
// bar size
|
|
16
13
|
_ScrollLocker_barSize.set(this, null);
|
|
17
|
-
return (__classPrivateFieldSet(
|
|
14
|
+
return (__classPrivateFieldSet(_c = _a, _a, (_b = __classPrivateFieldGet(_c, _a, "f", _ScrollLocker_scrollLocker)) !== null && _b !== void 0 ? _b : this, "f", _ScrollLocker_scrollLocker));
|
|
18
15
|
}
|
|
19
16
|
get barSize() {
|
|
20
|
-
var
|
|
17
|
+
var _b;
|
|
21
18
|
if (__classPrivateFieldGet(this, _ScrollLocker_barSize, "f"))
|
|
22
19
|
return __classPrivateFieldGet(this, _ScrollLocker_barSize, "f");
|
|
23
20
|
const { width, height } = getComputedStyle(document.body, '::-webkit-scrollbar');
|
|
24
|
-
return (__classPrivateFieldSet(this, _ScrollLocker_barSize, (
|
|
21
|
+
return (__classPrivateFieldSet(this, _ScrollLocker_barSize, (_b = __classPrivateFieldGet(this, _ScrollLocker_barSize, "f")) !== null && _b !== void 0 ? _b : {
|
|
25
22
|
width: isComputable(width) ? width : '0',
|
|
26
23
|
height: isComputable(height) ? height : '0'
|
|
27
24
|
}, "f"));
|
|
@@ -64,7 +61,9 @@ class ScrollLocker {
|
|
|
64
61
|
this.container.removeChild(locked);
|
|
65
62
|
}
|
|
66
63
|
}
|
|
67
|
-
|
|
64
|
+
_a = ScrollLocker, _ScrollLocker_barSize = new WeakMap();
|
|
65
|
+
// singleton mode
|
|
66
|
+
_ScrollLocker_scrollLocker = { value: null };
|
|
68
67
|
const useScrollLocker = (isLock) => {
|
|
69
68
|
const id = useId();
|
|
70
69
|
useLayoutEffect(() => {
|
|
@@ -1,24 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* @description
|
|
4
|
-
* The delay time (in milliseconds) until the throttle function is called.
|
|
5
|
-
* default 1000
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* 2000
|
|
9
|
-
*/
|
|
10
|
-
readonly duration: number;
|
|
11
|
-
}
|
|
12
|
-
type Callable<T, R> = (value: T) => R;
|
|
1
|
+
import { type Throttled } from '../utils/throttle';
|
|
13
2
|
/**
|
|
14
3
|
* @author murukal
|
|
15
4
|
*
|
|
16
5
|
* @description
|
|
17
6
|
* throttle callback
|
|
7
|
+
*
|
|
8
|
+
* @param callback
|
|
9
|
+
* @param duration number
|
|
10
|
+
* @description
|
|
11
|
+
* The duration time (in milliseconds) until the throttle function is called.
|
|
12
|
+
* default 1000
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* 1000
|
|
18
16
|
*/
|
|
19
|
-
export declare const useThrottleCallback: <T
|
|
20
|
-
next: (value: T) => void;
|
|
21
|
-
complete: () => void;
|
|
22
|
-
cancel: () => void;
|
|
23
|
-
};
|
|
24
|
-
export {};
|
|
17
|
+
export declare const useThrottleCallback: <T extends Function>(callback: T, duration?: number) => Throttled<T>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useRef,
|
|
2
|
-
import {
|
|
1
|
+
import { useRef, useEffect, useMemo } from 'react';
|
|
2
|
+
import { throttle } from '../utils/throttle.mjs';
|
|
3
3
|
import { useEvent } from './use-event.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,51 +7,34 @@ import { useEvent } from './use-event.mjs';
|
|
|
7
7
|
*
|
|
8
8
|
* @description
|
|
9
9
|
* throttle callback
|
|
10
|
+
*
|
|
11
|
+
* @param callback
|
|
12
|
+
* @param duration number
|
|
13
|
+
* @description
|
|
14
|
+
* The duration time (in milliseconds) until the throttle function is called.
|
|
15
|
+
* default 1000
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* 1000
|
|
10
19
|
*/
|
|
11
|
-
const useThrottleCallback = (
|
|
12
|
-
var _a;
|
|
20
|
+
const useThrottleCallback = (callback, duration = 1000) => {
|
|
13
21
|
const trigger = useRef(null);
|
|
14
|
-
const
|
|
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;
|
|
20
|
-
})
|
|
21
|
-
.pipe(throttleTime(duration))
|
|
22
|
-
.subscribe({
|
|
23
|
-
next: callable,
|
|
24
|
-
complete: listen
|
|
25
|
-
});
|
|
26
|
-
listener.current = listened;
|
|
27
|
-
return listened;
|
|
28
|
-
}, [duration]);
|
|
22
|
+
const callable = useEvent(callback);
|
|
29
23
|
useEffect(() => {
|
|
30
|
-
const
|
|
24
|
+
const throttled = throttle(callable, duration);
|
|
25
|
+
trigger.current = throttled;
|
|
31
26
|
// dispose
|
|
32
27
|
return () => {
|
|
33
|
-
|
|
34
|
-
listener.current = null;
|
|
28
|
+
throttled.cancel();
|
|
35
29
|
trigger.current = null;
|
|
36
30
|
};
|
|
37
|
-
}, [
|
|
38
|
-
const
|
|
39
|
-
var _a;
|
|
40
|
-
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
(_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete();
|
|
45
|
-
});
|
|
46
|
-
const cancel = useEvent(() => {
|
|
47
|
-
var _a;
|
|
48
|
-
(_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
|
49
|
-
});
|
|
50
|
-
return {
|
|
51
|
-
next,
|
|
52
|
-
complete,
|
|
53
|
-
cancel
|
|
54
|
-
};
|
|
31
|
+
}, [duration]);
|
|
32
|
+
const throttled = useMemo(() => ({
|
|
33
|
+
next: ((...args) => { var _a; return (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(...args); }),
|
|
34
|
+
complete: () => { var _a; return (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete(); },
|
|
35
|
+
cancel: () => { var _a; return (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.cancel(); }
|
|
36
|
+
}), []);
|
|
37
|
+
return throttled;
|
|
55
38
|
};
|
|
56
39
|
|
|
57
40
|
export { useThrottleCallback };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* @author murukal
|
|
@@ -7,13 +7,12 @@ import { useRef, useEffect } from 'react';
|
|
|
7
7
|
* timeout effect
|
|
8
8
|
*/
|
|
9
9
|
const useTimeout = (handler, { duration }) => {
|
|
10
|
-
const timer = useRef(null);
|
|
11
10
|
useEffect(() => {
|
|
12
|
-
timer
|
|
11
|
+
const timer = setTimeout(handler, duration);
|
|
13
12
|
return () => {
|
|
14
|
-
if (!timer
|
|
13
|
+
if (!timer)
|
|
15
14
|
return;
|
|
16
|
-
clearTimeout(timer
|
|
15
|
+
clearTimeout(timer);
|
|
17
16
|
};
|
|
18
17
|
}, [duration]);
|
|
19
18
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export { useToggleable } from './hooks/use-toggleable';
|
|
|
19
19
|
export { useEvent } from './hooks/use-event';
|
|
20
20
|
export { useUpdateEffect } from './hooks/use-update-effect';
|
|
21
21
|
export { useCounter } from './hooks/use-counter';
|
|
22
|
+
export { useHover } from './hooks/use-hover';
|
|
22
23
|
/**
|
|
23
24
|
* @description
|
|
24
25
|
* is
|
|
@@ -46,3 +47,6 @@ export { unique, uniqueBy } from './utils/unique';
|
|
|
46
47
|
export { range } from './utils/range';
|
|
47
48
|
export { clamp } from './utils/clamp';
|
|
48
49
|
export { chain } from './utils/chain';
|
|
50
|
+
export { debounce } from './utils/debounce';
|
|
51
|
+
export { toArray } from './utils/to-array';
|
|
52
|
+
export { toFunction } from './utils/to-function';
|
package/dist/index.mjs
CHANGED
|
@@ -15,6 +15,7 @@ export { useToggleable } from './hooks/use-toggleable.mjs';
|
|
|
15
15
|
export { useEvent } from './hooks/use-event.mjs';
|
|
16
16
|
export { useUpdateEffect } from './hooks/use-update-effect.mjs';
|
|
17
17
|
export { useCounter } from './hooks/use-counter.mjs';
|
|
18
|
+
export { useHover } from './hooks/use-hover.mjs';
|
|
18
19
|
export { isRefable } from './is/is-refable.mjs';
|
|
19
20
|
export { isUndefined } from './is/is-undefined.mjs';
|
|
20
21
|
export { isStateGetter } from './is/is-state-getter.mjs';
|
|
@@ -34,3 +35,6 @@ export { unique, uniqueBy } from './utils/unique.mjs';
|
|
|
34
35
|
export { range } from './utils/range.mjs';
|
|
35
36
|
export { clamp } from './utils/clamp.mjs';
|
|
36
37
|
export { chain } from './utils/chain.mjs';
|
|
38
|
+
export { debounce } from './utils/debounce.mjs';
|
|
39
|
+
export { toArray } from './utils/to-array.mjs';
|
|
40
|
+
export { toFunction } from './utils/to-function.mjs';
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
type StateGetter
|
|
2
|
-
/**
|
|
3
|
-
* @description
|
|
4
|
-
* state setter
|
|
5
|
-
*
|
|
6
|
-
* used by initial state, default state, controlled state
|
|
7
|
-
*/
|
|
8
|
-
export type State<T> = T | StateGetter<T>;
|
|
1
|
+
import type { State, StateGetter } from "../types";
|
|
9
2
|
/**
|
|
10
3
|
* @description
|
|
11
4
|
* is state getter
|
|
12
5
|
*/
|
|
13
6
|
export declare const isStateGetter: <T>(state: State<T>) => state is StateGetter<T>;
|
|
14
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export type Arguments<T extends Function> = T extends (...args: infer A) => unknown ? A :
|
|
1
|
+
export type Arguments<T extends Function> = T extends (...args: infer A) => unknown ? A : [];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type First<T> = T extends [infer D, ...Array<unknown>] ? D : undefined;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,3 +3,5 @@ export type { Nullable } from './nullable';
|
|
|
3
3
|
export type { Partialable } from './partialable';
|
|
4
4
|
export type { RequiredIn } from './required-in';
|
|
5
5
|
export type { ThenableEffectCallback } from './thenable-effect-callback';
|
|
6
|
+
export type { First } from './first';
|
|
7
|
+
export type { State, StateGetter } from './state';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Subscriber } from 'rxjs';
|
|
2
|
+
export interface Debounced<T> {
|
|
3
|
+
/**
|
|
4
|
+
* @description
|
|
5
|
+
* value trigger
|
|
6
|
+
*/
|
|
7
|
+
next: T;
|
|
8
|
+
/**
|
|
9
|
+
* @description
|
|
10
|
+
* complete current debounce function
|
|
11
|
+
* this function will not be usable
|
|
12
|
+
*/
|
|
13
|
+
complete: () => void;
|
|
14
|
+
/**
|
|
15
|
+
* @description
|
|
16
|
+
* ignore any value after call cancel
|
|
17
|
+
* this function will not be usable
|
|
18
|
+
*/
|
|
19
|
+
cancel: () => void;
|
|
20
|
+
}
|
|
21
|
+
export declare class Trigger<T> {
|
|
22
|
+
#private;
|
|
23
|
+
constructor();
|
|
24
|
+
set use(subscriber: Subscriber<T>);
|
|
25
|
+
next(value: T): void;
|
|
26
|
+
error(error?: unknown): void;
|
|
27
|
+
complete(): void;
|
|
28
|
+
}
|
|
29
|
+
export declare const debounce: <T extends Function>(callback: T, wait: number) => Debounced<T>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { __classPrivateFieldSet, __classPrivateFieldGet } from '../node_modules/tslib/tslib.es6.mjs';
|
|
2
|
+
import { Observable, debounceTime } from 'rxjs';
|
|
3
|
+
|
|
4
|
+
var _Trigger_next, _Trigger_error, _Trigger_complete;
|
|
5
|
+
class Trigger {
|
|
6
|
+
constructor() {
|
|
7
|
+
_Trigger_next.set(this, void 0);
|
|
8
|
+
_Trigger_error.set(this, void 0);
|
|
9
|
+
_Trigger_complete.set(this, void 0);
|
|
10
|
+
__classPrivateFieldSet(this, _Trigger_next, null, "f");
|
|
11
|
+
__classPrivateFieldSet(this, _Trigger_error, null, "f");
|
|
12
|
+
__classPrivateFieldSet(this, _Trigger_complete, null, "f");
|
|
13
|
+
}
|
|
14
|
+
set use(subscriber) {
|
|
15
|
+
__classPrivateFieldSet(this, _Trigger_next, (value) => subscriber.next(value), "f");
|
|
16
|
+
__classPrivateFieldSet(this, _Trigger_error, (error) => subscriber.error(error), "f");
|
|
17
|
+
__classPrivateFieldSet(this, _Trigger_complete, () => subscriber.complete(), "f");
|
|
18
|
+
}
|
|
19
|
+
next(value) {
|
|
20
|
+
var _a;
|
|
21
|
+
(_a = __classPrivateFieldGet(this, _Trigger_next, "f")) === null || _a === void 0 ? void 0 : _a.call(this, value);
|
|
22
|
+
}
|
|
23
|
+
error(error) {
|
|
24
|
+
var _a;
|
|
25
|
+
(_a = __classPrivateFieldGet(this, _Trigger_error, "f")) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
|
26
|
+
}
|
|
27
|
+
complete() {
|
|
28
|
+
var _a;
|
|
29
|
+
(_a = __classPrivateFieldGet(this, _Trigger_complete, "f")) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
_Trigger_next = new WeakMap(), _Trigger_error = new WeakMap(), _Trigger_complete = new WeakMap();
|
|
33
|
+
const debounce = (callback, wait) => {
|
|
34
|
+
const trigger = new Trigger();
|
|
35
|
+
const listened = new Observable((subscriber) => {
|
|
36
|
+
trigger.use = subscriber;
|
|
37
|
+
})
|
|
38
|
+
.pipe(debounceTime(wait))
|
|
39
|
+
.subscribe(() => {
|
|
40
|
+
callback();
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
next: trigger.next.bind(trigger),
|
|
44
|
+
complete: trigger.complete.bind(trigger),
|
|
45
|
+
cancel: () => {
|
|
46
|
+
listened.unsubscribe();
|
|
47
|
+
trigger.error();
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { Trigger, debounce };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Observable, throttleTime } from 'rxjs';
|
|
2
|
+
import { Trigger } from './debounce.mjs';
|
|
3
|
+
|
|
4
|
+
const throttle = (callback, wait) => {
|
|
5
|
+
const trigger = new Trigger();
|
|
6
|
+
const listened = new Observable((subscriber) => {
|
|
7
|
+
trigger.use = subscriber;
|
|
8
|
+
})
|
|
9
|
+
.pipe(throttleTime(wait))
|
|
10
|
+
.subscribe(() => {
|
|
11
|
+
callback();
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
next: trigger.next.bind(trigger),
|
|
15
|
+
complete: trigger.complete.bind(trigger),
|
|
16
|
+
cancel: () => {
|
|
17
|
+
listened.unsubscribe();
|
|
18
|
+
trigger.error();
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { throttle };
|
package/dist/utils/to-array.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const toFunction: <T extends Function>(value: unknown) => T;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiszlab/relax",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.26",
|
|
4
4
|
"description": "react utils collection",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@babel/core": "^7.22.5",
|
|
29
|
-
"@babel/preset-env": "^7.
|
|
29
|
+
"@babel/preset-env": "^7.24.0",
|
|
30
30
|
"@babel/preset-typescript": "^7.22.5",
|
|
31
31
|
"@rollup/plugin-babel": "^6.0.3",
|
|
32
32
|
"@rollup/plugin-node-resolve": "^15.1.0",
|