@aiszlab/relax 1.2.23 → 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.
@@ -1,24 +1,17 @@
1
- interface Options {
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, R>(_callable: Callable<T, R>, options?: Options) => {
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, useCallback, useEffect } from 'react';
2
- import { Observable, debounceTime } from 'rxjs';
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 = (_callable, options) => {
12
- var _a;
20
+ const useDebounceCallback = (callback, wait = 1000) => {
13
21
  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) => {
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 listened = listen();
24
+ const debounced = debounce(callable, wait);
25
+ trigger.current = debounced;
31
26
  // dispose
32
27
  return () => {
33
- listened.unsubscribe();
34
- listener.current = null;
28
+ debounced.cancel();
35
29
  trigger.current = null;
36
30
  };
37
- }, [listen]);
38
- const next = useEvent((value) => {
39
- var _a;
40
- (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(value);
41
- });
42
- const complete = useEvent(() => {
43
- var _a;
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,5 @@
1
+ export declare const useHover: () => {
2
+ isHovered: boolean;
3
+ onPointerEnter: () => void;
4
+ onPointerLeave: () => void;
5
+ };
@@ -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 { EffectCallback } from 'react';
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: (callable: EffectCallback | UnderlyingSinkCloseCallback) => void;
8
+ export declare const useMount: (callback: ThenableEffectCallback) => void;
@@ -1,5 +1,5 @@
1
1
  import { useLayoutEffect } from 'react';
2
- import { callAsEffect } from '../utils/thenable-effect-callback.mjs';
2
+ import { effect } from '../utils/effect.mjs';
3
3
 
4
4
  /**
5
5
  * @author murukal
@@ -7,9 +7,9 @@ import { callAsEffect } from '../utils/thenable-effect-callback.mjs';
7
7
  * @description
8
8
  * when components will mount
9
9
  */
10
- const useMount = (callable) => {
10
+ const useMount = (callback) => {
11
11
  useLayoutEffect(() => {
12
- return callAsEffect(callable);
12
+ return effect(callback);
13
13
  }, []);
14
14
  };
15
15
 
@@ -1,8 +1,8 @@
1
- import { EffectCallback } from 'react';
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: (callable: EffectCallback | UnderlyingSinkCloseCallback) => void;
8
+ export declare const useMounted: (callback: ThenableEffectCallback) => void;
@@ -1,5 +1,5 @@
1
1
  import { useEffect } from 'react';
2
- import { callAsEffect } from '../utils/thenable-effect-callback.mjs';
2
+ import { effect } from '../utils/effect.mjs';
3
3
 
4
4
  /**
5
5
  * @author murukal
@@ -7,9 +7,9 @@ import { callAsEffect } from '../utils/thenable-effect-callback.mjs';
7
7
  * @description
8
8
  * when components mounted
9
9
  */
10
- const useMounted = (callable) => {
10
+ const useMounted = (callback) => {
11
11
  useEffect(() => {
12
- return callAsEffect(callable);
12
+ return effect(callback);
13
13
  }, []);
14
14
  };
15
15
 
@@ -1,5 +1,5 @@
1
1
  import { type MutableRefObject, type RefCallback } from 'react';
2
- import type { Nullable } from '../utils/null-able';
2
+ import type { Nullable } from '../types';
3
3
  type Refable<T> = RefCallback<T> | MutableRefObject<T>;
4
4
  export declare const useRefs: <T>(...refs: Nullable<Refable<Nullable<T>>>[]) => (trigger: T) => void;
5
5
  export {};
@@ -1,24 +1,17 @@
1
- interface Options {
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, R>(_callable: Callable<T, R>, options?: Options) => {
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, useCallback, useEffect } from 'react';
2
- import { Observable, throttleTime } from 'rxjs';
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 = (_callable, options) => {
12
- var _a;
20
+ const useThrottleCallback = (callback, duration = 1000) => {
13
21
  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;
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 listened = listen();
24
+ const throttled = throttle(callable, duration);
25
+ trigger.current = throttled;
31
26
  // dispose
32
27
  return () => {
33
- listened.unsubscribe();
34
- listener.current = null;
28
+ throttled.cancel();
35
29
  trigger.current = null;
36
30
  };
37
- }, [listen]);
38
- const next = useEvent((value) => {
39
- var _a;
40
- (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(value);
41
- });
42
- const complete = useEvent(() => {
43
- var _a;
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 { Key } from 'react';
1
+ import { type Key } from 'react';
2
2
  /**
3
3
  * @description
4
4
  * toggleable key
@@ -1,3 +1,3 @@
1
1
  import { DependencyList } from 'react';
2
- import { ThenableEffectCallback } from '../utils/thenable-effect-callback';
2
+ import type { ThenableEffectCallback } from '../types';
3
3
  export declare const useUpdateEffect: (callable: ThenableEffectCallback, deps?: DependencyList) => void;
@@ -1,13 +1,13 @@
1
1
  import { useRef, useEffect } from 'react';
2
2
  import { useMounted } from './use-mounted.mjs';
3
- import { callAsEffect } from '../utils/thenable-effect-callback.mjs';
3
+ import { effect } from '../utils/effect.mjs';
4
4
 
5
5
  const useUpdateEffect = (callable, deps) => {
6
6
  const isMounted = useRef(false);
7
7
  useEffect(() => {
8
8
  if (!isMounted.current)
9
9
  return;
10
- return callAsEffect(callable);
10
+ return effect(callable);
11
11
  }, deps);
12
12
  useMounted(() => {
13
13
  isMounted.current = true;
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
@@ -41,11 +42,9 @@ export { isThenable } from './is/is-thenable';
41
42
  * @description
42
43
  * utils
43
44
  */
44
- export { type Nullable } from './utils/null-able';
45
- export { type Partialable } from './utils/partial-able';
46
- export { type RequiredIn } from './utils/required-in';
47
- export { type ThenableEffectCallback, callAsEffect } from './utils/thenable-effect-callback';
45
+ export { effect } from './utils/effect';
48
46
  export { unique, uniqueBy } from './utils/unique';
49
47
  export { range } from './utils/range';
50
48
  export { clamp } from './utils/clamp';
51
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';
@@ -29,8 +30,9 @@ export { isOverflow } from './is/is-overflow.mjs';
29
30
  export { isStyleElement } from './is/is-style-element.mjs';
30
31
  export { isFunction } from './is/is-function.mjs';
31
32
  export { isThenable } from './is/is-thenable.mjs';
32
- export { callAsEffect } from './utils/thenable-effect-callback.mjs';
33
+ export { effect } from './utils/effect.mjs';
33
34
  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 : never;
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;
@@ -0,0 +1,6 @@
1
+ export type { Arguments } from './arguments';
2
+ export type { Nullable } from './nullable';
3
+ export type { Partialable } from './partialable';
4
+ export type { RequiredIn } from './required-in';
5
+ export type { ThenableEffectCallback } from './thenable-effect-callback';
6
+ export type { First } from './first';
@@ -4,8 +4,3 @@ import type { EffectCallback } from 'react';
4
4
  * thenable effect callback
5
5
  */
6
6
  export type ThenableEffectCallback = () => ReturnType<EffectCallback> | PromiseLike<ReturnType<EffectCallback>>;
7
- /**
8
- * @description
9
- * call thenable effect callback
10
- */
11
- export declare const callAsEffect: (callable: ThenableEffectCallback) => ReturnType<EffectCallback>;
@@ -1,4 +1,4 @@
1
- import type { Partialable } from '../utils/partial-able';
1
+ import type { Partialable } from '../types';
2
2
  type Callable<R> = (...args: R[]) => void;
3
3
  export declare const chain: <S>(...callbacks: Partialable<Callable<S>>[]) => Callable<S>;
4
4
  export {};
@@ -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,7 @@
1
+ import type { EffectCallback } from 'react';
2
+ import type { ThenableEffectCallback } from '../types';
3
+ /**
4
+ * @description
5
+ * call thenable effect
6
+ */
7
+ export declare const effect: (callable: ThenableEffectCallback) => ReturnType<EffectCallback>;
@@ -2,9 +2,9 @@ import { isThenable } from '../is/is-thenable.mjs';
2
2
 
3
3
  /**
4
4
  * @description
5
- * call thenable effect callback
5
+ * call thenable effect
6
6
  */
7
- const callAsEffect = (callable) => {
7
+ const effect = (callable) => {
8
8
  const called = callable();
9
9
  // if result is void
10
10
  if (!called)
@@ -15,4 +15,4 @@ const callAsEffect = (callable) => {
15
15
  return called;
16
16
  };
17
17
 
18
- export { callAsEffect };
18
+ export { effect };
@@ -0,0 +1,3 @@
1
+ import { type Debounced } from './debounce';
2
+ export type Throttled<T> = Debounced<T>;
3
+ export declare const throttle: <T extends Function>(callback: T, wait: number) => Throttled<T>;
@@ -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 };
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @description
3
+ * convert any type data to a array
4
+ */
5
+ export declare const toArray: <T extends unknown[]>(value: unknown) => T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.2.23",
3
+ "version": "1.2.25",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "exports": {
@@ -11,7 +11,8 @@
11
11
  "./dom": {
12
12
  "types": "./dist/dom/index.d.ts",
13
13
  "default": "./dist/dom/index.mjs"
14
- }
14
+ },
15
+ "./types": "./dist/types/index.d.ts"
15
16
  },
16
17
  "scripts": {
17
18
  "dev": "rollup -c -w",
@@ -33,7 +34,7 @@
33
34
  "@types/react": "^18",
34
35
  "@types/react-dom": "^18",
35
36
  "@types/react-is": "^18",
36
- "rollup": "^3.27.0",
37
+ "rollup": "^4.12.0",
37
38
  "typescript": "^5.1.3"
38
39
  },
39
40
  "peerDependencies": {
File without changes