miolo-react 3.0.0-beta.225 → 3.0.0-beta.226
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 +2 -1
- package/src/ssr/useSsrDataOrReload.mjs +32 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "miolo-react",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.226",
|
|
4
4
|
"description": "React utils for miolo",
|
|
5
5
|
"author": "Donato Lorenzo <donato@afialapis.com>",
|
|
6
6
|
"contributors": [
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"reset": "rm -fr package-lock.json npm-lock.yaml node_modules && npm i",
|
|
28
28
|
"lint": "biome check ./src --reporter=github",
|
|
29
29
|
"lint:fix": "biome check --write ./src --reporter=github",
|
|
30
|
+
"lint:types": "tsc -p jsconfig.json --noEmit",
|
|
30
31
|
"prepublishOnly": "npm run lint"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
@@ -12,27 +12,27 @@ import usePropsCheck from "./usePropsCheck.mjs"
|
|
|
12
12
|
*
|
|
13
13
|
*
|
|
14
14
|
* @typedef {Object} MioloSSRDataOptions
|
|
15
|
-
* @property {any} [defval
|
|
16
|
-
* @property {Function} [loader] - A function that loads the data remotely. You need either @loader or @url.
|
|
17
|
-
* @property {string} [url] - The URL to load the data from. You need either @loader or @url.
|
|
18
|
-
* @property {Object} [params] - The parameters to pass to the @loader.
|
|
19
|
-
* @property {MioloModel} [model] - A MioloModel class for the data.
|
|
20
|
-
* @property {Function} [modifier] - A function that modifies the data (after ssr'ed or loaded, and after instantiated @model if any).
|
|
21
|
-
* @property {Function} [effect] - A function that is called when the data is loaded. The @effect function should return true if the data needs to be reloaded.
|
|
22
|
-
* @property {boolean} [cache
|
|
15
|
+
* @property {any} [defval] - The default value for the data.
|
|
16
|
+
* @property {Function} [loader] - A function that loads the data remotely. You need either {@link loader} or {@link url}.
|
|
17
|
+
* @property {string} [url] - The URL to load the data from. You need either {@link loader} or {@link url}.
|
|
18
|
+
* @property {Object} [params] - The parameters to pass to the {@link loader}.
|
|
19
|
+
* @property {new (...args: any[]) => MioloModel} [model] - A MioloModel class for the data.
|
|
20
|
+
* @property {Function} [modifier] - A function that modifies the data (after ssr'ed or loaded, and after instantiated {@link model} if any).
|
|
21
|
+
* @property {Function|boolean} [effect] - A function that is called when the data is loaded. The {@link effect} function should return true if the data needs to be reloaded.
|
|
22
|
+
* @property {boolean} [cache] - Whether to cache the data.
|
|
23
23
|
* @property {number} [ttl] - The time to live for the cached data in seconds.
|
|
24
|
-
* @property {boolean} [autoRefresh
|
|
24
|
+
* @property {boolean} [autoRefresh] - Whether to automatically refresh the data on cache expiration.
|
|
25
25
|
*
|
|
26
26
|
*
|
|
27
27
|
* @typedef {Object} MioloSSRData
|
|
28
|
-
* @property {MioloModel | MioloArray | any} data - The data state. Generaly defined by @model and/or @modifier options.
|
|
28
|
+
* @property {MioloModel | MioloArray | any} data - The data state. Generaly defined by {@link model} and/or {@link modifier} options.
|
|
29
29
|
* @property {Function} setData - Set the data state directly.
|
|
30
|
-
* @property {Function} refresh - If needed, remotely reload data (by calling @loader or @url).
|
|
31
|
-
* @property {Function} invalidate - Invalidate the data cache (requires @cache to be true).
|
|
30
|
+
* @property {Function} refresh - If needed, remotely reload data (by calling {@link loader} or {@link url}).
|
|
31
|
+
* @property {Function} invalidate - Invalidate the data cache (requires {@link cache} to be true).
|
|
32
32
|
* @property {string|undefined} error - The error message if any.
|
|
33
33
|
*
|
|
34
34
|
* @property {boolean} ok - Status of SSR data, true if everything was ssr'ed or remotely loaded ok.
|
|
35
|
-
* @property {boolean} ready - true when @data is loaded (either ssr'ed or remotely).
|
|
35
|
+
* @property {boolean} ready - true when {@link data} is loaded (either ssr'ed or remotely).
|
|
36
36
|
*/
|
|
37
37
|
|
|
38
38
|
const globalSocketState = {
|
|
@@ -230,7 +230,7 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
230
230
|
}
|
|
231
231
|
}, [ssrDataFromContext, cacheSet])
|
|
232
232
|
|
|
233
|
-
const handlersRef = useRef({})
|
|
233
|
+
const handlersRef = useRef(/** @type {any} */ ({}))
|
|
234
234
|
|
|
235
235
|
useEffect(() => {
|
|
236
236
|
handlersRef.current = {
|
|
@@ -367,22 +367,24 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
367
367
|
}
|
|
368
368
|
}, [name, cache, socket, logger])
|
|
369
369
|
|
|
370
|
-
useOnWindowFocus(
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
globalSocketState.versionsRequestedAt
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
370
|
+
useOnWindowFocus(
|
|
371
|
+
useCallback(() => {
|
|
372
|
+
if (cache !== true) {
|
|
373
|
+
return
|
|
374
|
+
}
|
|
375
|
+
if (socket === undefined) {
|
|
376
|
+
return
|
|
377
|
+
}
|
|
378
|
+
logger.verbose(
|
|
379
|
+
`[miolo-react][ssr][${name}] Window focused, checking versions (socket id is ${socket.id})`
|
|
380
|
+
)
|
|
381
|
+
const now = Date.now()
|
|
382
|
+
if (now - globalSocketState.versionsRequestedAt > 500) {
|
|
383
|
+
globalSocketState.versionsRequestedAt = now
|
|
384
|
+
socket.emit("ssr-versions")
|
|
385
|
+
}
|
|
386
|
+
}, [name, cache, logger, socket])
|
|
387
|
+
)
|
|
386
388
|
|
|
387
389
|
return {
|
|
388
390
|
data: ssrData,
|