Give CachedOperation a type parameter for its args

This commit is contained in:
Robert
2024-02-08 17:34:02 +00:00
parent f4727fe7b5
commit 98c96b09ee
2 changed files with 19 additions and 21 deletions

View File

@@ -1,10 +1,10 @@
import { asError } from "../../common/helpers-pure";
/**
* A cached mapping from strings to value of type U.
* A cached mapping from args of type [string, S] to a value of type Promise<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<
@@ -13,7 +13,7 @@ export class CachedOperation<U> {
>;
constructor(
operation: (t: string, ...args: any[]) => Promise<U>,
operation: (t: string, ...args: S) => Promise<U>,
private cacheSize = 100,
) {
this.operation = operation;
@@ -25,7 +25,7 @@ export class CachedOperation<U> {
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) {

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(