From 76073043bf7065c2812f8418da743e7cb455ea4e Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sat, 21 Oct 2023 05:11:11 +0200 Subject: [PATCH] Whoops, condition wrong way around --- src/components/player/atoms/ProgressBar.tsx | 6 ++-- .../player/internals/ThumbnailScraper.tsx | 32 ++++++++++++++++--- src/stores/player/slices/display.ts | 3 ++ src/stores/player/slices/thumbnails.ts | 7 ++-- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/components/player/atoms/ProgressBar.tsx b/src/components/player/atoms/ProgressBar.tsx index 34e5840f..8b23f4fb 100644 --- a/src/components/player/atoms/ProgressBar.tsx +++ b/src/components/player/atoms/ProgressBar.tsx @@ -19,7 +19,7 @@ function ThumbnailDisplay(props: { at: number }) { }, [thumbnailImages, props.at]); if (!currentThumbnail) return null; - return ; + return ; } function useMouseHoverPosition(barRef: RefObject) { @@ -72,8 +72,6 @@ export function ProgressBar() { setDraggingTime((dragPercentage / 100) * duration); }, [setDraggingTime, duration, dragPercentage]); - const mousePosition = Math.floor(dragPercentage * duration); - return (
@@ -84,7 +82,7 @@ export function ProgressBar() { left: `${mousePos}%`, }} > - +
) : null}
diff --git a/src/components/player/internals/ThumbnailScraper.tsx b/src/components/player/internals/ThumbnailScraper.tsx index 34d457a1..e4f9ac08 100644 --- a/src/components/player/internals/ThumbnailScraper.tsx +++ b/src/components/player/internals/ThumbnailScraper.tsx @@ -74,6 +74,7 @@ class ThumnbnailWorker { this.canvasEl.height ); const imgUrl = this.canvasEl.toDataURL(); + if (this.interrupted) return; this.cb({ at, data: imgUrl, @@ -84,13 +85,20 @@ class ThumnbnailWorker { const vid = this.videoEl; if (!vid) return; await this.initVideo(); - if (this.interrupted) return; - await this.takeSnapshot(vid.duration / 2); + + // TODO make a queue based on refinement algorithm + + const queue = [0.5, 0.25, 0.75]; + for (let i = 0; i < queue.length; i += 1) { + if (this.interrupted) return; + await this.takeSnapshot(vid.duration * queue[i]); + } } } export function ThumbnailScraper() { const addImage = usePlayerStore((s) => s.thumbnails.addImage); + const meta = usePlayerStore((s) => s.meta); const source = usePlayerStore((s) => s.source); const workerRef = useRef(null); @@ -102,8 +110,6 @@ export function ThumbnailScraper() { }); }, [source]); - // TODO stop worker on meta change - // start worker with the stream useEffect(() => { // dont interrupt existing working @@ -119,9 +125,25 @@ export function ThumbnailScraper() { // destroy worker on unmount useEffect(() => { return () => { - if (workerRef.current) workerRef.current.destroy(); + if (workerRef.current) { + workerRef.current.destroy(); + workerRef.current = null; + } }; }, []); + // if targeted meta changes, abort the scraper + const serializedMeta = JSON.stringify({ + id: meta?.tmdbId, + ep: meta?.episode?.tmdbId, + se: meta?.season?.tmdbId, + }); + useEffect(() => { + if (workerRef.current) { + workerRef.current.destroy(); + workerRef.current = null; + } + }, [serializedMeta]); + return null; } diff --git a/src/stores/player/slices/display.ts b/src/stores/player/slices/display.ts index e777f607..06885589 100644 --- a/src/stores/player/slices/display.ts +++ b/src/stores/player/slices/display.ts @@ -105,6 +105,9 @@ export const createDisplaySlice: MakeSlice = (set, get) => ({ set((s) => { s.status = playerStatus.IDLE; s.meta = null; + s.thumbnails.images = []; + s.progress.time = 0; + s.progress.duration = 0; }); }, }); diff --git a/src/stores/player/slices/thumbnails.ts b/src/stores/player/slices/thumbnails.ts index 588d4351..b0394d34 100644 --- a/src/stores/player/slices/thumbnails.ts +++ b/src/stores/player/slices/thumbnails.ts @@ -28,7 +28,7 @@ export function nearestImageAt( // no images, early return if (images.length === 0) return null; - const indexPastTimestamp = images.findIndex((v) => v.at < at); + const indexPastTimestamp = images.findIndex((v) => v.at > at); // no image found past timestamp, so last image must be closest if (indexPastTimestamp === -1) @@ -76,13 +76,14 @@ export const createThumbnailSlice: MakeSlice = (set, get) => ({ addImage(img) { const store = get(); const exactOrPastImageIndex = store.thumbnails.images.findIndex( - (v) => v.at <= img.at + (v) => v.at >= img.at ); // not found past or exact, so just append to the end if (exactOrPastImageIndex === -1) { set((s) => { s.thumbnails.images.push(img); + s.thumbnails.images = [...s.thumbnails.images]; }); return; } @@ -93,6 +94,7 @@ export const createThumbnailSlice: MakeSlice = (set, get) => ({ if (exactOrPastImage.at === img.at) { set((s) => { s.thumbnails.images[exactOrPastImageIndex] = img; + s.thumbnails.images = [...s.thumbnails.images]; }); return; } @@ -100,6 +102,7 @@ export const createThumbnailSlice: MakeSlice = (set, get) => ({ // found one past, insert right before it set((s) => { s.thumbnails.images.splice(exactOrPastImageIndex, 0, img); + s.thumbnails.images = [...s.thumbnails.images]; }); }, },