Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

feat(photos):prefetch-nearby-slideshow-photos

Aswin Asok374cfd60661e
diff --git a/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx b/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx
index a46f74756c..f84472b449 100644
--- a/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx
+++ b/web/apps/photos/src/components/Collections/AlbumSlideshow.tsx
@@ -34,6 +34,7 @@ import {
saveSlideshowSettings,
scheduleSlideshowAdvance,
slideshowIndex,
+ slideshowPrefetchFiles,
type SlideshowSettings,
} from "./album-slideshow";
import { AlbumSlideshowSettings } from "./AlbumSlideshowSettings";
@@ -55,6 +56,7 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
const [visible, setVisible] = useState(() => !document.hidden);
const [controlsVisible, setControlsVisible] = useState(true);
const controlsHideTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
+ const prefetchDirection = useRef(1);
const { setIsFileViewerOpen } = usePhotosAppContext();
const current = orderedFiles[slide.index]!;
@@ -76,6 +78,7 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
const navigate = useCallback(
(offset: number) => {
+ prefetchDirection.current = offset;
setSlide((previous) => {
const index = slideshowIndex(
previous.index,
@@ -124,6 +127,20 @@ export function AlbumSlideshow({ files, title, onClose }: AlbumSlideshowProps) {
],
);
+ useEffect(() => {
+ // ponytail: the download manager already caches and deduplicates these requests.
+ for (const file of slideshowPrefetchFiles(
+ orderedFiles,
+ slide.index,
+ prefetchDirection.current,
+ )) {
+ void downloadManager
+ .renderableThumbnailURL(file)
+ .then(() => downloadManager.renderableSourceURLs(file))
+ .catch(() => undefined);
+ }
+ }, [orderedFiles, slide.index]);
+
useEffect(() => {
const changed = () => setVisible(!document.hidden);
document.addEventListener("visibilitychange", changed);
diff --git a/web/apps/photos/src/components/Collections/album-slideshow.ts b/web/apps/photos/src/components/Collections/album-slideshow.ts
index 5e89c522ce..4ae4fa78d2 100644
--- a/web/apps/photos/src/components/Collections/album-slideshow.ts
+++ b/web/apps/photos/src/components/Collections/album-slideshow.ts
@@ -52,6 +52,20 @@ export const slideshowFiles = (files: EnteFile[]) =>
export const slideshowIndex = (index: number, offset: number, count: number) =>
count ? (index + offset + count) % count : 0;
+export const slideshowPrefetchFiles = (
+ files: EnteFile[],
+ index: number,
+ direction: number,
+) => {
+ if (files.length < 2) return [];
+ const indexes = [1, 2, -1].map((offset) =>
+ slideshowIndex(index, offset * direction, files.length),
+ );
+ return [...new Set(indexes)]
+ .filter((nextIndex) => nextIndex !== index)
+ .map((nextIndex) => files[nextIndex]!);
+};
+
export function scheduleSlideshowAdvance(
{
enabled,
diff --git a/web/apps/photos/tests/album-slideshow.test.ts b/web/apps/photos/tests/album-slideshow.test.ts
index 6ae91417af..fabc21082c 100644
--- a/web/apps/photos/tests/album-slideshow.test.ts
+++ b/web/apps/photos/tests/album-slideshow.test.ts
@@ -5,6 +5,7 @@ import {
scheduleSlideshowAdvance,
slideshowFiles,
slideshowIndex,
+ slideshowPrefetchFiles,
} from "../src/components/Collections/album-slideshow";
const photo = (id: number, fileType: FileType = FileType.image) =>
@@ -34,6 +35,22 @@ test("navigation loops in both directions and handles empty and single-photo alb
expect(slideshowIndex(0, -1, 0)).toBe(0);
});
+test("prefetches two upcoming photos and one previous photo without duplicates", () => {
+ const files = [photo(1), photo(2), photo(3), photo(4)];
+ expect(slideshowPrefetchFiles(files, 0, 1)).toEqual([
+ files[1],
+ files[2],
+ files[3],
+ ]);
+ expect(slideshowPrefetchFiles(files, 0, -1)).toEqual([
+ files[3],
+ files[2],
+ files[1],
+ ]);
+ expect(slideshowPrefetchFiles(files.slice(0, 2), 0, 1)).toEqual([files[1]]);
+ expect(slideshowPrefetchFiles(files.slice(0, 1), 0, 1)).toEqual([]);
+});
+
describe("playback timeout", () => {
const ready = { enabled: true, ready: true, count: 2 };
test.each([5, 15, 600])(