84 lines · 2.8 KBRaw
| 1 | import { useEffect, useRef } from "react"; |
| 2 | import { useRevalidator } from "react-router"; |
| 3 | |
| 4 | const BACKOFF_MS = [1000, 2000, 5000, 10000, 30000]; |
| 5 | const POLL_AFTER_FAILURES = 3; |
| 6 | const POLL_MS = 30000; |
| 7 | const PING_MS = 30000; |
| 8 | |
| 9 | /** |
| 10 | * Reloads the page's data whenever a new event lands on the repository or task. The repository's hub says so over |
| 11 | * a socket; while the socket keeps failing, the page also polls slowly. A hidden tab reloads when it is shown again. |
| 12 | */ |
| 13 | export function useLive(repo: string, task?: number) { |
| 14 | const { revalidate } = useRevalidator(); |
| 15 | const reload = useRef(revalidate); |
| 16 | reload.current = revalidate; |
| 17 | useEffect(() => { |
| 18 | const path = `/${repo.split("/").map(encodeURIComponent).join("/")}/live${task ? `?task=${task}` : ""}`; |
| 19 | let socket: WebSocket | null = null; |
| 20 | let opens = 0; |
| 21 | let failures = 0; |
| 22 | let stale = false; |
| 23 | let polled: number | null = null; |
| 24 | let retry: ReturnType<typeof setTimeout> | undefined; |
| 25 | let poller: ReturnType<typeof setInterval> | undefined; |
| 26 | |
| 27 | const refresh = () => { |
| 28 | if (document.hidden) stale = true; |
| 29 | else reload.current(); |
| 30 | }; |
| 31 | const shown = () => { |
| 32 | if (!document.hidden && stale) { |
| 33 | stale = false; |
| 34 | reload.current(); |
| 35 | } |
| 36 | }; |
| 37 | const poll = async () => { |
| 38 | const response = await fetch(path).catch(() => null); |
| 39 | if (!response?.ok) return; |
| 40 | const { latest } = (await response.json()) as { latest: number }; |
| 41 | if (polled !== null && latest !== polled) refresh(); |
| 42 | polled = latest; |
| 43 | }; |
| 44 | const connect = () => { |
| 45 | const opened = new WebSocket(`${location.protocol === "https:" ? "wss" : "ws"}://${location.host}${path}`); |
| 46 | socket = opened; |
| 47 | opened.onopen = () => { |
| 48 | if (opens++) refresh(); |
| 49 | failures = 0; |
| 50 | clearInterval(poller); |
| 51 | poller = undefined; |
| 52 | }; |
| 53 | opened.onmessage = (event) => { |
| 54 | if (event.data !== "pong") refresh(); |
| 55 | }; |
| 56 | opened.onclose = () => { |
| 57 | if (socket !== opened) return; |
| 58 | failures++; |
| 59 | if (failures >= POLL_AFTER_FAILURES && poller === undefined) { |
| 60 | polled = null; |
| 61 | poll(); |
| 62 | poller = setInterval(poll, POLL_MS); |
| 63 | } |
| 64 | const wait = BACKOFF_MS[Math.min(failures, BACKOFF_MS.length) - 1]; |
| 65 | retry = setTimeout(connect, wait * (0.5 + Math.random())); |
| 66 | }; |
| 67 | }; |
| 68 | const pinger = setInterval(() => { |
| 69 | if (socket?.readyState === WebSocket.OPEN) socket.send("ping"); |
| 70 | }, PING_MS); |
| 71 | |
| 72 | connect(); |
| 73 | document.addEventListener("visibilitychange", shown); |
| 74 | return () => { |
| 75 | const open = socket; |
| 76 | socket = null; |
| 77 | open?.close(); |
| 78 | clearTimeout(retry); |
| 79 | clearInterval(poller); |
| 80 | clearInterval(pinger); |
| 81 | document.removeEventListener("visibilitychange", shown); |
| 82 | }; |
| 83 | }, [repo, task]); |
| 84 | } |