-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuseStream.ts
52 lines (42 loc) · 1.52 KB
/
useStream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { RELAY_WINDOW_KEY } from "@/relay/environment/helpers";
import { QueryResponsePayload } from "@/relay/environment/server";
import { useCallback, useMemo, useRef } from "react";
import { Observer } from "relay-runtime";
export function useStream() {
const responseStream = useRef<QueryResponsePayload[]>([]);
const observer = useMemo(
() =>
({
next: (response) => responseStream.current.push(response),
} satisfies Observer<QueryResponsePayload>),
[]
);
const buildHydrationScript = useCallback(() => {
const responses = responseStream.current;
const scriptContent: string[] = [];
while (responses.length > 0) {
const payload = responses.shift();
if (!payload) {
break;
}
const queryId = payload.queryId;
const serializedResponse = JSON.stringify(payload.response);
// Ensure there's a wrapper for the query responses on the window.
scriptContent.push(
`window["${RELAY_WINDOW_KEY}"] = window["${RELAY_WINDOW_KEY}"] || {};`
);
// Ensure there's an array on the window object for the given queryId.
scriptContent.push(
`window["${RELAY_WINDOW_KEY}"]["${queryId}"] = window["${RELAY_WINDOW_KEY}"]["${queryId}"] || [];`
);
scriptContent.push(
`window["${RELAY_WINDOW_KEY}"]["${queryId}"].push(${serializedResponse});`
);
}
if (scriptContent.length === 0) {
return null;
}
return scriptContent.join("");
}, []);
return { observer, buildHydrationScript };
}