Skip to content

Commit

Permalink
chore: improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
jmendiara committed Dec 20, 2020
1 parent 3c75924 commit f27f9bb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lru-pcache",
"version": "1.1.0",
"version": "1.1.1",
"description": "LRU cache for promises",
"author": "Javier Mendiara Cañardo <[email protected]>",
"license": "Apache-2.0",
Expand Down
37 changes: 37 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,40 @@ export class Cache<K, V = unknown> {
return this.lru.prune();
}
}

export interface Cache<K, V = unknown> {
/**
* Gets a value from the cache. When an optional getter method is provided,
* it will be called when there is a cache miss to get the value and store
* it in the cache.
* When the getter method throws/rejects, it will be propagated down the chain
*
* ```js
* // Old Way (Sync code)
* let value = cache.get(key);
* if (!value) {
* value = calculateValue();
* cache.put(key, value);
* }
* return value;
* ```
* that becomes
* ```js
* return cache.get(key, calculateValue);
* ```
*
* @param key the cache entry key
* @param getter the function to call when a value is not found
* in the cache. Return promise or a discrete value, that will be
* stored in the cache for that key
* @returns the cache entry
*/
get(key: K, getter?: () => Promise<V> | V, maxAge?: number): Promise<V>;
/**
* Gets a value from the cache.
*
* @param key the cache entry key
* @returns the cache entry
*/
get(key: K): Promise<V | undefined>;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"noImplicitReturns": true,
"noImplicitThis": true,
"outDir": "./lib",
"removeComments": true,
"removeComments": false,
"rootDir": "./src",
"sourceMap": true,
"strictNullChecks": true,
Expand Down

0 comments on commit f27f9bb

Please sign in to comment.