-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from leosuncin/fix/amend-typescript-definition
fix: amend typescript definition
- Loading branch information
Showing
1 changed file
with
21 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,68 @@ | ||
type LookupHandler<T extends any> = () => T | Promise<T> | ||
type LookupHandler<T extends unknown> = () => T | Promise<T>; | ||
type SupportedTypes = string | number | boolean; | ||
type SupportedArgumentTypes = SupportedTypes | SupportedTypes[]; | ||
|
||
interface ValueRecord { | ||
value: InstanceType<typeof CachedLookup>, | ||
updated_at: number | ||
interface ValueRecord<T = unknown> { | ||
value: T; | ||
updated_at: number; | ||
} | ||
|
||
export default class CachedLookup<T extends any> { | ||
export default class CachedLookup<T extends unknown> { | ||
/** | ||
* Creates a new CachedLookup instance with the specified lookup function. | ||
* The lookup function can be both synchronous or asynchronous. | ||
* | ||
* @param {function(...(SupportedArgumentTypes|Array<SupportedArgumentTypes>)):T} lookup | ||
* @param {LookupHandler<T>} lookup | ||
*/ | ||
constructor(lookup: LookupHandler<T>) | ||
constructor(lookup: LookupHandler<T>); | ||
|
||
/** | ||
* Returns a cached value that is up to max_age milliseconds old for the provided set of arguments. | ||
* Falls back to a fresh value if the cache value is older than max_age. | ||
* | ||
* | ||
* @param {Number} max_age In Milliseconds | ||
* @param {...(SupportedArgumentTypes)} args | ||
* @param {Array<SupportedTypes>} args | ||
* @returns {Promise<T>} | ||
*/ | ||
cached(max_age: number, ...args: SupportedArgumentTypes[]): Promise<T>; | ||
cached(max_age: number, ...args: SupportedTypes[]): Promise<T>; | ||
|
||
/** | ||
* Returns a fresh value and automatically updates the internal cache with this value for the provided set of arguments. | ||
* | ||
* @param {...(SupportedArgumentTypes)} args | ||
* @param {Array<SupportedTypes>} args | ||
* @returns {Promise<T>} | ||
*/ | ||
fresh(...args: SupportedArgumentTypes[]): Promise<T>; | ||
fresh(...args: SupportedTypes[]): Promise<T>; | ||
|
||
/** | ||
* Expires the cached value for the provided set of arguments. | ||
* | ||
* @param {...(SupportedArgumentTypes)} args | ||
* @param {Array<SupportedTypes>} args | ||
* @returns {Boolean} True if the cache value was expired, false otherwise. | ||
*/ | ||
expire(...args: SupportedArgumentTypes[]): boolean; | ||
expire(...args: SupportedTypes[]): boolean; | ||
|
||
/** | ||
* Returns whether a fresh value is currently being resolved for the provided set of arguments. | ||
* | ||
* @param {...(SupportedArgumentTypes)} args | ||
* @param {Array<SupportedTypes>} args | ||
* @returns {Boolean} | ||
*/ | ||
in_flight(...args: SupportedArgumentTypes[]): boolean; | ||
in_flight(...args: SupportedTypes[]): boolean; | ||
|
||
/** | ||
* Returns the last value update timestamp in milliseconds for the provided set of arguments. | ||
* | ||
* @param {...(SupportedArgumentTypes)} args | ||
* @param {Array<SupportedTypes>} args | ||
* @returns {Boolean} | ||
*/ | ||
updated_at(...args: SupportedArgumentTypes[]): number | void; | ||
updated_at(...args: SupportedTypes[]): number | void; | ||
|
||
/* CachedLookup Getters */ | ||
|
||
/** | ||
* Returns the underlying cache object which contains the cached values identified by their serialized arguments. | ||
* | ||
* @returns {Map<String, ValueRecord>} | ||
* @returns {Map<String, ValueRecord<T>>} | ||
*/ | ||
get cache(): Map<string, ValueRecord>; | ||
} | ||
get cache(): Map<string, ValueRecord<T>>; | ||
} |