limit-once
Advanced tools
Comparing version 0.8.0 to 0.9.0
@@ -13,3 +13,3 @@ { | ||
"license": "MIT", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"private": false, | ||
@@ -23,5 +23,8 @@ "repository": { | ||
"exports": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/cjs/index.cjs", | ||
"types": "./dist/esm/index.d.ts" | ||
"types": { | ||
"import": "./dist/index.d.js", | ||
"require": "./dist/index.d.cjs" | ||
}, | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
@@ -41,3 +44,4 @@ "files": [ | ||
"rimraf": "^5.0.7", | ||
"tiny-invariant": "^1.3.3" | ||
"tiny-invariant": "^1.3.3", | ||
"tsup": "^8.0.2" | ||
}, | ||
@@ -48,8 +52,4 @@ "peerDependencies": { | ||
"scripts": { | ||
"prepublishOnly": "bun build:all", | ||
"build:all": "bun build:clean && bun build:dist:esm && bun build:dist:cjs && bun build:types", | ||
"build:clean": "rimraf dist", | ||
"build:dist:esm": "esbuild ./src/* --outdir=./dist/esm --format=esm", | ||
"build:dist:cjs": "esbuild ./src/* --outdir=./dist/cjs --format=cjs --out-extension:.js=.cjs", | ||
"build:types": "tsc ./src/* --outDir ./dist/esm --declaration --emitDeclarationOnly --skipLibCheck", | ||
"prepublishOnly": "bun build:dist", | ||
"build:dist": "bun tsup", | ||
"check:all": "bun check:prettier && bun check:typescript", | ||
@@ -56,0 +56,0 @@ "check:prettier": "prettier --debug-check src/**/*.ts test/**/*.ts", |
@@ -11,3 +11,3 @@ # limit-once | ||
- [Synchronous variant](#synchronous-variant) (tiny `150B`) | ||
- [Asynchronous variant for promises](#asynchronous-variant) (tiny `372B`) | ||
- [Asynchronous variant for promises](#asynchronous-variant) (tiny `360B`) | ||
- Only include the code for the variant(s) you want | ||
@@ -14,0 +14,0 @@ - Both variants support cache clearing (avoid memory leaks) |
@@ -13,4 +13,21 @@ type ResultValue<TFunc extends (this: any, ...args: any[]) => Promise<any>> = Awaited< | ||
| { type: 'pending'; promise: Promise<T>; abort: () => void } | ||
| { type: 'fulfilled'; result: T }; | ||
| { type: 'fulfilled'; promise: Promise<T> }; | ||
/** | ||
* Creates a new function that will cache the result of it's first call. | ||
* | ||
* @example | ||
* async function getUser() { | ||
* return fetch('/user').json(); | ||
* } | ||
* const getUserOnce = once(getUser); | ||
* | ||
* const user1 = await getUserOnce(); | ||
* // returns result of `getUser()` | ||
* | ||
* const user2 = await getUserOnce(); | ||
* // `getUser()` not called, previously "fulfilled" value returned | ||
* | ||
* console.log(user1 === user2); // true | ||
*/ | ||
export function onceAsync<TFunc extends (...args: any[]) => Promise<any>>( | ||
@@ -25,3 +42,3 @@ fn: TFunc, | ||
function cached( | ||
function onced( | ||
this: ThisParameterType<TFunc>, | ||
@@ -31,8 +48,5 @@ ...args: Parameters<TFunc> | ||
if (state.type === 'fulfilled') { | ||
// Doing a Promise.resolve() so that | ||
// this function _always_ returns a promise. | ||
return Promise.resolve(state.result); | ||
return state.promise; | ||
} | ||
// while the promise is pending, all folks | ||
if (state.type === 'pending') { | ||
@@ -53,3 +67,3 @@ return state.promise; | ||
type: 'fulfilled', | ||
result, | ||
promise, | ||
}; | ||
@@ -74,3 +88,3 @@ resolve(result); | ||
cached.clear = function clear() { | ||
onced.clear = function clear() { | ||
if (state.type === 'pending') { | ||
@@ -85,3 +99,3 @@ state.abort(); | ||
return cached; | ||
return onced; | ||
} |
@@ -17,6 +17,2 @@ export type CachedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
* cached('Sam'); // returns "Hello Alex" (underlying `sayHello` function not called) | ||
* | ||
* cached.clear(); | ||
* | ||
* cached('Sam'); // returns "Hello Sam" | ||
*/ | ||
@@ -23,0 +19,0 @@ export function once<TFunc extends (...args: any[]) => any>(fn: TFunc): CachedFn<TFunc> { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20907
18
370
6
1