Skip to content

Commit bebe750

Browse files
committed
Simplify CacheConsultant: replace trampoline with default arrow methods
1 parent 0987421 commit bebe750

1 file changed

Lines changed: 11 additions & 18 deletions

File tree

packages/knip/src/CacheConsultant.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,21 @@ import { version } from './version.ts';
66
const dummyFileDescriptor: FileDescriptor<any> = { key: '', changed: true, notFound: true };
77

88
export class CacheConsultant<T> {
9-
private isEnabled: boolean;
10-
private cache: undefined | FileEntryCache<T>;
9+
private cache: FileEntryCache<T> | undefined;
10+
getFileDescriptor: (filePath: string) => FileDescriptor<T> = () => dummyFileDescriptor;
11+
reconcile: () => void = () => {};
1112

1213
constructor(name: string, options: MainOptions) {
13-
this.isEnabled = options.isCache;
14-
if (this.isEnabled) {
15-
const cacheName = `${name.replace(/[^a-z0-9]/g, '-').replace(/-*$/, '')}-${options.isProduction ? '-prod' : ''}-${version}`;
16-
this.cache = new FileEntryCache(cacheName, options.cacheLocation);
17-
this.reconcile = timerify(this.cache.reconcile).bind(this.cache);
18-
this.getFileDescriptor = timerify(this.cache.getFileDescriptor).bind(this.cache);
19-
}
14+
if (!options.isCache) return;
15+
const cacheName = `${name.replace(/[^a-z0-9]/g, '-').replace(/-*$/, '')}-${options.isProduction ? '-prod' : ''}-${version}`;
16+
this.cache = new FileEntryCache(cacheName, options.cacheLocation);
17+
this.getFileDescriptor = timerify(this.cache.getFileDescriptor.bind(this.cache));
18+
this.reconcile = timerify(this.cache.reconcile.bind(this.cache));
2019
}
21-
public getFileDescriptor(filePath: string): FileDescriptor<T> {
22-
if (this.isEnabled && this.cache) return this.cache.getFileDescriptor(filePath);
23-
return dummyFileDescriptor;
24-
}
25-
public getCachedFile(filePath: string): T | undefined {
26-
if (!this.isEnabled || !this.cache) return undefined;
20+
21+
getCachedFile(filePath: string): T | undefined {
22+
if (!this.cache) return undefined;
2723
const fd = this.cache.getFileDescriptor(filePath);
2824
return !fd.changed ? fd.meta?.data : undefined;
2925
}
30-
public reconcile() {
31-
if (this.isEnabled && this.cache) this.cache.reconcile();
32-
}
3326
}

0 commit comments

Comments
 (0)