refactor(realtime): centralize refresh policy
This commit is contained in:
@@ -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);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user