@devries-cameron20 / t3code Mirror · read-only / commits / fbfebc8537
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 11:00 UTC

fix(mobile): video previews survive re-signed URLs and a late-attached player item (#32196)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Bartłomiej Błoniarz committed Nov 15, 2026 at 06:07 UTC fbfebc85378f5d7013adcd72ed86c9ea8cf1ee26
3 files changed +97 -30
apps/mobile/src/components/VideoThumbnailImage.tsx
+1 -1
@@ -41,7 +41,7 @@ export function VideoThumbnailImage(props: {
41 readonly contentFit?: "cover" | "contain";
42 }) {
43 const { cacheKey, source } = props;
44 - // Extraction runs only while the screen is focused; leaving it interrupts the load.
44 + // Extraction starts only while the screen is focused; leaving it skips queued loads.
45 const isFocused = useIsFocused();
46 const loaded = useAtomValue(
47 source && isFocused ? videoThumbnailAtom({ cacheKey, source }) : noVideoThumbnail,
apps/mobile/src/lib/videoThumbnails.test.ts
+76 -17
@@ -5,11 +5,20 @@ vi.mock("expo-video", () => ({ createVideoPlayer: mocks.createPlayer }));
5
6 let thumbnails: typeof import("./videoThumbnails");
7 const frame = { width: 480, height: 270 };
8 -const player = () => ({
9 - replaceAsync: vi.fn(async (): Promise<void> => {}),
10 - generateThumbnailsAsync: vi.fn(async () => [frame]),
11 - release: vi.fn(),
12 -});
8 +const player = () => {
9 + const listeners = new Set<(event: { source: unknown }) => void>();
10 + const attach = () => listeners.forEach((listener) => listener({ source: {} }));
11 + return {
12 + attach,
13 + addListener: vi.fn((_event: string, listener: (event: { source: unknown }) => void) => {
14 + listeners.add(listener);
15 + return { remove: () => listeners.delete(listener) };
16 + }),
17 + replaceAsync: vi.fn(async (): Promise<void> => queueMicrotask(attach)),
18 + generateThumbnailsAsync: vi.fn(async () => [frame]),
19 + release: vi.fn(),
20 + };
21 +};
22 const source = () => ({ uri: "file:///clip.mp4", dispose: vi.fn() });
23
24 beforeEach(async () => {
@@ -51,7 +60,7 @@ describe("video thumbnails", () => {
60 expect(first.release).toHaveBeenCalledTimes(1);
61 });
62
54 - it("releases an active canceled player and ignores late source loading", async () => {
63 + it("finishes a started extraction after its reader leaves so a re-signed URL reuses it", async () => {
64 const started = Promise.withResolvers<void>();
65 const replaced = Promise.withResolvers<void>();
66 const first = player();
@@ -62,21 +71,71 @@ describe("video thumbnails", () => {
71 mocks.createPlayer.mockReturnValueOnce(first);
72 const file = source();
73 const controller = new AbortController();
65 - const request = thumbnails.loadVideoThumbnail("canceled", async () => file, controller.signal);
74 + const request = thumbnails.loadVideoThumbnail("video", async () => file, controller.signal);
75 await started.promise;
76 controller.abort();
68 - expect(await request).toBeNull();
77 + const resigned = thumbnails.loadVideoThumbnail(
78 + "video",
79 + async () => source(),
80 + new AbortController().signal,
81 + );
82 + replaced.resolve();
83 + first.attach();
84 + expect(await request).toBe(frame);
85 + expect(await resigned).toBe(frame);
86 + expect(mocks.createPlayer).toHaveBeenCalledTimes(1);
87 expect(first.release).toHaveBeenCalledTimes(1);
88 expect(file.dispose).toHaveBeenCalledTimes(1);
71 - replaced.resolve();
72 - expect(
73 - await thumbnails.loadVideoThumbnail(
74 - "next",
75 - async () => source(),
76 - new AbortController().signal,
77 - ),
78 - ).toBe(frame);
89 + });
90 +
91 + it("asks for a frame only once the player has attached the item", async () => {
92 + const first = player();
93 + let attached = false;
94 + // Like iOS: replaceAsync resolves before the item attaches, and until then
95 + // generateThumbnailsAsync finds no asset.
96 + first.replaceAsync.mockImplementation(async () => {});
97 + first.generateThumbnailsAsync.mockImplementation(async () => (attached ? [frame] : []));
98 + mocks.createPlayer.mockReturnValueOnce(first);
99 + const request = thumbnails.loadVideoThumbnail(
100 + "attach",
101 + async () => source(),
102 + new AbortController().signal,
103 + );
104 + await vi.waitFor(() => expect(first.replaceAsync).toHaveBeenCalled());
105 + await first.replaceAsync.mock.results[0]!.value;
106 + attached = true;
107 + first.attach();
108 + expect(await request).toBe(frame);
109 + });
110 +
111 + it("gives up on an item that never attaches without holding up the next request", async () => {
112 + vi.useFakeTimers();
113 + const first = player();
114 + const subscriptions: Array<{ remove: ReturnType<typeof vi.fn> }> = [];
115 + first.addListener.mockImplementation(() => {
116 + const subscription = { remove: vi.fn() };
117 + subscriptions.push(subscription);
118 + return subscription;
119 + });
120 + first.replaceAsync.mockImplementation(async () => {});
121 + mocks.createPlayer.mockReturnValueOnce(first);
122 + const file = source();
123 + const stuck = thumbnails.loadVideoThumbnail(
124 + "never-attached",
125 + async () => file,
126 + new AbortController().signal,
127 + );
128 + const next = thumbnails.loadVideoThumbnail(
129 + "next",
130 + async () => source(),
131 + new AbortController().signal,
132 + );
133 + await vi.advanceTimersByTimeAsync(15_000);
134 + expect(await stuck).toBeNull();
135 expect(first.generateThumbnailsAsync).not.toHaveBeenCalled();
80 - expect(thumbnails.cachedVideoThumbnail("canceled")).toBeNull();
136 + expect(subscriptions[0]!.remove).toHaveBeenCalledTimes(1);
137 + expect(first.release).toHaveBeenCalledTimes(1);
138 + expect(file.dispose).toHaveBeenCalledTimes(1);
139 + expect(await next).toBe(frame);
140 });
141 });
apps/mobile/src/lib/videoThumbnails.ts
+20 -12
@@ -10,35 +10,40 @@ export function cachedVideoThumbnail(key: string): VideoThumbnail | null {
10 return thumbnails.get(key) ?? null;
11 }
12
13 -async function extractFrame(uri: string, signal: AbortSignal) {
13 +async function extractFrame(uri: string) {
14 const { createVideoPlayer } = await import("expo-video");
15 - if (signal.aborted) return null;
15 const player = createVideoPlayer(null);
16 let disposed = false;
18 - let cancel = () => {};
17 let timeout: ReturnType<typeof setTimeout> | undefined;
18 + let attachedSubscription: { remove(): void } | undefined;
19 try {
20 // Never play or change audio settings: thumbnails must leave the shared audio session alone.
21 player.bufferOptions = { preferredForwardBufferDuration: 1 };
23 - const canceled = new Promise<null>((resolve) => {
24 - cancel = () => resolve(null);
22 + const timedOut = new Promise<null>((resolve) => {
23 + timeout = setTimeout(() => resolve(null), 15_000);
24 + });
25 + // iOS resolves replaceAsync before the main thread attaches the item, and until then
26 + // generateThumbnailsAsync finds no asset and returns no frames.
27 + const attached = new Promise<void>((resolve) => {
28 + attachedSubscription = player.addListener("sourceChange", ({ source }) => {
29 + if (source) resolve();
30 + });
31 });
26 - signal.addEventListener("abort", cancel, { once: true });
27 - timeout = setTimeout(cancel, 15_000);
32 const frame = (async () => {
33 await player.replaceAsync({ uri, contentType: "progressive" });
30 - if (disposed || signal.aborted) return null;
34 + await attached;
35 + if (disposed) return null;
36 const [thumbnail] = await player.generateThumbnailsAsync([0], {
37 maxWidth: 480,
38 maxHeight: 480,
39 });
40 return thumbnail ?? null;
41 })();
37 - return await Promise.race([frame, canceled]);
42 + return await Promise.race([frame, timedOut]);
43 } finally {
44 disposed = true;
45 clearTimeout(timeout);
41 - signal.removeEventListener("abort", cancel);
46 + attachedSubscription?.remove();
47 player.release();
48 }
49 }
@@ -62,8 +67,11 @@ export function loadVideoThumbnail(
67 const source = await resolveSource(signal);
68 if (!source) return null;
69 try {
65 - const thumbnail = await extractFrame(source.uri, signal);
66 - if (!thumbnail || signal.aborted) return null;
70 + if (signal.aborted) return null;
71 + // A started extraction finishes even if its reader leaves: its requests are already in
72 + // flight, and a re-signed URL for the same video replaces the reader, not the frame.
73 + const thumbnail = await extractFrame(source.uri);
74 + if (!thumbnail) return null;
75 thumbnails.set(key, thumbnail);
76 if (thumbnails.size > MAX_CACHED_THUMBNAILS) {
77 thumbnails.delete(thumbnails.keys().next().value!);