{"version":3,"file":"retry.mjs","names":[],"sources":["../../src/promise/retry.ts"],"sourcesContent":["import type { RetryOptions } from '../types';\nimport { abortError } from './abort';\nimport { delay } from './delay';\n\n/**\n * Retries a function until it succeeds or max attempts are reached.\n *\n * @template T - The type of the resolved value\n * @param fn - The function to retry (can be async or return a promise)\n * @param options - The options object\n * @param options.maxAttempts - Maximum number of attempts (default: 3)\n * @param options.delay - Delay between attempts in milliseconds (default: 1000)\n * @param options.backoff - Backoff multiplier for exponential backoff (default: 1, no backoff)\n * @param options.onRetry - Callback function called on each retry with the attempt number and error\n * @param options.signal - Optional AbortSignal that cancels remaining attempts when aborted\n * @returns Returns a promise that resolves with the function result or rejects with the last error\n *\n * @example\n * const result = await retry(() => fetchData(), { maxAttempts: 5, delay: 1000 });\n *\n * @example\n * const result = await retry(() => fetchData(), {\n *   maxAttempts: 5,\n *   delay: 1000,\n *   backoff: 2, // exponential backoff\n *   onRetry: (attempt, error) => console.log(`Attempt ${attempt} failed: ${error.message}`)\n * });\n */\nexport function retry<T>(fn: () => T | Promise<T>, options: RetryOptions = {}): Promise<T> {\n  const { maxAttempts = 3, delay: initialDelay = 1000, backoff = 1, onRetry, signal } = options;\n\n  if (signal?.aborted) {\n    return Promise.reject(abortError(signal));\n  }\n\n  let lastError: Error;\n  let attempt = 0;\n\n  async function execute(): Promise<T> {\n    if (signal?.aborted) {\n      throw abortError(signal);\n    }\n\n    attempt++;\n\n    try {\n      return await Promise.resolve(fn());\n    } catch (error) {\n      lastError = error instanceof Error ? error : new Error(String(error));\n\n      if (attempt >= maxAttempts) {\n        throw lastError;\n      }\n\n      if (signal?.aborted) {\n        throw abortError(signal);\n      }\n\n      if (onRetry) {\n        onRetry(attempt, lastError);\n      }\n\n      if (signal?.aborted) {\n        throw abortError(signal);\n      }\n\n      const waitTime = initialDelay * Math.pow(backoff, attempt - 1);\n      await delay(waitTime, undefined, signal);\n\n      return execute();\n    }\n  }\n\n  return execute();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,SAAgB,MAAS,IAA0B,UAAwB,CAAC,GAAe;CACzF,MAAM,EAAE,cAAc,GAAG,OAAO,eAAe,KAAM,UAAU,GAAG,SAAS,WAAW;CAEtF,IAAI,QAAQ,SACV,OAAO,QAAQ,OAAO,WAAW,MAAM,CAAC;CAG1C,IAAI;CACJ,IAAI,UAAU;CAEd,eAAe,UAAsB;EACnC,IAAI,QAAQ,SACV,MAAM,WAAW,MAAM;EAGzB;EAEA,IAAI;GACF,OAAO,MAAM,QAAQ,QAAQ,GAAG,CAAC;EACnC,SAAS,OAAO;GACd,YAAY,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;GAEpE,IAAI,WAAW,aACb,MAAM;GAGR,IAAI,QAAQ,SACV,MAAM,WAAW,MAAM;GAGzB,IAAI,SACF,QAAQ,SAAS,SAAS;GAG5B,IAAI,QAAQ,SACV,MAAM,WAAW,MAAM;GAGzB,MAAM,WAAW,eAAe,KAAK,IAAI,SAAS,UAAU,CAAC;GAC7D,MAAM,MAAM,UAAU,KAAA,GAAW,MAAM;GAEvC,OAAO,QAAQ;EACjB;CACF;CAEA,OAAO,QAAQ;AACjB"}