Skip to content

Commit

Permalink
Only do pre-fetch on hover if there are segments near the start of th…
Browse files Browse the repository at this point in the history
…e video
  • Loading branch information
ajayyy committed Jan 18, 2025
1 parent 0f75953 commit 236c95d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/utils/thumbnails.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isOnInvidious, parseYouTubeVideoIDFromURL } from "../../maze-utils/src/video";
import Config from "../config";
import { getVideoLabel } from "./videoLabels";
import { getHasStartSegment, getVideoLabel } from "./videoLabels";
import { getThumbnailSelector, setThumbnailListener } from "../../maze-utils/src/thumbnailManagement";
import { VideoID } from "../types";
import { getSegmentsForVideo } from "./segmentData";
Expand Down Expand Up @@ -59,14 +59,14 @@ function thumbnailHoverListener(e: MouseEvent) {

// Pre-fetch data for this video
let fetched = false;
const preFetch = () => {
const preFetch = async () => {
fetched = true;
const videoID = extractVideoID(thumbnail);
if (videoID) {
if (videoID && await getHasStartSegment(videoID)) {
void getSegmentsForVideo(videoID, false);
}
};
const timeout = setTimeout(preFetch, 200);
const timeout = setTimeout(preFetch, 100);
const onMouseDown = () => {
clearTimeout(timeout);
if (!fetched) {
Expand Down
29 changes: 24 additions & 5 deletions src/utils/videoLabels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import { asyncRequestToServer } from "./requests";

const utils = new Utils();

interface VideoLabelsCacheData {
category: Category;
hasStartSegment: boolean;
}

export interface LabelCacheEntry {
timestamp: number;
videos: Record<VideoID, Category>;
videos: Record<VideoID, VideoLabelsCacheData>;
}

const labelCache: Record<string, LabelCacheEntry> = {};
Expand All @@ -21,7 +26,7 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |
return cachedEntry;
}

const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}`);
const response = await asyncRequestToServer("GET", `/api/videoLabels/${hashPrefix}?hasStartSegment=true`);
if (response.status !== 200) {
// No video labels or server down
labelCache[hashPrefix] = {
Expand All @@ -36,7 +41,10 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |

const newEntry: LabelCacheEntry = {
timestamp: Date.now(),
videos: Object.fromEntries(data.map(video => [video.videoID, video.segments[0].category])),
videos: Object.fromEntries(data.map(video => [video.videoID, {
category: video.segments[0]?.category,
hasStartSegment: video.hasStartSegment
}])),
};
labelCache[hashPrefix] = newEntry;

Expand All @@ -55,17 +63,28 @@ async function getLabelHashBlock(hashPrefix: string): Promise<LabelCacheEntry |
}

export async function getVideoLabel(videoID: VideoID): Promise<Category | null> {
const prefix = (await getHash(videoID, 1)).slice(0, 3);
const prefix = (await getHash(videoID, 1)).slice(0, 4);
const result = await getLabelHashBlock(prefix);

if (result) {
const category = result.videos[videoID];
const category = result.videos[videoID]?.category;
if (category && utils.getCategorySelection(category).option !== CategorySkipOption.Disabled) {
return category;
} else {
return null;
}
}

return null;
}

export async function getHasStartSegment(videoID: VideoID): Promise<boolean | null> {
const prefix = (await getHash(videoID, 1)).slice(0, 4);
const result = await getLabelHashBlock(prefix);

if (result) {
return result?.videos[videoID]?.hasStartSegment ?? false;
}

return null;
}

0 comments on commit 236c95d

Please sign in to comment.