miolo-react 3.0.0-beta.215 → 3.0.0-beta.218
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,11 +1,11 @@
|
|
|
1
1
|
import { miolo_client } from "miolo-cli"
|
|
2
|
-
import { useCallback, useEffect
|
|
2
|
+
import { useCallback, /*useEffect,*/ useState } from "react"
|
|
3
3
|
import { useSsrDataOrReload } from "../ssr/useSsrDataOrReload.mjs"
|
|
4
4
|
import Context from "./MioloContext.mjs"
|
|
5
5
|
|
|
6
6
|
const MioloContextProvider = ({ context, children }) => {
|
|
7
7
|
const [innerContext, setInnerContext] = useState(context)
|
|
8
|
-
const [mioloObj,
|
|
8
|
+
const [mioloObj, _setMioloObj] = useState(miolo_client(context))
|
|
9
9
|
|
|
10
10
|
/*useEffect(() => {
|
|
11
11
|
setInnerContext(context)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useState } from "react"
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
|
|
2
2
|
import getSsrDataFromContext from "./getSsrDataFromContext.mjs"
|
|
3
3
|
import useOnWindowFocus from "./useOnWindowFocus.mjs"
|
|
4
4
|
import usePropsCheck from "./usePropsCheck.mjs"
|
|
@@ -11,6 +11,13 @@ const _makeSerializable = (obj) => {
|
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
const globalSocketState = {
|
|
15
|
+
rooms: new Map(),
|
|
16
|
+
unsubscribeTimers: new Map(),
|
|
17
|
+
lastSubscribeEmitted: new Map(),
|
|
18
|
+
versionsRequestedAt: 0
|
|
19
|
+
}
|
|
20
|
+
|
|
14
21
|
const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
15
22
|
const { fetcher, socket, logger } = miolo
|
|
16
23
|
const {
|
|
@@ -223,16 +230,45 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
223
230
|
}
|
|
224
231
|
}, [ssrDataFromContext, cacheSet])
|
|
225
232
|
|
|
233
|
+
const handlersRef = useRef({})
|
|
234
|
+
|
|
235
|
+
useEffect(() => {
|
|
236
|
+
handlersRef.current = {
|
|
237
|
+
checkCacheVersions,
|
|
238
|
+
cacheInvalidate,
|
|
239
|
+
refreshSsrData
|
|
240
|
+
}
|
|
241
|
+
})
|
|
242
|
+
|
|
226
243
|
useEffect(() => {
|
|
227
244
|
if (!socket || cache !== true) {
|
|
228
245
|
return
|
|
229
246
|
}
|
|
230
247
|
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
248
|
+
const currentCount = globalSocketState.rooms.get(name) || 0
|
|
249
|
+
globalSocketState.rooms.set(name, currentCount + 1)
|
|
250
|
+
|
|
251
|
+
const pendingTimer = globalSocketState.unsubscribeTimers.get(name)
|
|
252
|
+
if (pendingTimer) {
|
|
253
|
+
clearTimeout(pendingTimer)
|
|
254
|
+
globalSocketState.unsubscribeTimers.delete(name)
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const requestSubscribeAndVersions = () => {
|
|
258
|
+
const now = Date.now()
|
|
259
|
+
const lastSub = globalSocketState.lastSubscribeEmitted.get(name) || 0
|
|
260
|
+
|
|
261
|
+
if (now - lastSub > 100) {
|
|
262
|
+
globalSocketState.lastSubscribeEmitted.set(name, now)
|
|
263
|
+
logger.verbose(`[miolo-react][ssr][${name}] Socket connected, subscribing`)
|
|
264
|
+
socket.emit("ssr-subscribe", name)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (now - globalSocketState.versionsRequestedAt > 500) {
|
|
268
|
+
globalSocketState.versionsRequestedAt = now
|
|
269
|
+
logger.verbose(`[miolo-react][ssr][${name}] Asking for ssr-versions`)
|
|
270
|
+
socket.emit("ssr-versions")
|
|
271
|
+
}
|
|
236
272
|
}
|
|
237
273
|
|
|
238
274
|
const onInvalidate = async (data) => {
|
|
@@ -246,7 +282,7 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
246
282
|
return
|
|
247
283
|
}
|
|
248
284
|
logger.info(`[miolo-react][ssr][${name}] ssr-invalidate ${data.name}`)
|
|
249
|
-
await cacheInvalidate()
|
|
285
|
+
await handlersRef.current.cacheInvalidate()
|
|
250
286
|
}
|
|
251
287
|
|
|
252
288
|
const onRefresh = async (data) => {
|
|
@@ -262,30 +298,51 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
262
298
|
return
|
|
263
299
|
}
|
|
264
300
|
logger.info(`[miolo-react][ssr][${name}] ssr-refresh ${data.name}`)
|
|
265
|
-
await cacheInvalidate()
|
|
266
|
-
refreshSsrData()
|
|
301
|
+
await handlersRef.current.cacheInvalidate()
|
|
302
|
+
handlersRef.current.refreshSsrData()
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const onVersions = (versions) => {
|
|
306
|
+
if (handlersRef.current.checkCacheVersions) {
|
|
307
|
+
handlersRef.current.checkCacheVersions(versions)
|
|
308
|
+
}
|
|
267
309
|
}
|
|
268
310
|
|
|
269
311
|
logger.verbose(`[miolo-react][ssr][${name}] Attaching ssr/cache socket handlers`)
|
|
270
312
|
|
|
271
313
|
if (socket.connected) {
|
|
272
|
-
|
|
314
|
+
requestSubscribeAndVersions()
|
|
273
315
|
}
|
|
274
316
|
|
|
275
|
-
socket.on("connect",
|
|
276
|
-
socket.on("ssr-versions",
|
|
317
|
+
socket.on("connect", requestSubscribeAndVersions)
|
|
318
|
+
socket.on("ssr-versions", onVersions)
|
|
277
319
|
socket.on("ssr-invalidate", onInvalidate)
|
|
278
320
|
socket.on("ssr-refresh", onRefresh)
|
|
279
321
|
|
|
280
322
|
return () => {
|
|
281
323
|
logger.verbose(`[miolo-react][ssr][${name}] Detaching ssr/cache socket handlers`)
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
324
|
+
|
|
325
|
+
const count = globalSocketState.rooms.get(name) || 0
|
|
326
|
+
if (count > 0) {
|
|
327
|
+
globalSocketState.rooms.set(name, count - 1)
|
|
328
|
+
if (count - 1 === 0) {
|
|
329
|
+
const timer = setTimeout(() => {
|
|
330
|
+
if ((globalSocketState.rooms.get(name) || 0) === 0) {
|
|
331
|
+
socket.emit("ssr-unsubscribe", name)
|
|
332
|
+
globalSocketState.lastSubscribeEmitted.set(name, 0)
|
|
333
|
+
}
|
|
334
|
+
globalSocketState.unsubscribeTimers.delete(name)
|
|
335
|
+
}, 100)
|
|
336
|
+
globalSocketState.unsubscribeTimers.set(name, timer)
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
socket.off("connect", requestSubscribeAndVersions)
|
|
341
|
+
socket.off("ssr-versions", onVersions)
|
|
285
342
|
socket.off("ssr-invalidate", onInvalidate)
|
|
286
343
|
socket.off("ssr-refresh", onRefresh)
|
|
287
344
|
}
|
|
288
|
-
}, [name, cache, socket,
|
|
345
|
+
}, [name, cache, socket, logger])
|
|
289
346
|
|
|
290
347
|
useOnWindowFocus(() => {
|
|
291
348
|
if (cache !== true) {
|
|
@@ -297,7 +354,11 @@ const useSsrDataOrReload = (context, miolo, name, options) => {
|
|
|
297
354
|
logger.verbose(
|
|
298
355
|
`[miolo-react][ssr][${name}] Window focused, checking versions (socket id is ${socket.id})`
|
|
299
356
|
)
|
|
300
|
-
|
|
357
|
+
const now = Date.now()
|
|
358
|
+
if (now - globalSocketState.versionsRequestedAt > 500) {
|
|
359
|
+
globalSocketState.versionsRequestedAt = now
|
|
360
|
+
socket.emit("ssr-versions")
|
|
361
|
+
}
|
|
301
362
|
}, [name, cache, logger, socket])
|
|
302
363
|
|
|
303
364
|
return {
|