{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["export type TOnceArgs = Parameters<typeof once>;\n\nexport type TOnceReturn = ReturnType<typeof once>;\n\n/**\n * Returns a function that invokes the source function once and reuses its result.\n * @template T\n * @param {T} fn Source function\n * @returns {(...args: Parameters<T>) => ReturnType<T>} One-time function\n * @throws {TypeError} once: fn must be a function\n * @throws {unknown} Re-throws and remembers an error from the first invocation\n * @example\n * const initialize = once(() => ({ ready: true }));\n * initialize() === initialize(); // true\n * @example\n * // Initialize an analytics SDK only once across multiple components\n * const initializeAnalytics = once(() => analytics.init(config));\n * initializeAnalytics();\n */\nexport const once = <T extends (...args: any[]) => any>(\n  fn: T\n): ((...args: Parameters<T>) => ReturnType<T>) => {\n  if (typeof fn !== \"function\") {\n    throw new TypeError(\"once: fn must be a function\");\n  }\n\n  let isCalled = false;\n  let error: unknown;\n  let isFailed = false;\n  let result: ReturnType<T>;\n  return function onceFunction(this: unknown, ...args: Parameters<T>): ReturnType<T> {\n    if (!isCalled) {\n      isCalled = true;\n      try {\n        result = fn.apply(this, args) as ReturnType<T>;\n      } catch (caughtError) {\n        error = caughtError;\n        isFailed = true;\n        throw caughtError;\n      }\n    }\n    if (isFailed) {\n      throw error;\n    }\n    return result;\n  };\n};\n"],"names":["once","fn","TypeError","isCalled","error","isFailed","result","onceFunction","args","apply","this","caughtError"],"mappings":"yDAmBO,MAAMA,KACXC,KAEA,UAAWA,KAAO,WAChB,MAAM,IAAIC,UAAU,+BAGtB,IAAIC,SAAW,MACf,IAAIC,MACJ,IAAIC,SAAW,MACf,IAAIC,OACJ,OAAO,SAASC,gBAA+BC,MAC7C,IAAKL,SAAU,CACbA,SAAW,KACX,IACEG,OAASL,GAAGQ,MAAMC,KAAMF,KAC1B,CAAE,MAAOG,aACPP,MAAQO,YACRN,SAAW,KACX,MAAMM,WACR,CACF,CACA,GAAIN,SACF,MAAMD,MAER,OAAOE,MACT"}