{"version":3,"sources":["../src/memoize.ts"],"sourcesContent":["// ─────────────────────────────────────────────────────────────────────────────\n// Simple memoize an async-loaded result once.\n//\n// Interface design notes:\n// 1. Retry on errors - supported. If an error is thrown, then nothing is\n//    memoized.\n// 2. Cache invalidation - does not seem useful because all the user needs to\n//    do is to call memoize again.\n// 3. TTL - could be useful, but doesn't seem necessary to have. Niche use case.\n// 4. Memoize different results for different function arguments - Could be\n//    useful, but not the main use case we are targeting for.\n// ─────────────────────────────────────────────────────────────────────────────\n\n/**\n * Memoizes the result of an async loader function.\n *\n * - The loader is only executed once.\n * - Concurrent callers share the same in-flight promise.\n * - Subsequent calls return the cached value.\n *\n * Example usage:\n *\n *   const getSomeData = memoize(async () => {\n *     return await loadSomethingExpensive();\n *   });\n *\n * Or just use:\n *\n *   const getSomeData = memoize(loadSomethingExpensive);\n *   ...\n *   // Slow in the first call, immediate in subsequent calls.\n *   const data = await getSomeData();\n *\n */\nexport function memoize<T>(loader: () => Promise<T>): () => Promise<T> {\n  let promise: Promise<T> | undefined;\n\n  return (): Promise<T> => {\n    if (!promise) {\n      promise = loader().catch((error) => {\n        // Clear the cache so the next call retries\n        promise = undefined;\n        throw error;\n      });\n    }\n\n    return promise;\n  };\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAkCO,SAASE,EAAWE,EAA4C,CACrE,IAAIC,EAEJ,MAAO,KACAA,IACHA,EAAUD,EAAO,EAAE,MAAOE,GAAU,CAElC,MAAAD,EAAU,OACJC,CACR,CAAC,GAGID,EAEX","names":["memoize_exports","__export","memoize","__toCommonJS","loader","promise","error"]}