From 10c516215fe6280f8f7e55a7290fa461a3f4ae70 Mon Sep 17 00:00:00 2001 From: HxxxxxS <16c52527@opayq.com> Date: Mon, 6 May 2024 17:00:49 +0200 Subject: [PATCH] add cachedVideos and only hit api when cache is empty --- script.js | 72 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/script.js b/script.js index 67b8c4f..25f533d 100644 --- a/script.js +++ b/script.js @@ -10,13 +10,23 @@ const videos = [] let videosLoaded = 0 let aspectRatio = 1 +let cachedVideos = [] + +let showText = true + +function debugLog(...args) { + if (debugMode) { + console.log(...args) + } +} + function loadVideos(num) { - console.log(`Loading ${num} videos`) + debugLog(`Loading ${num} videos`) while (num > 0) { let video = createVideoElement() video.addEventListener("loadedmetadata", handleVideoLoaded) - video.addEventListener("loaded", handleVideoEnded) video.addEventListener("ended", handleVideoEnded) + video.addEventListener("error", handleVideoEnded) video.src = selectRandomVideo(video) videos.push(video) num-- @@ -51,10 +61,10 @@ function handleVideoLoaded() { function handleVideoEnded(event) { const video = event.target + //debugLog(video,event) selectRandomVideo(video) } - function drawVideos() { ctx.clearRect(0, 0, canvas.width, canvas.height) @@ -86,8 +96,8 @@ function drawVideos() { ctx.globalCompositeOperation = "source-over" }) - // If debugMode is true, draw debug text - if (debugMode) { + // If showText is true, draw debug text + if (showText) { drawDebugText() } @@ -138,7 +148,7 @@ function formatTime(seconds) { const hours = Math.floor(seconds / 3600) const minutes = Math.floor((seconds % 3600) / 60) const remainingSeconds = Math.floor(seconds % 60) - return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}` + return `${(hours?hours.toString().padStart(2, '0')+':':'')}${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}` } function updateCanvasSize() { @@ -153,30 +163,44 @@ function updateCanvasSize() { window.addEventListener("resize", updateCanvasSize) +function getSourceFiles() { + debugLog("Getting source files") + return fetch("/api/videos") + .then(response => response.json()) + .then(videoFiles => { + debugLog("Success") + cachedVideos = videoFiles; + }) + .catch(error => console.error("Error fetching videos:", error)); +} function selectRandomVideo(videoElement) { - fetch("/api/videos") - .then(response => response.json()) - .then(videoFiles => { - const currentSrc = videoElement.src - const filteredVideos = videoFiles.filter(video => video.src !== currentSrc) - if (filteredVideos.length > 0) { - const randomIndex = Math.floor(Math.random() * filteredVideos.length) - const randomVideo = filteredVideos[randomIndex] - videoElement.src = randomVideo.src - } else { - console.log("No other videos available.") - } + if (cachedVideos.length) { + const currentSrc = videoElement.src; + const filteredVideos = cachedVideos.filter(video => video.src !== currentSrc); + if (filteredVideos.length > 0) { + const randomIndex = Math.floor(Math.random() * filteredVideos.length); + const randomVideo = filteredVideos[randomIndex]; + videoElement.src = randomVideo.src; + } else { + debugLog("No other videos available."); + getSourceFiles().then(()=>{ + selectRandomVideo(videoElement) }) - .catch(error => console.error("Error fetching videos:", error)) + } + } else { + debugLog("Cache empty, doing new request") + getSourceFiles().then(()=>{ + selectRandomVideo(videoElement) + }) + } } document.addEventListener('keydown', function(event) { console.log('keyDown',event) // Check if the pressed key is the one you want to bind if (event.key === 'Enter') { - debugMode = !debugMode - console.log("debugMode:",debugMode) + showText = !showText } if (event.key === 'ArrowDown') { loadVideos(-1) @@ -186,5 +210,7 @@ document.addEventListener('keydown', function(event) { } }) -loadVideos(videosToLoad) -updateCanvasSize() \ No newline at end of file +getSourceFiles().then(()=>{ + loadVideos(videosToLoad) + updateCanvasSize() +}) \ No newline at end of file