@codeleap/hooks 5.8.20 → 6.0.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/hooks",
3
- "version": "5.8.20",
3
+ "version": "6.0.1",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -9,22 +9,22 @@
9
9
  "directory": "packages/hooks"
10
10
  },
11
11
  "devDependencies": {
12
- "@codeleap/config": "5.8.20",
13
- "@codeleap/types": "5.8.20",
14
- "@codeleap/utils": "5.8.20",
15
- "@codeleap/logger": "5.8.20",
12
+ "@codeleap/config": "6.0.1",
13
+ "@codeleap/types": "6.0.1",
14
+ "@codeleap/utils": "6.0.1",
15
+ "@codeleap/logger": "6.0.1",
16
16
  "ts-node-dev": "1.1.8"
17
17
  },
18
18
  "scripts": {
19
19
  "build": "echo 'No build needed'"
20
20
  },
21
21
  "peerDependencies": {
22
- "@codeleap/types": "5.8.20",
23
- "@codeleap/utils": "5.8.20",
24
- "@codeleap/logger": "5.8.20",
22
+ "@codeleap/types": "6.0.1",
23
+ "@codeleap/utils": "6.0.1",
24
+ "@codeleap/logger": "6.0.1",
25
25
  "axios": "^1.7.9",
26
26
  "typescript": "5.5.2",
27
- "react": "18.2.0",
27
+ "react": "19.1.0",
28
28
  "@tanstack/react-query": "5.89.0"
29
29
  }
30
30
  }
package/package.json.bak CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/hooks",
3
- "version": "5.8.20",
3
+ "version": "6.0.1",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -24,7 +24,7 @@
24
24
  "@codeleap/logger": "workspace:*",
25
25
  "axios": "^1.7.9",
26
26
  "typescript": "5.5.2",
27
- "react": "18.2.0",
27
+ "react": "19.1.0",
28
28
  "@tanstack/react-query": "5.89.0"
29
29
  }
30
30
  }
@@ -1,17 +1,38 @@
1
- import { useRef } from 'react'
1
+ import { useRef, useCallback, useEffect } from 'react'
2
2
  import { AnyFunction } from '@codeleap/types'
3
3
 
4
- export function useInterval(callback: AnyFunction, interval: number) {
5
- const intervalRef = useRef(null)
4
+ type UseIntervalHandler = (clear: AnyFunction) => void
6
5
 
7
- function clear() {
8
- clearInterval(intervalRef.current)
9
- intervalRef.current = null
10
- }
6
+ export function useInterval(handler: UseIntervalHandler, interval: number) {
7
+ const intervalRef = useRef<NodeJS.Timer | null>(null)
8
+ const handlerRef = useRef<UseIntervalHandler>(handler)
11
9
 
12
- function start() {
13
- intervalRef.current = setInterval(callback, interval)
14
- }
10
+ useEffect(() => {
11
+ handlerRef.current = handler
12
+ }, [handler])
13
+
14
+ const clear = useCallback(() => {
15
+ if (intervalRef.current != null) {
16
+ clearInterval(intervalRef.current)
17
+ intervalRef.current = null
18
+ }
19
+ }, [])
20
+
21
+ const start = useCallback(() => {
22
+ clear()
23
+
24
+ if (intervalRef.current == null) {
25
+ intervalRef.current = setInterval(() => {
26
+ handlerRef.current(clear)
27
+ }, interval)
28
+ }
29
+ }, [])
30
+
31
+ useEffect(() => {
32
+ return () => {
33
+ clear()
34
+ }
35
+ }, [clear])
15
36
 
16
37
  return {
17
38
  clear,
@@ -1,7 +1,7 @@
1
1
  import { useEffect, useRef } from 'react'
2
2
 
3
3
  export const usePrevious = <T>(value: T) => {
4
- const ref = useRef<T>()
4
+ const ref = useRef<T>(null)
5
5
  useEffect(() => {
6
6
  ref.current = value
7
7
  })
package/src/usePromise.ts CHANGED
@@ -9,8 +9,8 @@ type UsePromiseOptions<T = any> = {
9
9
  }
10
10
 
11
11
  export const usePromise = <T = any>(options?: UsePromiseOptions<T>) => {
12
- const rejectRef = useRef<AnyFunction>()
13
- const resolveRef = useRef<(v:T) => any>()
12
+ const rejectRef = useRef<AnyFunction>(null)
13
+ const resolveRef = useRef<(v:T) => any>(null)
14
14
  const timeoutRef = useRef(null)
15
15
  const reject = async (err: any) => {
16
16
  await rejectRef.current?.(err)