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