Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 9 additions & 37 deletions src/tscache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,37 +200,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 @@ -268,18 +239,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 @@ -295,7 +267,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