Skip to content

Commit

Permalink
Merge pull request #3 from leosuncin/fix/amend-typescript-definition
Browse files Browse the repository at this point in the history
fix: amend typescript definition
  • Loading branch information
kartikk221 authored May 18, 2022
2 parents 6a1f3e3 + 6857c45 commit b3f666a
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions types/index.d.ts
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>>;
}

0 comments on commit b3f666a

Please sign in to comment.