@codeleap/mobile 3.22.1 → 3.22.2

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/mobile",
3
- "version": "3.22.1",
3
+ "version": "3.22.2",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -11,3 +11,4 @@ export * from './input'
11
11
  export * from './theme'
12
12
  export * from './locale'
13
13
  export * from './Subscription'
14
+ export * from './useQueryListRefresh'
@@ -0,0 +1,60 @@
1
+ import React from 'react'
2
+ import { useFocusEffect } from '@react-navigation/native'
3
+ import { AnyFunction, TypeGuards, UseListEffect } from '@codeleap/common'
4
+
5
+ type useQueryListRefresh = (
6
+ listQuery: Parameters<UseListEffect>[0],
7
+ options?: {
8
+ staleTime?: number
9
+ silentRefresh?: boolean
10
+ initialStale?: boolean
11
+ cancelQueryEnabled?: boolean
12
+ refreshQueryEnabled?: boolean
13
+ onFocus?: AnyFunction
14
+ onBlur?: AnyFunction
15
+ }
16
+ ) => void
17
+
18
+ export const useQueryListRefresh: useQueryListRefresh = (listQuery, options = {}) => {
19
+ const {
20
+ staleTime = 5000,
21
+ initialStale = listQuery?.query?.isStale,
22
+ cancelQueryEnabled = true,
23
+ refreshQueryEnabled = true,
24
+ silentRefresh = false,
25
+ onFocus,
26
+ onBlur,
27
+ } = options
28
+
29
+ const staleRefetch = React.useRef(initialStale)
30
+ const staleTimeout = React.useRef(null)
31
+
32
+ useFocusEffect(
33
+ React.useCallback(() => {
34
+ if (staleRefetch.current && refreshQueryEnabled) {
35
+ listQuery?.refreshQuery(silentRefresh)
36
+ }
37
+
38
+ if (TypeGuards.isFunction(onFocus)) {
39
+ onFocus?.()
40
+ }
41
+
42
+ return () => {
43
+ if (cancelQueryEnabled) listQuery?.cancelQuery?.()
44
+ staleRefetch.current = false
45
+
46
+ if (staleTimeout.current == null) {
47
+ staleTimeout.current = setTimeout(() => {
48
+ staleRefetch.current = true
49
+ clearTimeout(staleTimeout.current)
50
+ staleTimeout.current = null
51
+ }, staleTime)
52
+ }
53
+
54
+ if (TypeGuards.isFunction(onBlur)) {
55
+ onBlur?.()
56
+ }
57
+ }
58
+ }, [])
59
+ )
60
+ }