From 229dea56da93217e57dcce9894c166347bdac048 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Fri, 24 Jun 2022 12:36:19 -0400 Subject: [PATCH] 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 --- src/tscache.ts | 46 +++++++++------------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/src/tscache.ts b/src/tscache.ts index a9724d4..a6df796 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -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, id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[] + { + return this.getCached(cache, id, snapshot, true, () => convertDiagnostic(type, check())); + } + + private getCached(cache: ICache, 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;