File size: 4,298 Bytes
d02c2a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use client';
import { useEffect, useRef, useState } from 'react';
import { apiClient } from '@/utils/api';

// Module-level batcher: many cards mount at once when the virtualized grid scrolls;
// instead of N HTTP requests, queue paths and flush them as a single batch.
type Resolver = { resolve: (caption: string) => void; reject: (err: unknown) => void };
const pending = new Map<string, Resolver[]>();
const cache = new Map<string, string>();
let flushTimer: ReturnType<typeof setTimeout> | null = null;
const FLUSH_DELAY_MS = 30;
const MAX_BATCH = 200;

function scheduleFlush() {
  if (flushTimer) return;
  flushTimer = setTimeout(flush, FLUSH_DELAY_MS);
}

async function flush() {
  flushTimer = null;
  if (pending.size === 0) return;

  // Drain up to MAX_BATCH paths; if more arrived, reschedule.
  const paths: string[] = [];
  for (const path of pending.keys()) {
    paths.push(path);
    if (paths.length >= MAX_BATCH) break;
  }
  const batchResolvers = paths.map(p => ({ path: p, resolvers: pending.get(p)! }));
  for (const p of paths) pending.delete(p);

  try {
    const res = await apiClient.post('/api/caption/getBatch', { imgPaths: paths });
    const captions: Record<string, string> = res.data?.captions ?? {};
    for (const { path, resolvers } of batchResolvers) {
      const value = captions[path] ?? '';
      cache.set(path, value);
      for (const r of resolvers) r.resolve(value);
    }
  } catch (err) {
    for (const { resolvers } of batchResolvers) {
      for (const r of resolvers) r.reject(err);
    }
  }

  if (pending.size > 0) scheduleFlush();
}

function requestCaption(path: string, signal?: AbortSignal): Promise<string> {
  return new Promise((resolve, reject) => {
    if (signal?.aborted) {
      reject(new DOMException('Aborted', 'AbortError'));
      return;
    }
    const resolver: Resolver = { resolve, reject };
    const list = pending.get(path);
    if (list) {
      list.push(resolver);
    } else {
      pending.set(path, [resolver]);
    }
    if (signal) {
      const onAbort = () => {
        // Remove this resolver from the pending batch. If no other card is
        // still waiting on the same path, drop the path entirely so the next
        // batch doesn't include it.
        const arr = pending.get(path);
        if (arr) {
          const idx = arr.indexOf(resolver);
          if (idx >= 0) arr.splice(idx, 1);
          if (arr.length === 0) pending.delete(path);
        }
        reject(new DOMException('Aborted', 'AbortError'));
      };
      signal.addEventListener('abort', onAbort, { once: true });
    }
    scheduleFlush();
  });
}

export function invalidateCaption(path: string) {
  cache.delete(path);
}

export function setCachedCaption(path: string, caption: string) {
  cache.set(path, caption);
}

// Fetches caption for a path, using the module-level batcher + cache.
// `refreshKey` busts the cache (e.g. after external edits or auto-captioning poll).
export default function useCaptionBatch(imgPath: string | null, refreshKey: number = 0) {
  const [caption, setCaption] = useState<string>(() => (imgPath ? (cache.get(imgPath) ?? '') : ''));
  const [isLoaded, setIsLoaded] = useState<boolean>(() => Boolean(imgPath && cache.has(imgPath)));
  const lastPathRef = useRef<string | null>(null);

  useEffect(() => {
    if (!imgPath) {
      setCaption('');
      setIsLoaded(false);
      return;
    }

    if (refreshKey > 0) invalidateCaption(imgPath);

    const cached = cache.get(imgPath);
    if (cached !== undefined) {
      setCaption(cached);
      setIsLoaded(true);
      lastPathRef.current = imgPath;
      return;
    }

    let cancelled = false;
    const controller = new AbortController();
    lastPathRef.current = imgPath;
    setIsLoaded(false);
    requestCaption(imgPath, controller.signal)
      .then(value => {
        if (cancelled || lastPathRef.current !== imgPath) return;
        setCaption(value);
        setIsLoaded(true);
      })
      .catch(err => {
        if (err?.name === 'AbortError' || cancelled) return;
        console.error('Error fetching caption:', err);
        setIsLoaded(true);
      });

    return () => {
      cancelled = true;
      controller.abort();
    };
  }, [imgPath, refreshKey]);

  return { caption, isLoaded, setCaption };
}