refactor(cache): split a getCached function out (#360)

- basically, refactor the `getDiagnostics` a tiny bit to also handle what the `getCompiled` function needs
  - then just call `getCached` from both instead
  - `getDiagnostics` and `getCompiled` were near identical except for the cache they used and `checkImports`, which are both parameters now
    - `getCompiled` also had a logging statement (called in both `noCache` and w/ cache branches), which was moved to before the call to `getCompiled` instead
    - the `type` param was composed into another function in `getDiagnostics` before the call too `getCached` now
  - this simplifies all the cache code to one main function now

- remove the `markAsDirty` call under `noCache`, as well, "dirty" has no meaning for `noCache` anyway
  - this simplifies that one `if` statement now too
This commit is contained in:
Anton Gilgur 2022-06-24 12:36:19 -04:00 committed by GitHub
parent b2584971da
commit 229dea56da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -209,37 +209,8 @@ export class TsCache
public getCompiled(id: string, snapshot: tsTypes.IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined
{
if (this.noCache)
{
this.context.info(`${blue("transpiling")} '${id}'`);
this.markAsDirty(id);
return transform();
}
const hash = this.createHash(id, snapshot);
this.context.info(`${blue("transpiling")} '${id}'`);
this.context.debug(` cache: '${this.codeCache.path(hash)}'`);
if (this.codeCache.exists(hash) && !this.isDirty(id, false))
{
this.context.debug(green(" cache hit"));
const data = this.codeCache.read(hash);
if (data)
{
this.codeCache.write(hash, data);
return data;
}
else
this.context.warn(yellow(" cache broken, discarding"));
}
this.context.debug(yellow(" cache miss"));
const transformedData = transform();
this.codeCache.write(hash, transformedData);
this.markAsDirty(id);
return transformedData;
return this.getCached(this.codeCache, id, snapshot, false, transform);
}
public getSyntacticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]
@ -277,18 +248,19 @@ export class TsCache
}
private getDiagnostics(type: string, cache: ICache<IDiagnostics[]>, id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]
{
return this.getCached(cache, id, snapshot, true, () => convertDiagnostic(type, check()));
}
private getCached<CacheType>(cache: ICache<CacheType>, id: string, snapshot: tsTypes.IScriptSnapshot, checkImports: boolean, convert: () => CacheType): CacheType
{
if (this.noCache)
{
this.markAsDirty(id);
return convertDiagnostic(type, check());
}
return convert();
const hash = this.createHash(id, snapshot);
this.context.debug(` cache: '${cache.path(hash)}'`);
if (cache.exists(hash) && !this.isDirty(id, true))
if (cache.exists(hash) && !this.isDirty(id, checkImports))
{
this.context.debug(green(" cache hit"));
@ -304,7 +276,7 @@ export class TsCache
this.context.debug(yellow(" cache miss"));
const convertedData = convertDiagnostic(type, check());
const convertedData = convert();
cache.write(hash, convertedData);
this.markAsDirty(id);
return convertedData;