fix: 修复竞态条件、重置逻辑、无匹配终态等关键问题
- 用 Prisma $transaction 实现 atomicUpdateRoom,防止并发写入覆盖 - 新增 POST /api/room/[id]/reset 端点,修复"再来一轮"按钮死循环 - 新增 swipeCounts 字段追踪滑动进度,检测"无人匹配"终态 - 着陆页 handleCreate 增加 res.ok 检查,防止跳转到无效房间 - 匹配或无匹配后停止轮询,减少无效请求
This commit is contained in:
@@ -1,22 +1,36 @@
|
||||
"use client";
|
||||
|
||||
import useSWR from "swr";
|
||||
import { useRef } from "react";
|
||||
import { RoomStatus } from "@/types";
|
||||
|
||||
const fetcher = (url: string) => fetch(url).then((r) => r.json());
|
||||
|
||||
export function useRoomPolling(roomId: string) {
|
||||
const { data, error, isLoading } = useSWR<RoomStatus>(
|
||||
const settled = useRef(false);
|
||||
|
||||
const { data, error, isLoading, mutate } = useSWR<RoomStatus>(
|
||||
`/api/room/${roomId}`,
|
||||
fetcher,
|
||||
{ refreshInterval: 1500, revalidateOnFocus: true },
|
||||
{
|
||||
refreshInterval: settled.current ? 0 : 1500,
|
||||
revalidateOnFocus: true,
|
||||
},
|
||||
);
|
||||
|
||||
if (data?.match != null || data?.noMatch) {
|
||||
settled.current = true;
|
||||
} else {
|
||||
settled.current = false;
|
||||
}
|
||||
|
||||
return {
|
||||
userCount: data?.userCount ?? 0,
|
||||
match: data?.match ?? null,
|
||||
noMatch: data?.noMatch ?? false,
|
||||
restaurants: data?.restaurants ?? [],
|
||||
isLoading,
|
||||
error,
|
||||
mutate,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user