MCPcopy Create free account
hub / github.com/Thanas-R/Virdis / invokeWithRetry

Function invokeWithRetry

src/lib/invoke-with-retry.ts:36–66  ·  view source on GitHub ↗
(
  fn: string,
  body: Record<string, unknown> | undefined,
  { retries = 3, baseDelayMs = 800, isEmpty, signal }: RetryOptions = {}
)

Source from the content-addressed store, hash-verified

34 * Returns the final data (may still be a fallback after all retries exhausted).
35 */
36export async function invokeWithRetry<T = any>(
37 fn: string,
38 body: Record<string, unknown> | undefined,
39 { retries = 3, baseDelayMs = 800, isEmpty, signal }: RetryOptions = {}
40): Promise<T | null> {
41 let lastData: T | null = null;
42 for (let attempt = 0; attempt <= retries; attempt++) {
43 if (signal?.aborted) return lastData;
44 try {
45 const { data, error } = await supabase.functions.invoke(fn, { body });
46 if (error) throw error;
47 lastData = data as T;
48 const empty = isFallbackPayload(data) || (isEmpty ? isEmpty(data) : false);
49 if (!empty) return lastData;
50 // else fall through to retry
51 } catch (e) {
52 // swallow & retry
53 // eslint-disable-next-line no-console
54 console.warn(`[invokeWithRetry] ${fn} attempt ${attempt + 1} failed`, e);
55 }
56 if (attempt < retries) {
57 const delay = baseDelayMs * Math.pow(2, attempt) + Math.floor(Math.random() * 400);
58 try {
59 await sleep(delay);
60 } catch {
61 return lastData;
62 }
63 }
64 }
65 return lastData;
66}

Callers

nothing calls this directly

Calls 2

isFallbackPayloadFunction · 0.90
sleepFunction · 0.70

Tested by

no test coverage detected