@aiszlab/relax 1.2.24 → 1.2.25
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-debounce-callback.d.ts +11 -18
- package/dist/hooks/use-debounce-callback.mjs +23 -40
- package/dist/hooks/use-hover.d.ts +5 -0
- package/dist/hooks/use-hover.mjs +19 -0
- 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-throttle-callback.d.ts +11 -18
- package/dist/hooks/use-throttle-callback.mjs +23 -40
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/types/arguments.d.ts +1 -1
- package/dist/types/first.d.ts +1 -0
- package/dist/types/index.d.ts +1 -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/package.json +1 -1
|
@@ -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 };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useBoolean } from './use-boolean.mjs';
|
|
2
|
+
import { useEvent } from './use-event.mjs';
|
|
3
|
+
|
|
4
|
+
const useHover = () => {
|
|
5
|
+
const { isOn: isHovered, turnOff, turnOn } = useBoolean(false);
|
|
6
|
+
const onPointerEnter = useEvent(() => {
|
|
7
|
+
turnOn();
|
|
8
|
+
});
|
|
9
|
+
const onPointerLeave = useEvent(() => {
|
|
10
|
+
turnOff();
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
isHovered,
|
|
14
|
+
onPointerEnter,
|
|
15
|
+
onPointerLeave
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { useHover };
|
|
@@ -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
|
|
|
@@ -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 };
|
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,4 @@ 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';
|
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,4 @@ 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';
|
|
@@ -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
|
@@ -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