miolo-react 3.0.0-beta.218 → 3.0.0-beta.219

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": "miolo-react",
3
- "version": "3.0.0-beta.218",
3
+ "version": "3.0.0-beta.219",
4
4
  "description": "React utils for miolo",
5
5
  "author": "Donato Lorenzo <donato@afialapis.com>",
6
6
  "contributors": [
@@ -30,7 +30,7 @@
30
30
  "prepublishOnly": "npm run lint"
31
31
  },
32
32
  "dependencies": {
33
- "idb-keyval": "^6.2.5",
33
+ "idb-keyval": "^6.2.6",
34
34
  "miolo-cli": "../miolo-cli"
35
35
  },
36
36
  "peerDependencies": {
@@ -11,6 +11,31 @@ const _makeSerializable = (obj) => {
11
11
  }
12
12
  }
13
13
 
14
+ let _historyPatched = false
15
+ const patchHistory = () => {
16
+ if (typeof window === "undefined" || _historyPatched) return
17
+
18
+ const originalPushState = window.history.pushState
19
+ window.history.pushState = function (...args) {
20
+ const ret = originalPushState.apply(this, args)
21
+ window.dispatchEvent(new Event("miolo-navigation"))
22
+ return ret
23
+ }
24
+
25
+ const originalReplaceState = window.history.replaceState
26
+ window.history.replaceState = function (...args) {
27
+ const ret = originalReplaceState.apply(this, args)
28
+ window.dispatchEvent(new Event("miolo-navigation"))
29
+ return ret
30
+ }
31
+
32
+ window.addEventListener("popstate", () => {
33
+ window.dispatchEvent(new Event("miolo-navigation"))
34
+ })
35
+
36
+ _historyPatched = true
37
+ }
38
+
14
39
  const globalSocketState = {
15
40
  rooms: new Map(),
16
41
  unsubscribeTimers: new Map(),
@@ -58,6 +83,7 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
58
83
  )
59
84
  const [status, setStatus] = useState(ssrDataFromContext !== undefined ? "loaded" : "idle")
60
85
  const [error, setError] = useState(undefined)
86
+ const pendingLazyRefreshRef = useRef(false)
61
87
 
62
88
  const cacheInvalidate = useCallback(async () => {
63
89
  if (typeof window !== "undefined") {
@@ -102,6 +128,7 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
102
128
 
103
129
  const updateSsrData = useCallback(
104
130
  (data) => {
131
+ pendingLazyRefreshRef.current = false
105
132
  setStatus("loading")
106
133
  setSsrData(parseData(data))
107
134
  setError(undefined)
@@ -116,6 +143,7 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
116
143
  return
117
144
  }
118
145
 
146
+ pendingLazyRefreshRef.current = false
119
147
  setStatus("loading")
120
148
  setError(undefined)
121
149
 
@@ -146,7 +174,7 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
146
174
  setStatus("loaded")
147
175
  }, [status, context, fetcher, loader, url, params, parseData, name, cacheSet, logger])
148
176
 
149
- const checkCacheVersions = useCallback(
177
+ const cacheVersionsCheck = useCallback(
150
178
  async (versions) => {
151
179
  logger.verbose(
152
180
  `[miolo-react][ssr][${name}] Backend versions received: ${JSON.stringify(versions)}`
@@ -234,12 +262,35 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
234
262
 
235
263
  useEffect(() => {
236
264
  handlersRef.current = {
237
- checkCacheVersions,
265
+ cacheVersionsCheck,
238
266
  cacheInvalidate,
239
- refreshSsrData
267
+ refreshSsrData,
268
+ refreshSsrDataLazy: () => {
269
+ pendingLazyRefreshRef.current = true
270
+ }
240
271
  }
241
272
  })
242
273
 
274
+ useEffect(() => {
275
+ if (cache === true) {
276
+ patchHistory()
277
+ }
278
+
279
+ const onNav = () => {
280
+ if (pendingLazyRefreshRef.current) {
281
+ logger.verbose(
282
+ `[miolo-react][ssr][${name}] navigation detected, doing pending lazy refresh`
283
+ )
284
+ pendingLazyRefreshRef.current = false
285
+ refreshSsrData()
286
+ }
287
+ }
288
+ window.addEventListener("miolo-navigation", onNav)
289
+ return () => {
290
+ window.removeEventListener("miolo-navigation", onNav)
291
+ }
292
+ }, [cache, name, logger, refreshSsrData])
293
+
243
294
  useEffect(() => {
244
295
  if (!socket || cache !== true) {
245
296
  return
@@ -271,20 +322,6 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
271
322
  }
272
323
  }
273
324
 
274
- const onInvalidate = async (data) => {
275
- if (data.name !== name) return
276
-
277
- logger.verbose(`[miolo-react][ssr][${name}] ssr-invalidate ${JSON.stringify(data)}`)
278
- if (data.exclude_socket_id && data.exclude_socket_id === socket.id) {
279
- logger.verbose(
280
- `[miolo-react][ssr][${name}] ssr-invalidate ${data.name} ignored for socketid ${socket.id}`
281
- )
282
- return
283
- }
284
- logger.info(`[miolo-react][ssr][${name}] ssr-invalidate ${data.name}`)
285
- await handlersRef.current.cacheInvalidate()
286
- }
287
-
288
325
  const onRefresh = async (data) => {
289
326
  if (data.name !== name) return
290
327
 
@@ -297,14 +334,30 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
297
334
  )
298
335
  return
299
336
  }
300
- logger.info(`[miolo-react][ssr][${name}] ssr-refresh ${data.name}`)
337
+ if (data.only_socket_id && data.only_socket_id !== socket.id) {
338
+ logger.verbose(
339
+ `[miolo-react][ssr][${name}] ssr-refresh ${data.name} ignored for socketid ${socket.id} (only_socket_id mismatch)`
340
+ )
341
+ return
342
+ }
343
+
344
+ logger.info(
345
+ `[miolo-react][ssr][${name}] ssr-refresh ${data.name}${data.lazy ? " (lazy)" : ""}`
346
+ )
301
347
  await handlersRef.current.cacheInvalidate()
302
- handlersRef.current.refreshSsrData()
348
+
349
+ if (!data.lazy) {
350
+ handlersRef.current.refreshSsrData()
351
+ } else {
352
+ if (handlersRef.current.refreshSsrDataLazy) {
353
+ handlersRef.current.refreshSsrDataLazy()
354
+ }
355
+ }
303
356
  }
304
357
 
305
358
  const onVersions = (versions) => {
306
- if (handlersRef.current.checkCacheVersions) {
307
- handlersRef.current.checkCacheVersions(versions)
359
+ if (handlersRef.current.cacheVersionsCheck) {
360
+ handlersRef.current.cacheVersionsCheck(versions)
308
361
  }
309
362
  }
310
363
 
@@ -316,7 +369,6 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
316
369
 
317
370
  socket.on("connect", requestSubscribeAndVersions)
318
371
  socket.on("ssr-versions", onVersions)
319
- socket.on("ssr-invalidate", onInvalidate)
320
372
  socket.on("ssr-refresh", onRefresh)
321
373
 
322
374
  return () => {
@@ -339,7 +391,6 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
339
391
 
340
392
  socket.off("connect", requestSubscribeAndVersions)
341
393
  socket.off("ssr-versions", onVersions)
342
- socket.off("ssr-invalidate", onInvalidate)
343
394
  socket.off("ssr-refresh", onRefresh)
344
395
  }
345
396
  }, [name, cache, socket, logger])