@@ -3,19 +3,24 @@ import { env } from "node:process";
33import { getModifiedDate } from "./fs.js" ;
44
55
6- const CACHE = new Map < string , { date : Date ; value : any ; } > ( ) ;
6+ interface CacheItem {
7+ date : Date ;
8+ value : any ;
9+ }
10+
11+ const CACHE = new Map < string , CacheItem > ( ) ;
712
8- export function invalidateByModifiedDate ( cacheDate : Date , path : string ) : boolean {
13+ export function invalidateByModifiedDate ( cache : CacheItem , path : string ) : boolean {
914 const modified = getModifiedDate ( path ) ;
10- return ! modified || modified > cacheDate ;
15+ return ! modified || modified > cache . date ;
1116}
1217
13- export function withCache < Result > ( key : string , callback : ( ) => Result , invalidate ?: ( cacheDate : Date , cacheKey : string ) => boolean ) : Result ;
14- export function withCache < Result > ( key : string , callback : ( ) => Promise < Result > , invalidate ?: ( cacheDate : Date , cacheKey : string ) => boolean ) : Promise < Result > ;
15- export function withCache < Result > ( key : string , callback : ( ) => Promise < Result > | Result , invalidate : ( cacheDate : Date , cacheKey : string ) => boolean = invalidateByModifiedDate ) : Promise < Result > | Result {
18+ export function withCache < Result > ( key : string , callback : ( ) => Result , invalidate ?: ( cache : CacheItem , cacheKey : string ) => boolean ) : Result ;
19+ export function withCache < Result > ( key : string , callback : ( ) => Promise < Result > , invalidate ?: ( cache : CacheItem , cacheKey : string ) => boolean ) : Promise < Result > ;
20+ export function withCache < Result > ( key : string , callback : ( ) => Promise < Result > | Result , invalidate : ( cache : CacheItem , cacheKey : string ) => boolean = invalidateByModifiedDate ) : Promise < Result > | Result {
1621 const cached = CACHE . get ( key ) ;
1722
18- if ( env . NODE_ENV !== "test" && cached && ! invalidate ( cached . date , key ) ) {
23+ if ( env . NODE_ENV !== "test" && cached && ! invalidate ( cached , key ) ) {
1924 return cached . value ;
2025 }
2126
0 commit comments