From 9db9671a9d5a1d63a32c24afe87a9d5d94ef1b66 Mon Sep 17 00:00:00 2001
From: memdmp <memdmp@estrogen.zone>
Date: Thu, 31 Jul 2025 23:52:46 +0200
Subject: feat: further enhancements and an api breaking change

---
 src/lib/Player/Player.svelte | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

(limited to 'src/lib/Player/Player.svelte')

diff --git a/src/lib/Player/Player.svelte b/src/lib/Player/Player.svelte
index f3b8fb8..a742857 100644
--- a/src/lib/Player/Player.svelte
+++ b/src/lib/Player/Player.svelte
@@ -39,7 +39,9 @@
 			}
 		})();
 		video['_isInit'] = true;
-		renderPromise = video.init();
+		renderPromise = video.init({
+			isPreview: true
+		});
 		await renderPromise;
 		video['_isInit'] = false;
 		frameCount = video.length;
@@ -49,6 +51,7 @@
 		const ourId = ++renderId;
 		if (renderPromise) await renderPromise;
 		if (renderId !== ourId) return;
+		if (frame > video.length) frame = video.length;
 		renderPromise =
 			video.renderFrame({
 				frames: frame,
@@ -120,12 +123,19 @@
 		}
 		loadedFrameTimestamp = true;
 	});
-	// TODO: implement waitin a few seconds before saving
-	$effect(() => {
-		if (loadedFrameTimestamp)
+	let nextSaveFrameNumber = 0;
+	let lastSave = 0;
+	const saveFramePosition = async (frame: number) => {
+		nextSaveFrameNumber = frame;
+		await new Promise((rs) => setTimeout(rs, 100));
+		if (frame === nextSaveFrameNumber || performance.now() - lastSave > 500)
 			try {
+				lastSave = performance.now();
 				sessionStorage.setItem('timestamp', frame.toString(36));
 			} catch (_) {}
+	};
+	$effect(() => {
+		if (loadedFrameTimestamp) saveFramePosition(frame);
 	});
 	$effect(() => {
 		if (audio && video && !playing && !playbackFrameOffset) {
@@ -160,7 +170,9 @@
 		if (canvas && video) {
 			(async () => {
 				video['_isInit'] = true;
-				renderPromise = await video.init();
+				renderPromise = await video.init({
+					isPreview: true
+				});
 				video['_isInit'] = false;
 				renderPreviewFrame(video, frame);
 			})();
-- 
cgit v1.2.3

