{"version":3,"file":"timeout.cjs","names":["handle: NodeJS.Timeout | undefined"],"sources":["../../src/utils/timeout.ts"],"sourcesContent":["/**\n * Races a promise against a timer and clears the timer however the race settles,\n * so a finished operation never leaves a pending timeout keeping the event loop alive.\n * @param promise - The operation to bound\n * @param ms - Milliseconds to wait before rejecting\n * @param error - Error to reject with when the timer wins\n * @returns The promise's value, or a rejection with `error` on timeout\n */\nexport const withTimeout = <T>(promise: Promise<T>, ms: number, error: Error): Promise<T> => {\n  let handle: NodeJS.Timeout | undefined;\n  const timeout = new Promise<never>((_resolve, reject) => {\n    handle = setTimeout(() => reject(error), ms);\n  });\n  return Promise.race([promise, timeout]).finally(() => {\n    if (handle) {\n      clearTimeout(handle);\n    }\n  });\n};\n\n/**\n * Restart delay for the n-th consecutive failure: exponential from `baseMs`, capped at `maxMs`.\n * @param attempt - 1 for the first restart, 2 for the second, and so on\n */\nexport const retryDelay = (attempt: number, baseMs: number, maxMs: number): number => {\n  const exponent = Math.max(0, attempt - 1);\n  return Math.min(baseMs * Math.pow(2, exponent), maxMs);\n};\n"],"mappings":";;;;;;;;;;AAQA,MAAa,eAAkB,SAAqB,IAAY,UAA6B;CAC3F,IAAIA;CACJ,MAAM,UAAU,IAAI,SAAgB,UAAU,WAAW;AACvD,WAAS,iBAAiB,OAAO,MAAM,EAAE,GAAG;GAC5C;AACF,QAAO,QAAQ,KAAK,CAAC,SAAS,QAAQ,CAAC,CAAC,cAAc;AACpD,MAAI,OACF,cAAa,OAAO;GAEtB;;;;;;AAOJ,MAAa,cAAc,SAAiB,QAAgB,UAA0B;CACpF,MAAM,WAAW,KAAK,IAAI,GAAG,UAAU,EAAE;AACzC,QAAO,KAAK,IAAI,SAAS,KAAK,IAAI,GAAG,SAAS,EAAE,MAAM"}