{"version":3,"sources":["../src/task/taskify/taskify.ts"],"sourcesContent":["/**\n * Creates a new function that, when called, returns the same promise as the\n * original function. If the returned function is called multiple times, it will\n * always return the same promise, ensuring that the original function is only\n * executed once.\n * \n * Note that a task's computation must not be able to fail.\n *\n * @param fn - A function that returns a promise\n * @returns A new function that returns the same promise as the original function\n *\n * @category task\n * @since 0.0.1\n\n * @example\n * import { taskify } from '@dankreiger/puppy-fp'\n * \n * (async () => {\n *   const longRunning = () => new Promise((resolve) => setTimeout(resolve, 1000));\n *   const runTask = taskify(longRunning);\n *   const t0 = performance.now();\n *   await runTask();\n *   const t1 = performance.now();\n *   const duration1 = t1 - t0;\n *   console.assert(duration1 >= 1000, { expect: 1000, actual: duration1 });\n *   await runTask();\n *   const t2 = performance.now();\n *   const duration2 = t2 - t1;\n *   console.assert(duration2 === 100000, { expect: 1000, actual: duration2 });\n * })();\n * \n * \n * \n */\nexport function taskify<T>(fn: () => Promise<T>): () => Promise<T> {\n  let task: Promise<T> | undefined;\n\n  return () => {\n    if (!task) {\n      task = fn();\n    }\n    return task;\n  };\n}\n"],"mappings":";AAkCO,SAAS,QAAW,IAAwC;AACjE,MAAI;AAEJ,SAAO,MAAM;AACX,QAAI,CAAC,MAAM;AACT,aAAO,GAAG;AAAA,IACZ;AACA,WAAO;AAAA,EACT;AACF;","names":[]}