Skip to content

Commit 79dccaa

Browse files
Pass in a Memento instead of a full ExtensionContext
1 parent b9c0f2b commit 79dccaa

File tree

3 files changed

+9
-96
lines changed

3 files changed

+9
-96
lines changed

extensions/ql-vscode/src/codeql-cli/distribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class DistributionManager implements DistributionProvider {
7575
extensionContext,
7676
);
7777
this.updateCheckRateLimiter = new InvocationRateLimiter(
78-
extensionContext,
78+
extensionContext.globalState,
7979
"extensionSpecificDistributionUpdateCheck",
8080
() =>
8181
this.extensionSpecificDistributionManager.checkForUpdatesToDistribution(),

extensions/ql-vscode/src/invocation-rate-limiter.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ExtensionContext } from "vscode";
1+
import { Memento } from "./common/memento";
22

33
/**
44
* Provides a utility method to invoke a function only if a minimum time interval has elapsed since
55
* the last invocation of that function.
66
*/
77
export class InvocationRateLimiter<T> {
88
constructor(
9-
private readonly extensionContext: ExtensionContext,
9+
private readonly globalState: Memento,
1010
private readonly funcIdentifier: string,
1111
private readonly func: () => Promise<T>,
1212
private readonly createDate: (dateString?: string) => Date = (s) =>
@@ -36,16 +36,14 @@ export class InvocationRateLimiter<T> {
3636
}
3737

3838
private getLastInvocationDate(): Date | undefined {
39-
const maybeDateString: string | undefined =
40-
this.extensionContext.globalState.get(
41-
InvocationRateLimiter._invocationRateLimiterPrefix +
42-
this.funcIdentifier,
43-
);
39+
const maybeDateString: string | undefined = this.globalState.get(
40+
InvocationRateLimiter._invocationRateLimiterPrefix + this.funcIdentifier,
41+
);
4442
return maybeDateString ? this.createDate(maybeDateString) : undefined;
4543
}
4644

4745
private async setLastInvocationDate(date: Date): Promise<void> {
48-
return await this.extensionContext.globalState.update(
46+
return await this.globalState.update(
4947
InvocationRateLimiter._invocationRateLimiterPrefix + this.funcIdentifier,
5048
date,
5149
);

extensions/ql-vscode/test/vscode-tests/no-workspace/invocation-rate-limiter.test.ts

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
import {
2-
EnvironmentVariableCollection,
3-
EnvironmentVariableMutator,
4-
Event,
5-
ExtensionContext,
6-
ExtensionMode,
7-
Memento,
8-
SecretStorage,
9-
SecretStorageChangeEvent,
10-
Uri,
11-
} from "vscode";
1+
import { Memento } from "vscode";
122
import { InvocationRateLimiter } from "../../../src/invocation-rate-limiter";
133

144
describe("Invocation rate limiter", () => {
@@ -28,75 +18,13 @@ describe("Invocation rate limiter", () => {
2818
func: () => Promise<T>,
2919
): InvocationRateLimiter<T> {
3020
return new InvocationRateLimiter(
31-
new MockExtensionContext(),
21+
new MockGlobalStorage(),
3222
funcIdentifier,
3323
func,
3424
(s) => createDate(s),
3525
);
3626
}
3727

38-
class MockExtensionContext implements ExtensionContext {
39-
extensionMode: ExtensionMode = 3;
40-
subscriptions: Array<{ dispose(): unknown }> = [];
41-
workspaceState: Memento = new MockMemento();
42-
globalState = new MockGlobalStorage();
43-
extensionPath = "";
44-
asAbsolutePath(_relativePath: string): string {
45-
throw new Error("Method not implemented.");
46-
}
47-
storagePath = "";
48-
globalStoragePath = "";
49-
logPath = "";
50-
extensionUri = Uri.parse("");
51-
environmentVariableCollection = new MockEnvironmentVariableCollection();
52-
secrets = new MockSecretStorage();
53-
storageUri = Uri.parse("");
54-
globalStorageUri = Uri.parse("");
55-
logUri = Uri.parse("");
56-
extension: any;
57-
}
58-
59-
class MockEnvironmentVariableCollection
60-
implements EnvironmentVariableCollection
61-
{
62-
[Symbol.iterator](): Iterator<
63-
[variable: string, mutator: EnvironmentVariableMutator],
64-
any,
65-
undefined
66-
> {
67-
throw new Error("Method not implemented.");
68-
}
69-
persistent = false;
70-
replace(_variable: string, _value: string): void {
71-
throw new Error("Method not implemented.");
72-
}
73-
append(_variable: string, _value: string): void {
74-
throw new Error("Method not implemented.");
75-
}
76-
prepend(_variable: string, _value: string): void {
77-
throw new Error("Method not implemented.");
78-
}
79-
get(_variable: string): EnvironmentVariableMutator | undefined {
80-
throw new Error("Method not implemented.");
81-
}
82-
forEach(
83-
_callback: (
84-
variable: string,
85-
mutator: EnvironmentVariableMutator,
86-
collection: EnvironmentVariableCollection,
87-
) => any,
88-
_thisArg?: any,
89-
): void {
90-
throw new Error("Method not implemented.");
91-
}
92-
delete(_variable: string): void {
93-
throw new Error("Method not implemented.");
94-
}
95-
clear(): void {
96-
throw new Error("Method not implemented.");
97-
}
98-
}
99-
10028
class MockMemento implements Memento {
10129
keys(): readonly string[] {
10230
throw new Error("Method not implemented.");
@@ -132,19 +60,6 @@ describe("Invocation rate limiter", () => {
13260
}
13361
}
13462

135-
class MockSecretStorage implements SecretStorage {
136-
get(_key: string): Thenable<string | undefined> {
137-
throw new Error("Method not implemented.");
138-
}
139-
store(_key: string, _value: string): Thenable<void> {
140-
throw new Error("Method not implemented.");
141-
}
142-
delete(_key: string): Thenable<void> {
143-
throw new Error("Method not implemented.");
144-
}
145-
onDidChange!: Event<SecretStorageChangeEvent>;
146-
}
147-
14863
it("initially invokes function", async () => {
14964
let numTimesFuncCalled = 0;
15065
const invocationRateLimiter = createInvocationRateLimiter(

0 commit comments

Comments
 (0)