1
1
import debug from 'debug'
2
2
3
- import type { ImportSettings , PluginSettings } from '../types.js'
3
+ import type { NormalizedCacheSettings , PluginSettings } from '../types.js'
4
4
5
5
const log = debug ( 'eslint-plugin-import-x:utils:ModuleCache' )
6
6
7
- export type CacheKey = unknown
8
-
9
7
export interface CacheObject {
10
8
result : unknown
11
9
lastSeen : ReturnType < typeof process . hrtime >
12
10
}
13
11
14
12
export class ModuleCache {
15
- constructor ( public map : Map < CacheKey , CacheObject > = new Map ( ) ) { }
13
+ constructor ( public map : Map < string , CacheObject > = new Map ( ) ) { }
16
14
17
- set ( cacheKey : CacheKey , result : unknown ) {
15
+ set ( cacheKey : string , result : unknown ) {
18
16
this . map . set ( cacheKey , {
19
17
result,
20
18
lastSeen : process . hrtime ( ) ,
@@ -23,13 +21,12 @@ export class ModuleCache {
23
21
return result
24
22
}
25
23
26
- get < T > ( cacheKey : CacheKey , settings : ImportSettings [ 'cache' ] ) : T | undefined {
27
- if ( this . map . has ( cacheKey ) ) {
28
- const f = this . map . get ( cacheKey )
24
+ get < T > ( cacheKey : string , settings : NormalizedCacheSettings ) : T | undefined {
25
+ const cache = this . map . get ( cacheKey )
26
+ if ( cache ) {
29
27
// check freshness
30
- // @ts -expect-error TS can't narrow properly from `has` and `get`
31
- if ( process . hrtime ( f . lastSeen ) [ 0 ] < settings . lifetime ) {
32
- return f ! . result as T
28
+ if ( process . hrtime ( cache . lastSeen ) [ 0 ] < settings . lifetime ) {
29
+ return cache . result as T
33
30
}
34
31
} else {
35
32
log ( 'cache miss for' , cacheKey )
@@ -51,8 +48,6 @@ export class ModuleCache {
51
48
cacheSettings . lifetime = Number . POSITIVE_INFINITY
52
49
}
53
50
54
- return cacheSettings as ImportSettings [ 'cache' ] & {
55
- lifetime : number
56
- }
51
+ return cacheSettings as NormalizedCacheSettings
57
52
}
58
53
}
0 commit comments