Merge pull request #3342 from github/robertbrignull/cached_operation

Give more types to CachedOperation to avoid use of any
This commit is contained in:
Robert
2024-02-13 10:48:31 +00:00
committed by GitHub
2 changed files with 25 additions and 25 deletions

View File

@@ -1,29 +1,31 @@
import { asError } from "../../common/helpers-pure";
/**
* A cached mapping from strings to value of type U.
* A cached mapping from strings to a value of type U.
*/
export class CachedOperation<U> {
private readonly operation: (t: string, ...args: any[]) => Promise<U>;
export class CachedOperation<S extends unknown[], U> {
private readonly operation: (t: string, ...args: S) => Promise<U>;
private readonly cached: Map<string, U>;
private readonly lru: string[];
private readonly inProgressCallbacks: Map<
string,
Array<[(u: U) => void, (reason?: any) => void]>
Array<[(u: U) => void, (reason?: Error) => void]>
>;
constructor(
operation: (t: string, ...args: any[]) => Promise<U>,
operation: (t: string, ...args: S) => Promise<U>,
private cacheSize = 100,
) {
this.operation = operation;
this.lru = [];
this.inProgressCallbacks = new Map<
string,
Array<[(u: U) => void, (reason?: any) => void]>
Array<[(u: U) => void, (reason?: Error) => void]>
>();
this.cached = new Map<string, U>();
}
async get(t: string, ...args: any[]): Promise<U> {
async get(t: string, ...args: S): Promise<U> {
// Try and retrieve from the cache
const fromCache = this.cached.get(t);
if (fromCache !== undefined) {
@@ -46,7 +48,7 @@ export class CachedOperation<U> {
}
// Otherwise compute the new value, but leave a callback to allow sharing work
const callbacks: Array<[(u: U) => void, (reason?: any) => void]> = [];
const callbacks: Array<[(u: U) => void, (reason?: Error) => void]> = [];
this.inProgressCallbacks.set(t, callbacks);
try {
const result = await this.operation(t, ...args);
@@ -61,7 +63,7 @@ export class CachedOperation<U> {
return result;
} catch (e) {
// Rethrow error on all callbacks
callbacks.forEach((f) => f[1](e));
callbacks.forEach((f) => f[1](asError(e)));
throw e;
} finally {
this.inProgressCallbacks.delete(t);

View File

@@ -50,7 +50,7 @@ import { MultiCancellationToken } from "../../common/vscode/multi-cancellation-t
*/
export class TemplateQueryDefinitionProvider implements DefinitionProvider {
private cache: CachedOperation<LocationLink[]>;
private cache: CachedOperation<[CancellationToken], LocationLink[]>;
constructor(
private cli: CodeQLCliServer,
@@ -58,9 +58,7 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
private dbm: DatabaseManager,
private queryStorageDir: string,
) {
this.cache = new CachedOperation<LocationLink[]>(
this.getDefinitions.bind(this),
);
this.cache = new CachedOperation(this.getDefinitions.bind(this));
}
async provideDefinition(
@@ -112,7 +110,7 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
* or from a selected identifier.
*/
export class TemplateQueryReferenceProvider implements ReferenceProvider {
private cache: CachedOperation<FullLocationLink[]>;
private cache: CachedOperation<[CancellationToken], FullLocationLink[]>;
constructor(
private cli: CodeQLCliServer,
@@ -120,9 +118,7 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
private dbm: DatabaseManager,
private queryStorageDir: string,
) {
this.cache = new CachedOperation<FullLocationLink[]>(
this.getReferences.bind(this),
);
this.cache = new CachedOperation(this.getReferences.bind(this));
}
async provideReferences(
@@ -185,7 +181,10 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
* source-language files.
*/
export class TemplatePrintAstProvider {
private cache: CachedOperation<CoreCompletedQuery>;
private cache: CachedOperation<
[ProgressCallback, CancellationToken],
CoreCompletedQuery
>;
constructor(
private cli: CodeQLCliServer,
@@ -193,9 +192,7 @@ export class TemplatePrintAstProvider {
private dbm: DatabaseManager,
private queryStorageDir: string,
) {
this.cache = new CachedOperation<CoreCompletedQuery>(
this.getAst.bind(this),
);
this.cache = new CachedOperation(this.getAst.bind(this));
}
async provideAst(
@@ -283,15 +280,16 @@ export class TemplatePrintAstProvider {
* source-language files.
*/
export class TemplatePrintCfgProvider {
private cache: CachedOperation<[Uri, Record<string, string>] | undefined>;
private cache: CachedOperation<
[number, number],
[Uri, Record<string, string>]
>;
constructor(
private cli: CodeQLCliServer,
private dbm: DatabaseManager,
) {
this.cache = new CachedOperation<[Uri, Record<string, string>] | undefined>(
this.getCfgUri.bind(this),
);
this.cache = new CachedOperation(this.getCfgUri.bind(this));
}
async provideCfgUri(