Live run moves

This commit is contained in:
2026-02-08 16:04:51 +01:00
parent 0d9ea07b4e
commit 8f7fc0188c
5 changed files with 18 additions and 7 deletions
+2
View File
@@ -27,6 +27,8 @@ export async function POST(req: NextRequest) {
{ status: 401 },
);
console.log(moves);
const run = await Core.addLiveRunMoves(liveRun.id, moves);
return NextResponse.json({ ok: true, run });
+6 -2
View File
@@ -43,11 +43,15 @@ function getLanguageExtension(lang: Language) {
export default function Editor({ run }: { run: LiveRun | null | undefined }) {
const [editorView, setEditorView] = useState<EditorView>();
const [moves] = useActionOnce<LiveRunMove[]>(
async () => (run ? (await getLiveRunMoves(run.id)) || [] : []),
const [moves] = useAction<LiveRunMove[]>(
async () => (run ? await getLiveRunMoves(run.id) : []),
[run],
);
useEffect(() => {
console.log(moves);
}, [moves]);
useEffect(() => {
if (!editorView || !moves) return;
+4
View File
@@ -0,0 +1,4 @@
import "server-only";
import Redis from "ioredis";
export const redis = new Redis(process.env.REDIS_URL!);
+1 -1
View File
@@ -7,7 +7,7 @@ export async function getLiveRuns(): Promise<LiveRun[]> {
return Core.getLiveRuns();
}
export async function getLiveRun(id: string): Promise<LiveRun | undefined> {
export async function getLiveRun(id: string): Promise<LiveRun | null> {
return Core.getLiveRun(id);
}
+5 -4
View File
@@ -3,9 +3,7 @@
import "server-only";
import { LiveRun, LiveRunMove } from "./types";
import { SupabaseClient } from "@supabase/supabase-js";
import Redis from "ioredis";
const redis = new Redis(process.env.REDIS_URL!);
import { redis } from "@/lib/redis";
/** -------------------- Live Runs in Redis -------------------- */
@@ -57,6 +55,9 @@ export async function addLiveRunMoves(id: string, moves: LiveRunMove[]) {
const pipeline = redis.pipeline();
moves.forEach((move) => {
pipeline.rpush(`liveRunMoves:${id}`, JSON.stringify(move));
setTimeout(() => {
redis.del(`liveRunMoves:${id}`);
}, 3000);
});
// Optional: keep moves ephemeral automatically after 3s
@@ -81,7 +82,7 @@ export async function getLiveRunMoves(id: string): Promise<LiveRunMove[]> {
export async function submitRun(
supabase: SupabaseClient,
run: LiveRun,
user_id: string
user_id: string,
) {
const now = new Date();
const durationMs = now.getTime() - run.start;