optimize watch progress persistence with debounced client saves and tiny-delta server skip

This commit is contained in:
Yusuf İpek
2026-02-24 17:51:40 +03:00
parent 71233f8fe8
commit 9f32aeafe6
2 changed files with 182 additions and 69 deletions
+40 -14
View File
@@ -122,29 +122,55 @@ export async function POST(request: NextRequest, { params }: RouteParams) {
// Calculate percentage
const safeDuration = duration || 0;
const percentage = safeDuration > 0 ? Math.min(100, (progress / safeDuration) * 100) : 0;
const tinyProgressDelta = 0.5;
const tinyDurationDelta = 1;
// Upsert watch progress
const watchProgress = await db.watchProgress.upsert({
const existingWatchProgress = await db.watchProgress.findUnique({
where: {
userId_versionId: {
userId: session.user.id,
versionId: targetVersion.id,
},
},
update: {
progress,
duration: safeDuration,
percentage,
},
create: {
userId: session.user.id,
versionId: targetVersion.id,
progress,
duration: safeDuration,
percentage,
},
});
if (existingWatchProgress) {
const progressDiff = Math.abs(existingWatchProgress.progress - progress);
const durationDiff = Math.abs(existingWatchProgress.duration - safeDuration);
if (progressDiff < tinyProgressDelta && durationDiff < tinyDurationDelta) {
return successResponse({
success: true,
progress: existingWatchProgress.progress,
percentage: existingWatchProgress.percentage,
skipped: true,
});
}
}
const watchProgress = existingWatchProgress
? await db.watchProgress.update({
where: {
userId_versionId: {
userId: session.user.id,
versionId: targetVersion.id,
},
},
data: {
progress,
duration: safeDuration,
percentage,
},
})
: await db.watchProgress.create({
data: {
userId: session.user.id,
versionId: targetVersion.id,
progress,
duration: safeDuration,
percentage,
},
});
return successResponse({
success: true,
progress: watchProgress.progress,