Skip to content

Commit

Permalink
refactor(cache): split a getCached function out (#360)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 authored Jun 24, 2022
1 parent b258497 commit 229dea5
Showing 1 changed file with 9 additions and 37 deletions.
46 changes: 9 additions & 37 deletions src/tscache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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"));

Expand All @@ -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;
Expand Down

0 comments on commit 229dea5

Please sign in to comment.