|
1 | 1 | import { useEffect } from "react" |
2 | | -import { useNavigation } from "react-router" |
3 | 2 | import type { ActionEvent, LoaderEvent } from "../../server/event-queue.js" |
| 3 | +import { eventClient } from "../../shared/event-client.js" |
4 | 4 | import type { ServerInfo } from "../context/rdtReducer.js" |
5 | 5 | import { useServerInfo } from "../context/useRDTContext.js" |
6 | 6 | import { cutArrayToLastN } from "../utils/common.js" |
@@ -49,46 +49,27 @@ const updateRouteInfo = ( |
49 | 49 | } |
50 | 50 |
|
51 | 51 | const useDevServerConnection = () => { |
52 | | - const navigation = useNavigation() |
53 | 52 | const { server, setServerInfo } = useServerInfo() |
54 | 53 |
|
55 | | - // Pull the event queue from the server when the page is idle |
| 54 | + // Listen to loader and action events from the event client |
56 | 55 | useEffect(() => { |
57 | | - if (typeof import.meta.hot === "undefined") return |
58 | | - if (navigation.state !== "idle") return |
59 | | - // We send a pull & clear event to pull the event queue and clear it |
60 | | - import.meta.hot.send("all-route-info") |
61 | | - }, [navigation.state]) |
62 | | - |
63 | | - useEffect(() => { |
64 | | - // biome-ignore lint/suspicious/noExplicitAny: we don't care about the type |
65 | | - const cb2 = (data: any) => { |
66 | | - const events = JSON.parse(data).data |
67 | | - const routes: ServerInfo["routes"] = {} |
68 | | - for (const routeInfo of Object.values(events)) { |
69 | | - // biome-ignore lint/suspicious/noExplicitAny: we don't care about the type |
70 | | - const { loader, action } = routeInfo as any |
71 | | - const events = [ |
72 | | - // biome-ignore lint/suspicious/noExplicitAny: we don't care about the type |
73 | | - loader.map((e: any) => ({ type: "loader", data: e })), |
74 | | - // biome-ignore lint/suspicious/noExplicitAny: we don't care about the type |
75 | | - action.map((e: any) => ({ type: "action", data: e })), |
76 | | - ].flat() |
77 | | - for (const event of events) { |
78 | | - updateRouteInfo(server, routes, event, false) |
79 | | - } |
80 | | - } |
| 56 | + // Listen to loader events |
| 57 | + const unsubscribeLoader = eventClient.on("loader-event", (event) => { |
| 58 | + const routes: ServerInfo["routes"] = { ...server?.routes } |
| 59 | + updateRouteInfo(server, routes, { type: "loader", data: event.payload as LoaderEvent["data"] }, false) |
| 60 | + setServerInfo({ routes }) |
| 61 | + }) |
81 | 62 |
|
| 63 | + // Listen to action events |
| 64 | + const unsubscribeAction = eventClient.on("action-event", (event) => { |
| 65 | + const routes: ServerInfo["routes"] = { ...server?.routes } |
| 66 | + updateRouteInfo(server, routes, { type: "action", data: event.payload as ActionEvent["data"] }, false) |
82 | 67 | setServerInfo({ routes }) |
83 | | - } |
84 | | - if (typeof import.meta.hot !== "undefined") { |
85 | | - import.meta.hot.on("all-route-info", cb2) |
86 | | - } |
| 68 | + }) |
87 | 69 |
|
88 | 70 | return () => { |
89 | | - if (typeof import.meta.hot !== "undefined") { |
90 | | - import.meta.hot.dispose(cb2) |
91 | | - } |
| 71 | + unsubscribeLoader() |
| 72 | + unsubscribeAction() |
92 | 73 | } |
93 | 74 | }, [server, setServerInfo]) |
94 | 75 |
|
|
0 commit comments