{"version":3,"file":"imports.cjs","names":[],"sources":["../src/imports/fetch-lazy-module.ts","../src/imports/unpack-async-module.ts"],"sourcesContent":["import { sleep } from 'yummies/async';\n\n/**\n * Lazily loads a module with retry support.\n *\n * @example\n * ```ts\n * fetchLazyModule(() => import('./test.ts'), 3) // starts loading test.ts\n * // If loading test.ts fails, fetchLazyModule retries by calling fetchModule() again\n * // It retries as many times as specified by attempts (3 by default)\n * ```\n */\nexport const fetchLazyModule = async <T>(\n  fetchModule: () => Promise<T>,\n  attempts = 3,\n  delay = 1000,\n): Promise<T> => {\n  const attemptsArray = Array.from<typeof fetchModule>({\n    length: attempts,\n  }).fill(fetchModule);\n\n  let lastError: null | Error = null;\n\n  for await (const attempt of attemptsArray) {\n    try {\n      if (lastError !== null) {\n        await sleep(delay);\n      }\n      return await attempt();\n    } catch (error) {\n      lastError = error as Error;\n    }\n  }\n  throw lastError;\n};\n","export type PackedAsyncModule<T> = Promise<T | { default: T }>;\n\n/**\n * Resolves either a direct value or an asynchronously imported module and unwraps its `default` export.\n *\n * @example\n * ```ts\n * const component = await unpackAsyncModule(import('./Component.ts'));\n * ```\n */\nexport const unpackAsyncModule = async <T>(\n  maybeModule: T | PackedAsyncModule<T>,\n): Promise<T> => {\n  if (maybeModule instanceof Promise) {\n    const data = (await maybeModule) as any;\n\n    if ((data as any).default) {\n      return (data as any).default;\n    } else {\n      return data;\n    }\n  }\n\n  return maybeModule;\n};\n"],"mappings":";;;;;;;;;;;;;AAYA,IAAa,kBAAkB,OAC7B,aACA,WAAW,GACX,QAAQ,QACO;CACf,MAAM,gBAAgB,MAAM,KAAyB,EACnD,QAAQ,SACV,CAAC,EAAE,KAAK,WAAW;CAEnB,IAAI,YAA0B;CAE9B,WAAW,MAAM,WAAW,eAC1B,IAAI;EACF,IAAI,cAAc,MAChB,OAAA,GAAA,cAAA,OAAY,KAAK;EAEnB,OAAO,MAAM,QAAQ;CACvB,SAAS,OAAO;EACd,YAAY;CACd;CAEF,MAAM;AACR;;;;;;;;;;;ACxBA,IAAa,oBAAoB,OAC/B,gBACe;CACf,IAAI,uBAAuB,SAAS;EAClC,MAAM,OAAQ,MAAM;EAEpB,IAAK,KAAa,SAChB,OAAQ,KAAa;OAErB,OAAO;CAEX;CAEA,OAAO;AACT"}