Skip to content

Commit 98c96b0

Browse files
Give CachedOperation a type parameter for its args
1 parent f4727fe commit 98c96b0

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

extensions/ql-vscode/src/language-support/contextual/cached-operation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { asError } from "../../common/helpers-pure";
22

33
/**
4-
* A cached mapping from strings to value of type U.
4+
* A cached mapping from args of type [string, S] to a value of type Promise<U>.
55
*/
6-
export class CachedOperation<U> {
7-
private readonly operation: (t: string, ...args: any[]) => Promise<U>;
6+
export class CachedOperation<S extends unknown[], U> {
7+
private readonly operation: (t: string, ...args: S) => Promise<U>;
88
private readonly cached: Map<string, U>;
99
private readonly lru: string[];
1010
private readonly inProgressCallbacks: Map<
@@ -13,7 +13,7 @@ export class CachedOperation<U> {
1313
>;
1414

1515
constructor(
16-
operation: (t: string, ...args: any[]) => Promise<U>,
16+
operation: (t: string, ...args: S) => Promise<U>,
1717
private cacheSize = 100,
1818
) {
1919
this.operation = operation;
@@ -25,7 +25,7 @@ export class CachedOperation<U> {
2525
this.cached = new Map<string, U>();
2626
}
2727

28-
async get(t: string, ...args: any[]): Promise<U> {
28+
async get(t: string, ...args: S): Promise<U> {
2929
// Try and retrieve from the cache
3030
const fromCache = this.cached.get(t);
3131
if (fromCache !== undefined) {

extensions/ql-vscode/src/language-support/contextual/template-provider.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ import { MultiCancellationToken } from "../../common/vscode/multi-cancellation-t
5050
*/
5151

5252
export class TemplateQueryDefinitionProvider implements DefinitionProvider {
53-
private cache: CachedOperation<LocationLink[]>;
53+
private cache: CachedOperation<[CancellationToken], LocationLink[]>;
5454

5555
constructor(
5656
private cli: CodeQLCliServer,
5757
private qs: QueryRunner,
5858
private dbm: DatabaseManager,
5959
private queryStorageDir: string,
6060
) {
61-
this.cache = new CachedOperation<LocationLink[]>(
62-
this.getDefinitions.bind(this),
63-
);
61+
this.cache = new CachedOperation(this.getDefinitions.bind(this));
6462
}
6563

6664
async provideDefinition(
@@ -112,17 +110,15 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
112110
* or from a selected identifier.
113111
*/
114112
export class TemplateQueryReferenceProvider implements ReferenceProvider {
115-
private cache: CachedOperation<FullLocationLink[]>;
113+
private cache: CachedOperation<[CancellationToken], FullLocationLink[]>;
116114

117115
constructor(
118116
private cli: CodeQLCliServer,
119117
private qs: QueryRunner,
120118
private dbm: DatabaseManager,
121119
private queryStorageDir: string,
122120
) {
123-
this.cache = new CachedOperation<FullLocationLink[]>(
124-
this.getReferences.bind(this),
125-
);
121+
this.cache = new CachedOperation(this.getReferences.bind(this));
126122
}
127123

128124
async provideReferences(
@@ -185,17 +181,18 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
185181
* source-language files.
186182
*/
187183
export class TemplatePrintAstProvider {
188-
private cache: CachedOperation<CoreCompletedQuery>;
184+
private cache: CachedOperation<
185+
[ProgressCallback, CancellationToken],
186+
CoreCompletedQuery
187+
>;
189188

190189
constructor(
191190
private cli: CodeQLCliServer,
192191
private qs: QueryRunner,
193192
private dbm: DatabaseManager,
194193
private queryStorageDir: string,
195194
) {
196-
this.cache = new CachedOperation<CoreCompletedQuery>(
197-
this.getAst.bind(this),
198-
);
195+
this.cache = new CachedOperation(this.getAst.bind(this));
199196
}
200197

201198
async provideAst(
@@ -283,15 +280,16 @@ export class TemplatePrintAstProvider {
283280
* source-language files.
284281
*/
285282
export class TemplatePrintCfgProvider {
286-
private cache: CachedOperation<[Uri, Record<string, string>] | undefined>;
283+
private cache: CachedOperation<
284+
[number, number],
285+
[Uri, Record<string, string>]
286+
>;
287287

288288
constructor(
289289
private cli: CodeQLCliServer,
290290
private dbm: DatabaseManager,
291291
) {
292-
this.cache = new CachedOperation<[Uri, Record<string, string>] | undefined>(
293-
this.getCfgUri.bind(this),
294-
);
292+
this.cache = new CachedOperation(this.getCfgUri.bind(this));
295293
}
296294

297295
async provideCfgUri(

0 commit comments

Comments
 (0)