Pass in a Memento instead of a full ExtensionContext

This commit is contained in:
Robert
2023-05-31 12:18:20 +01:00
parent b9c0f2bc14
commit 79dccaa12f
3 changed files with 9 additions and 96 deletions

View File

@@ -75,7 +75,7 @@ export class DistributionManager implements DistributionProvider {
extensionContext,
);
this.updateCheckRateLimiter = new InvocationRateLimiter(
extensionContext,
extensionContext.globalState,
"extensionSpecificDistributionUpdateCheck",
() =>
this.extensionSpecificDistributionManager.checkForUpdatesToDistribution(),

View File

@@ -1,4 +1,4 @@
import { ExtensionContext } from "vscode";
import { Memento } from "./common/memento";
/**
* Provides a utility method to invoke a function only if a minimum time interval has elapsed since
@@ -6,7 +6,7 @@ import { ExtensionContext } from "vscode";
*/
export class InvocationRateLimiter<T> {
constructor(
private readonly extensionContext: ExtensionContext,
private readonly globalState: Memento,
private readonly funcIdentifier: string,
private readonly func: () => Promise<T>,
private readonly createDate: (dateString?: string) => Date = (s) =>
@@ -36,16 +36,14 @@ export class InvocationRateLimiter<T> {
}
private getLastInvocationDate(): Date | undefined {
const maybeDateString: string | undefined =
this.extensionContext.globalState.get(
InvocationRateLimiter._invocationRateLimiterPrefix +
this.funcIdentifier,
);
const maybeDateString: string | undefined = this.globalState.get(
InvocationRateLimiter._invocationRateLimiterPrefix + this.funcIdentifier,
);
return maybeDateString ? this.createDate(maybeDateString) : undefined;
}
private async setLastInvocationDate(date: Date): Promise<void> {
return await this.extensionContext.globalState.update(
return await this.globalState.update(
InvocationRateLimiter._invocationRateLimiterPrefix + this.funcIdentifier,
date,
);

View File

@@ -1,14 +1,4 @@
import {
EnvironmentVariableCollection,
EnvironmentVariableMutator,
Event,
ExtensionContext,
ExtensionMode,
Memento,
SecretStorage,
SecretStorageChangeEvent,
Uri,
} from "vscode";
import { Memento } from "vscode";
import { InvocationRateLimiter } from "../../../src/invocation-rate-limiter";
describe("Invocation rate limiter", () => {
@@ -28,75 +18,13 @@ describe("Invocation rate limiter", () => {
func: () => Promise<T>,
): InvocationRateLimiter<T> {
return new InvocationRateLimiter(
new MockExtensionContext(),
new MockGlobalStorage(),
funcIdentifier,
func,
(s) => createDate(s),
);
}
class MockExtensionContext implements ExtensionContext {
extensionMode: ExtensionMode = 3;
subscriptions: Array<{ dispose(): unknown }> = [];
workspaceState: Memento = new MockMemento();
globalState = new MockGlobalStorage();
extensionPath = "";
asAbsolutePath(_relativePath: string): string {
throw new Error("Method not implemented.");
}
storagePath = "";
globalStoragePath = "";
logPath = "";
extensionUri = Uri.parse("");
environmentVariableCollection = new MockEnvironmentVariableCollection();
secrets = new MockSecretStorage();
storageUri = Uri.parse("");
globalStorageUri = Uri.parse("");
logUri = Uri.parse("");
extension: any;
}
class MockEnvironmentVariableCollection
implements EnvironmentVariableCollection
{
[Symbol.iterator](): Iterator<
[variable: string, mutator: EnvironmentVariableMutator],
any,
undefined
> {
throw new Error("Method not implemented.");
}
persistent = false;
replace(_variable: string, _value: string): void {
throw new Error("Method not implemented.");
}
append(_variable: string, _value: string): void {
throw new Error("Method not implemented.");
}
prepend(_variable: string, _value: string): void {
throw new Error("Method not implemented.");
}
get(_variable: string): EnvironmentVariableMutator | undefined {
throw new Error("Method not implemented.");
}
forEach(
_callback: (
variable: string,
mutator: EnvironmentVariableMutator,
collection: EnvironmentVariableCollection,
) => any,
_thisArg?: any,
): void {
throw new Error("Method not implemented.");
}
delete(_variable: string): void {
throw new Error("Method not implemented.");
}
clear(): void {
throw new Error("Method not implemented.");
}
}
class MockMemento implements Memento {
keys(): readonly string[] {
throw new Error("Method not implemented.");
@@ -132,19 +60,6 @@ describe("Invocation rate limiter", () => {
}
}
class MockSecretStorage implements SecretStorage {
get(_key: string): Thenable<string | undefined> {
throw new Error("Method not implemented.");
}
store(_key: string, _value: string): Thenable<void> {
throw new Error("Method not implemented.");
}
delete(_key: string): Thenable<void> {
throw new Error("Method not implemented.");
}
onDidChange!: Event<SecretStorageChangeEvent>;
}
it("initially invokes function", async () => {
let numTimesFuncCalled = 0;
const invocationRateLimiter = createInvocationRateLimiter(