refactor(realtime): centralize refresh policy

This commit is contained in:
2026-08-09 21:13:48 +07:00 Unverified
parent 078216f36d
commit e17a615c07
3 changed files with 81 additions and 79 deletions
+16 -11
View File
@@ -3,10 +3,7 @@
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { sse } from "@/lib/realtime/sse";
import {
startRealtimeRefresh,
type RealtimeRefreshHandlers,
} from "@/lib/realtime/realtime-refresh";
import { startRealtimeRefresh } from "@/lib/realtime/realtime-refresh";
type FormRealtimeRefreshProps = {
topic: "forms" | "submissions";
@@ -67,21 +64,29 @@ export function RealtimeRefresh(props: RealtimeRefreshProps) {
filter,
debounceMs,
refresh: () => router.refresh(),
subscribe(handlers: RealtimeRefreshHandlers) {
const options = { onopen: handlers.onOpen };
subscribe(emit) {
const options = { onopen: () => emit({ kind: "open" }) };
if (topic === "forms") {
return sse.forms.sub("update", handlers.onForm, options);
return sse.forms.sub(
"update",
(event) => emit({ kind: "form", formId: event.formId }),
options,
);
}
if (topic === "submissions") {
return sse.submissions.sub("update", handlers.onForm, options);
return sse.submissions.sub(
"update",
(event) => emit({ kind: "form", formId: event.formId }),
options,
);
}
if (topic === "uploads") {
return sse.uploads.sub("update", handlers.onUpload, options);
return sse.uploads.sub("update", () => emit({ kind: "upload" }), options);
}
return sse.leaderboards.subMany({
xp: handlers.onXp,
vc: handlers.onVc,
xp: () => emit({ kind: "xp" }),
vc: (event) => emit({ kind: "vc", channelId: event.channelId }),
}, options);
},
});