Pass in a Memento instead of a full ExtensionContext
This commit is contained in:
@@ -75,7 +75,7 @@ export class DistributionManager implements DistributionProvider {
|
|||||||
extensionContext,
|
extensionContext,
|
||||||
);
|
);
|
||||||
this.updateCheckRateLimiter = new InvocationRateLimiter(
|
this.updateCheckRateLimiter = new InvocationRateLimiter(
|
||||||
extensionContext,
|
extensionContext.globalState,
|
||||||
"extensionSpecificDistributionUpdateCheck",
|
"extensionSpecificDistributionUpdateCheck",
|
||||||
() =>
|
() =>
|
||||||
this.extensionSpecificDistributionManager.checkForUpdatesToDistribution(),
|
this.extensionSpecificDistributionManager.checkForUpdatesToDistribution(),
|
||||||
|
|||||||
@@ -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
|
* 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> {
|
export class InvocationRateLimiter<T> {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly extensionContext: ExtensionContext,
|
private readonly globalState: Memento,
|
||||||
private readonly funcIdentifier: string,
|
private readonly funcIdentifier: string,
|
||||||
private readonly func: () => Promise<T>,
|
private readonly func: () => Promise<T>,
|
||||||
private readonly createDate: (dateString?: string) => Date = (s) =>
|
private readonly createDate: (dateString?: string) => Date = (s) =>
|
||||||
@@ -36,16 +36,14 @@ export class InvocationRateLimiter<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getLastInvocationDate(): Date | undefined {
|
private getLastInvocationDate(): Date | undefined {
|
||||||
const maybeDateString: string | undefined =
|
const maybeDateString: string | undefined = this.globalState.get(
|
||||||
this.extensionContext.globalState.get(
|
InvocationRateLimiter._invocationRateLimiterPrefix + this.funcIdentifier,
|
||||||
InvocationRateLimiter._invocationRateLimiterPrefix +
|
);
|
||||||
this.funcIdentifier,
|
|
||||||
);
|
|
||||||
return maybeDateString ? this.createDate(maybeDateString) : undefined;
|
return maybeDateString ? this.createDate(maybeDateString) : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async setLastInvocationDate(date: Date): Promise<void> {
|
private async setLastInvocationDate(date: Date): Promise<void> {
|
||||||
return await this.extensionContext.globalState.update(
|
return await this.globalState.update(
|
||||||
InvocationRateLimiter._invocationRateLimiterPrefix + this.funcIdentifier,
|
InvocationRateLimiter._invocationRateLimiterPrefix + this.funcIdentifier,
|
||||||
date,
|
date,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,14 +1,4 @@
|
|||||||
import {
|
import { Memento } from "vscode";
|
||||||
EnvironmentVariableCollection,
|
|
||||||
EnvironmentVariableMutator,
|
|
||||||
Event,
|
|
||||||
ExtensionContext,
|
|
||||||
ExtensionMode,
|
|
||||||
Memento,
|
|
||||||
SecretStorage,
|
|
||||||
SecretStorageChangeEvent,
|
|
||||||
Uri,
|
|
||||||
} from "vscode";
|
|
||||||
import { InvocationRateLimiter } from "../../../src/invocation-rate-limiter";
|
import { InvocationRateLimiter } from "../../../src/invocation-rate-limiter";
|
||||||
|
|
||||||
describe("Invocation rate limiter", () => {
|
describe("Invocation rate limiter", () => {
|
||||||
@@ -28,75 +18,13 @@ describe("Invocation rate limiter", () => {
|
|||||||
func: () => Promise<T>,
|
func: () => Promise<T>,
|
||||||
): InvocationRateLimiter<T> {
|
): InvocationRateLimiter<T> {
|
||||||
return new InvocationRateLimiter(
|
return new InvocationRateLimiter(
|
||||||
new MockExtensionContext(),
|
new MockGlobalStorage(),
|
||||||
funcIdentifier,
|
funcIdentifier,
|
||||||
func,
|
func,
|
||||||
(s) => createDate(s),
|
(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 {
|
class MockMemento implements Memento {
|
||||||
keys(): readonly string[] {
|
keys(): readonly string[] {
|
||||||
throw new Error("Method not implemented.");
|
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 () => {
|
it("initially invokes function", async () => {
|
||||||
let numTimesFuncCalled = 0;
|
let numTimesFuncCalled = 0;
|
||||||
const invocationRateLimiter = createInvocationRateLimiter(
|
const invocationRateLimiter = createInvocationRateLimiter(
|
||||||
|
|||||||
Reference in New Issue
Block a user