{"_id":"@alexandermzchiu/singleflight","name":"@alexandermzchiu/singleflight","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@alexandermzchiu/singleflight","version":"0.0.1","description":"singleflight is a utility that prevents duplicate asynchronous operations by ensuring that only one execution per unique key is in progress at a time, reducing redundant processing and improving efficiency.","author":{"name":"Alexander Zhao","email":"alexandermzchiu@gmail.com"},"repository":{"type":"git","url":"git+ssh://git@github.com/ChiuMungZitAlexander/singleflight.git"},"license":"MIT","packageManager":"pnpm@10.10.0","engines":{"node":"^22.0.0"},"type":"module","main":"dist/index.cjs.js","module":"dist/index.esm.js","types":"dist/types/index.d.ts","scripts":{"build":"rm -rf dist && rollup -c","test":"jest"},"keywords":[],"devDependencies":{"@babel/preset-env":"^7.27.2","@rollup/plugin-babel":"^6.0.4","@rollup/plugin-commonjs":"^28.0.3","@rollup/plugin-node-resolve":"^16.0.1","@rollup/plugin-typescript":"^12.1.2","@types/jest":"^29.5.14","jest":"^29.7.0","rollup":"^4.40.2","rollup-plugin-typescript2":"^0.36.0","ts-jest":"^29.3.2","tslib":"^2.8.1","typescript":"^5.8.3"},"_id":"@alexandermzchiu/singleflight@0.0.1","gitHead":"dac9eb4d78f711fd114d0a8186ffd481389a4afc","bugs":{"url":"https://github.com/ChiuMungZitAlexander/singleflight/issues"},"homepage":"https://github.com/ChiuMungZitAlexander/singleflight#readme","_nodeVersion":"22.13.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-I9A8q7ktRhujQAmzZZ9dCthaM7J5w1wzthcTsBTD/yBxeUqEsPqt1nS9t3iZ5E6wQv8TeQn23S9Qvprl+fy8Tw==","shasum":"b9c5a46adc0fe1bd6c9d60e4c5abc6767504d2a3","tarball":"https://registry.npmjs.org/@alexandermzchiu/singleflight/-/singleflight-0.0.1.tgz","fileCount":8,"unpackedSize":57859,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIFq7SdSE4/kV/utLgn5JkcGEgIVQTAzTNfe4j4f9NoGJAiEAlYvJ3zXQ+TmiYG9lFZDPo4DrjQ+6gcy2hKINSWhdieY="}]},"_npmUser":{"name":"amz90","email":"alexandermzchiu@gmail.com"},"directories":{},"maintainers":[{"name":"amz90","email":"alexandermzchiu@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/singleflight_0.0.1_1747039512616_0.0744881715072454"},"_hasShrinkwrap":false}},"time":{"created":"2025-05-12T08:45:12.526Z","0.0.1":"2025-05-12T08:45:12.792Z","modified":"2025-05-12T08:45:13.076Z"},"maintainers":[{"name":"amz90","email":"alexandermzchiu@gmail.com"}],"description":"singleflight is a utility that prevents duplicate asynchronous operations by ensuring that only one execution per unique key is in progress at a time, reducing redundant processing and improving efficiency.","homepage":"https://github.com/ChiuMungZitAlexander/singleflight#readme","keywords":[],"repository":{"type":"git","url":"git+ssh://git@github.com/ChiuMungZitAlexander/singleflight.git"},"author":{"name":"Alexander Zhao","email":"alexandermzchiu@gmail.com"},"bugs":{"url":"https://github.com/ChiuMungZitAlexander/singleflight/issues"},"license":"MIT","readme":"## singleflight\n\nThe `singleflight` class is a utility that ensures only one instance of a given asynchronous operation (identified by a unique key) is executed at a time. If multiple calls are made with the same key, they will share the same promise and receive the same result instead of triggering duplicate executions.\n\n### How It Works\n\n- The class maintains a **private map (`#flightsMap`)** that tracks ongoing asynchronous operations using unique string keys.\n- The `do<T>(key: string, fn: () => Promise<T>)` method:\n  - Checks if an operation with the given key is already running. If so, it returns the existing promise.\n  - If not, it invokes the provided function (`fn`) and stores the resulting promise in the map.\n  - Once the promise resolves or rejects, it is removed from the map to allow future executions.\n- The `flightsCount()` method returns the current number of active asynchronous operations.\n\n### Use Case\n\nThe `singleflight` class is useful for preventing redundant API calls, database queries, or expensive computations when multiple requests for the same operation occur simultaneously.\n\n### Example Usage\n\n```typescript\nconst singleFlight = new SingleFlight();\n\nasync function fetchData(id: string) {\n  return singleFlight.do(id, async () => {\n    console.log(`Fetching data for ${id}...`);\n    return new Promise((resolve) =>\n      setTimeout(() => resolve(`Data for ${id}`), 1000)\n    );\n  });\n}\n\nfetchData(\"user-123\");\nfetchData(\"user-123\"); // This call will reuse the first request instead of making a new one.\n```\n","readmeFilename":"README.md","_rev":"1-72684f3afe3fff23b35ad18e0803819c"}