limit-once
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -1,2 +0,2 @@ | ||
export { once } from './once.js'; | ||
export { onceAsync } from './once-async.js'; | ||
export { OncedFn, once } from './once.js'; | ||
export { OnceAsyncFn, onceAsync } from './once-async.js'; |
type ResultValue<TFunc extends (this: any, ...args: any[]) => Promise<any>> = Awaited<ReturnType<TFunc>>; | ||
type CachedFn<TFunc extends (this: any, ...args: any[]) => Promise<any>> = { | ||
type OnceAsyncFn<TFunc extends (this: any, ...args: any[]) => Promise<any>> = { | ||
/** | ||
* Clear the cached `"fulfilled"` promise. | ||
* Allow the wrapped function (`TFunc`) to be called again | ||
* */ | ||
clear: () => void; | ||
@@ -16,11 +20,9 @@ (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): Promise<ResultValue<TFunc>>; | ||
* const user1 = await getUserOnce(); | ||
* // returns result of `getUser()` | ||
* // returns "fulfilled" `getUser()` promise value | ||
* | ||
* const user2 = await getUserOnce(); | ||
* // `getUser()` not called, previously "fulfilled" value returned | ||
* | ||
* console.log(user1 === user2); // true | ||
*/ | ||
declare function onceAsync<TFunc extends (...args: any[]) => Promise<any>>(fn: TFunc): CachedFn<TFunc>; | ||
declare function onceAsync<TFunc extends (...args: any[]) => Promise<any>>(fn: TFunc): OnceAsyncFn<TFunc>; | ||
export { type CachedFn, onceAsync }; | ||
export { type OnceAsyncFn, onceAsync }; |
@@ -1,2 +0,6 @@ | ||
type CachedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
type OncedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
/** | ||
* Clear the cached result. | ||
* Allow the wrapped function (`TFunc`) to be called again | ||
* */ | ||
clear: () => void; | ||
@@ -17,4 +21,4 @@ (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>; | ||
*/ | ||
declare function once<TFunc extends (...args: any[]) => any>(fn: TFunc): CachedFn<TFunc>; | ||
declare function once<TFunc extends (...args: any[]) => any>(fn: TFunc): OncedFn<TFunc>; | ||
export { type CachedFn, once }; | ||
export { type OncedFn, once }; |
{ | ||
"name": "limit-once", | ||
"description": "Remember the first result of a function call", | ||
"version": "0.10.0", | ||
"author": "Alex Reardon <alexreardon@gmail.com>", | ||
"keywords": [ | ||
"memoize", | ||
"memoization", | ||
"once", | ||
"cache", | ||
"performance" | ||
], | ||
"license": "MIT", | ||
"version": "0.9.0", | ||
"private": false, | ||
"repository": { | ||
@@ -19,4 +9,14 @@ "type": "git", | ||
}, | ||
"sideEffects": false, | ||
"type": "module", | ||
"devDependencies": { | ||
"@arethetypeswrong/cli": "^0.15.3", | ||
"@types/bun": "latest", | ||
"expect-type": "^0.19.0", | ||
"prettier": "^3.2.5", | ||
"rimraf": "^5.0.7", | ||
"tiny-invariant": "^1.3.3", | ||
"tsup": "^8.0.2" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.4.5" | ||
}, | ||
"exports": { | ||
@@ -30,2 +30,6 @@ "types": { | ||
}, | ||
"description": "Remember the first result of a function call", | ||
"engines": { | ||
"bun": ">=1.1.10" | ||
}, | ||
"files": [ | ||
@@ -35,24 +39,22 @@ "/dist", | ||
], | ||
"engines": { | ||
"bun": ">=1.1.8" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@types/bun": "latest", | ||
"esbuild": "^0.21.3", | ||
"prettier": "^3.2.5", | ||
"rimraf": "^5.0.7", | ||
"tiny-invariant": "^1.3.3", | ||
"tsup": "^8.0.2" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.4.5" | ||
}, | ||
"keywords": [ | ||
"memoize", | ||
"memoization", | ||
"once", | ||
"cache", | ||
"performance" | ||
], | ||
"license": "MIT", | ||
"private": false, | ||
"scripts": { | ||
"prepublishOnly": "bun build:dist", | ||
"build:dist": "bun tsup", | ||
"check:all": "bun check:prettier && bun check:typescript", | ||
"build:clean": "rimraf ./dist", | ||
"check:all": "bun check:prettier && bun check:typescript && bun check:dist", | ||
"check:prettier": "prettier --debug-check src/**/*.ts test/**/*.ts", | ||
"check:typescript": "tsc" | ||
} | ||
"check:typescript": "tsc", | ||
"check:dist": "attw --pack . --ignore-rules no-resolution" | ||
}, | ||
"sideEffects": false, | ||
"type": "module" | ||
} |
# limit-once | ||
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/alexreardon/limit-once/check.yml?style=flat-square) | ||
Gives you the ability to ensure a `function` is only called `"once"`, and that that the result of that single `function` call is returned every time. | ||
@@ -4,0 +6,0 @@ |
@@ -1,2 +0,2 @@ | ||
export { once } from './once'; | ||
export { onceAsync } from './once-async'; | ||
export { once, type OncedFn } from './once'; | ||
export { onceAsync, type OnceAsyncFn } from './once-async'; |
@@ -5,3 +5,7 @@ type ResultValue<TFunc extends (this: any, ...args: any[]) => Promise<any>> = Awaited< | ||
export type CachedFn<TFunc extends (this: any, ...args: any[]) => Promise<any>> = { | ||
export type OnceAsyncFn<TFunc extends (this: any, ...args: any[]) => Promise<any>> = { | ||
/** | ||
* Clear the cached `"fulfilled"` promise. | ||
* Allow the wrapped function (`TFunc`) to be called again | ||
* */ | ||
clear: () => void; | ||
@@ -26,12 +30,10 @@ (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): Promise<ResultValue<TFunc>>; | ||
* const user1 = await getUserOnce(); | ||
* // returns result of `getUser()` | ||
* // returns "fulfilled" `getUser()` promise value | ||
* | ||
* 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>>( | ||
fn: TFunc, | ||
): CachedFn<TFunc> { | ||
): OnceAsyncFn<TFunc> { | ||
type Result = ResultValue<TFunc>; | ||
@@ -46,3 +48,3 @@ | ||
...args: Parameters<TFunc> | ||
): ReturnType<CachedFn<TFunc>> { | ||
): ReturnType<OnceAsyncFn<TFunc>> { | ||
if (state.type === 'fulfilled') { | ||
@@ -56,4 +58,5 @@ return state.promise; | ||
let rejectPendingPromise: (() => void) | null; | ||
let rejectPendingPromise: ((reason?: any) => void) | null; | ||
function abort() { | ||
// TODO: should we reject with an error? | ||
rejectPendingPromise?.(); | ||
@@ -60,0 +63,0 @@ } |
@@ -1,2 +0,6 @@ | ||
export type CachedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
export type OncedFn<TFunc extends (this: any, ...args: any[]) => any> = { | ||
/** | ||
* Clear the cached result. | ||
* Allow the wrapped function (`TFunc`) to be called again | ||
* */ | ||
clear: () => void; | ||
@@ -18,3 +22,3 @@ (this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>; | ||
*/ | ||
export function once<TFunc extends (...args: any[]) => any>(fn: TFunc): CachedFn<TFunc> { | ||
export function once<TFunc extends (...args: any[]) => any>(fn: TFunc): OncedFn<TFunc> { | ||
let cache: { value: ReturnType<TFunc> } | null = null; | ||
@@ -21,0 +25,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
21975
383
232
0
7