miolo-react 3.0.0-beta.167 → 3.0.0-beta.169
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 +1 -1
- package/src/ssr/usePropsCheck.mjs +69 -0
- package/src/ssr/useSsrDataOrReload.mjs +40 -21
package/package.json
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react"
|
|
2
|
+
|
|
3
|
+
export default function usePropsCheck(loader, effect, modifier) {
|
|
4
|
+
const prevLoader = useRef(loader)
|
|
5
|
+
const prevEffect = useRef(effect)
|
|
6
|
+
const prevModifier = useRef(modifier)
|
|
7
|
+
const loaderChangeCount = useRef(0)
|
|
8
|
+
const effectChangeCount = useRef(0)
|
|
9
|
+
const modifierChangeCount = useRef(0)
|
|
10
|
+
const loaderChangeTime = useRef(0)
|
|
11
|
+
const effectChangeTime = useRef(0)
|
|
12
|
+
const modifierChangeTime = useRef(0)
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (prevLoader.current !== undefined && prevLoader.current !== loader) {
|
|
16
|
+
const now = Date.now()
|
|
17
|
+
// Si el cambio ocurrió rápido (<100ms desde el último cambio), sumamos al contador continuo
|
|
18
|
+
if (now - loaderChangeTime.current < 100) {
|
|
19
|
+
loaderChangeCount.current += 1
|
|
20
|
+
if (loaderChangeCount.current >= 4) {
|
|
21
|
+
console.warn(
|
|
22
|
+
"🚨 [miolo][useSsrDataOrReload]: 'options.loader' varies too frequently. Wrap it in a useCallback!"
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
} else {
|
|
26
|
+
// Ha pasado suficiente tiempo como para ser un cambio humano/aislado
|
|
27
|
+
loaderChangeCount.current = 1
|
|
28
|
+
}
|
|
29
|
+
loaderChangeTime.current = now
|
|
30
|
+
}
|
|
31
|
+
prevLoader.current = loader
|
|
32
|
+
}) // Evalúa en cada render
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (prevEffect.current !== undefined && prevEffect.current !== effect) {
|
|
36
|
+
const now = Date.now()
|
|
37
|
+
if (now - effectChangeTime.current < 100) {
|
|
38
|
+
effectChangeCount.current += 1
|
|
39
|
+
if (effectChangeCount.current >= 4) {
|
|
40
|
+
console.warn(
|
|
41
|
+
"🚨 [miolo][useSsrDataOrReload]: 'options.effect' varies too frequently. Wrap it in a useMemo or useCallback!"
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
effectChangeCount.current = 1
|
|
46
|
+
}
|
|
47
|
+
effectChangeTime.current = now
|
|
48
|
+
}
|
|
49
|
+
prevEffect.current = effect
|
|
50
|
+
}) // Evalúa en cada render
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (prevModifier.current !== undefined && prevModifier.current !== modifier) {
|
|
54
|
+
const now = Date.now()
|
|
55
|
+
if (now - modifierChangeTime.current < 100) {
|
|
56
|
+
modifierChangeCount.current += 1
|
|
57
|
+
if (modifierChangeCount.current >= 4) {
|
|
58
|
+
console.warn(
|
|
59
|
+
"🚨 [miolo][useSsrDataOrReload]: 'options.modifier' varies too frequently. Wrap it in a useCallback!"
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
modifierChangeCount.current = 1
|
|
64
|
+
}
|
|
65
|
+
modifierChangeTime.current = now
|
|
66
|
+
}
|
|
67
|
+
prevModifier.current = modifier
|
|
68
|
+
}) // Evalúa en cada render
|
|
69
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { useCallback, useEffect, useState } from "react"
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from "react"
|
|
2
2
|
import getSsrDataFromContext from "./getSsrDataFromContext.mjs"
|
|
3
|
+
import usePropsCheck from "./usePropsCheck.mjs"
|
|
3
4
|
|
|
4
5
|
const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
5
6
|
const { fetcher } = miolo
|
|
@@ -13,7 +14,9 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
13
14
|
model = undefined
|
|
14
15
|
} = options
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
usePropsCheck(loader, effect, modifier)
|
|
18
|
+
|
|
19
|
+
const parseData = useCallback(
|
|
17
20
|
(value) => {
|
|
18
21
|
let parsed = value
|
|
19
22
|
if (model !== undefined) {
|
|
@@ -27,58 +30,74 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
27
30
|
[modifier, model]
|
|
28
31
|
)
|
|
29
32
|
|
|
30
|
-
const ssrDataFromContext =
|
|
33
|
+
const ssrDataFromContext = useMemo(() => {
|
|
34
|
+
return getSsrDataFromContext(context, name)
|
|
35
|
+
}, [context, name])
|
|
36
|
+
|
|
31
37
|
const [ssrData, setSsrData] = useState(
|
|
32
|
-
|
|
38
|
+
parseData(ssrDataFromContext !== undefined ? ssrDataFromContext : defval)
|
|
33
39
|
)
|
|
34
40
|
|
|
35
|
-
const [
|
|
36
|
-
const [status, setStatus] = useState(needToRefresh ? "idle" : "loaded")
|
|
41
|
+
const [status, setStatus] = useState(ssrDataFromContext !== undefined ? "loaded" : "idle")
|
|
37
42
|
const [error, setError] = useState(undefined)
|
|
38
43
|
|
|
44
|
+
const updateSsrData = useCallback(
|
|
45
|
+
(data) => {
|
|
46
|
+
setStatus("loading")
|
|
47
|
+
setSsrData(parseData(data))
|
|
48
|
+
setStatus("loaded")
|
|
49
|
+
setError(undefined)
|
|
50
|
+
},
|
|
51
|
+
[parseData]
|
|
52
|
+
)
|
|
53
|
+
|
|
39
54
|
const refreshSsrData = useCallback(async () => {
|
|
55
|
+
if (status === "loading") {
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
40
59
|
setStatus("loading")
|
|
60
|
+
setError(undefined)
|
|
61
|
+
|
|
41
62
|
if (loader !== undefined) {
|
|
42
63
|
const nSsrData = await loader(context, fetcher)
|
|
43
|
-
setSsrData(
|
|
44
|
-
setStatus("loaded")
|
|
45
|
-
setError(undefined)
|
|
64
|
+
setSsrData(parseData(nSsrData))
|
|
46
65
|
} else {
|
|
47
66
|
if (!url) {
|
|
48
67
|
setError(`No url provided for ${name}`)
|
|
49
68
|
} else {
|
|
50
69
|
const resp = await fetcher.get(url, params)
|
|
51
70
|
if (resp.ok) {
|
|
52
|
-
setSsrData(
|
|
53
|
-
setStatus("loaded")
|
|
54
|
-
setError(undefined)
|
|
71
|
+
setSsrData(parseData(resp?.data))
|
|
55
72
|
} else {
|
|
56
73
|
setError(resp?.error || "Unknonwn error")
|
|
57
|
-
setStatus("loaded")
|
|
58
74
|
}
|
|
59
75
|
}
|
|
60
76
|
}
|
|
61
|
-
|
|
77
|
+
setStatus("loaded")
|
|
78
|
+
}, [status, context, fetcher, loader, url, params, parseData, name])
|
|
62
79
|
|
|
63
80
|
useEffect(() => {
|
|
64
81
|
try {
|
|
65
|
-
if (
|
|
66
|
-
const changed =
|
|
67
|
-
|
|
68
|
-
|
|
82
|
+
if (status === "idle") {
|
|
83
|
+
const changed =
|
|
84
|
+
effect === undefined ||
|
|
85
|
+
(typeof effect === "function" && effect() === true) ||
|
|
86
|
+
effect === true
|
|
87
|
+
if (changed === true) {
|
|
69
88
|
refreshSsrData()
|
|
70
89
|
}
|
|
71
90
|
}
|
|
72
91
|
} catch (_) {}
|
|
73
|
-
}, [
|
|
92
|
+
}, [status, refreshSsrData, effect])
|
|
74
93
|
|
|
75
94
|
return {
|
|
76
95
|
data: ssrData,
|
|
77
|
-
setData:
|
|
96
|
+
setData: updateSsrData,
|
|
78
97
|
refresh: refreshSsrData,
|
|
79
98
|
error,
|
|
80
99
|
ok: error === undefined,
|
|
81
|
-
ready: status === "loaded"
|
|
100
|
+
ready: status === "loaded"
|
|
82
101
|
}
|
|
83
102
|
}
|
|
84
103
|
|