async-cache-dedupe
Advanced tools
Comparing version
{ | ||
"name": "async-cache-dedupe", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "An async deduping cache", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "standard | snazzy && tap test/*test.js", | ||
"test": "standard | snazzy && tap test/*test.js && tsd", | ||
"lint:fix": "standard --fix", | ||
@@ -36,3 +37,4 @@ "redis": "docker run --rm -p 6379:6379 redis redis-server" | ||
"standard": "^17.0.0", | ||
"tap": "^16.3.0" | ||
"tap": "^16.3.0", | ||
"tsd": "^0.25.0" | ||
}, | ||
@@ -39,0 +41,0 @@ "dependencies": { |
@@ -282,2 +282,32 @@ # async-cache-dedupe | ||
## TypeScript | ||
This module provides a basic type definition for TypeScript. | ||
As the library does some meta-programming and magic stuff behind the scenes, your compiler could yell at you when defining functions using the `define` property. | ||
To avoid this, our suggestion is to use a [`Union Type`](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html) in combination with the given `Cache` type as below: | ||
```ts | ||
import { createCache } from 'async-cache-dedupe' | ||
const fetchSomething = async (k: any) => { | ||
console.log("query", k); | ||
return { k }; | ||
}; | ||
export type CachedFunctions = { | ||
fetchSomething: typeof fetchSomething; | ||
}; // <--- This is where you define all the functions you want to cache | ||
const cache = createCache({ | ||
ttl: 5, // seconds | ||
storage: { type: 'memory' }, | ||
}) as Cache & CachedFunctions // <--- This is where you tell the compiler to consider both the Cache type and your custom type | ||
cache.define('fetchSomething', fetchSomething) | ||
const p1 = cache.fetchSomething(42) // <--- TypeScript doesn't argue anymore here! | ||
``` | ||
--- | ||
## Maintainers | ||
@@ -284,0 +314,0 @@ |
154177
3.32%27
8%3759
3.73%327
10.1%7
16.67%