Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only trigger auto-update once in a VSCode session #623

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
prunedFolderNames,
sortedWorkspaceFolders
} from './vscodeUtils';
import { sleep } from './utils';
import {
SingleInstanceTimeout,
sleep
} from './utils';

interface terraformLanguageClient {
commandPrefix: string,
Expand All @@ -40,6 +43,7 @@ const appInsightsKey = '885372d2-6f3c-499f-9d25-b8b219983a52';
let reporter: TelemetryReporter;

let installPath: string;
let languageServerUpdater = new SingleInstanceTimeout();

export async function activate(context: vscode.ExtensionContext): Promise<any> {
const extensionVersion = vscode.extensions.getExtension(extensionId).packageJSON.version;
Expand Down Expand Up @@ -70,6 +74,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
const current = config('terraform').get('languageServer');
await config('terraform').update('languageServer', Object.assign(current, { external: false }), vscode.ConfigurationTarget.Global);
}
languageServerUpdater.clear();
return stopClients();
}),
vscode.commands.registerCommand('terraform.apply', async () => {
Expand Down Expand Up @@ -168,7 +173,7 @@ export function deactivate(): Promise<void[]> {

async function updateLanguageServer() {
const delay = 1000 * 60 * 60 * 24;
setTimeout(updateLanguageServer, delay); // check for new updates every 24hrs
languageServerUpdater.timeout(updateLanguageServer, delay); // check for new updates every 24hrs

// skip install if a language server binary path is set
if (!config('terraform').get('languageServer.pathToBinary')) {
Expand Down
19 changes: 19 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ export function httpsRequest(url: string, options: https.RequestOptions = {}, en
export async function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}

// A small wrapper around setTimeout which ensures that only a single timeout
// timer can be running at a time. Attempts to add a new timeout silently fail.
export class SingleInstanceTimeout {
private timerLock = false;
private timerId: NodeJS.Timeout;

public timeout(fn, delay, ...args) {
if (!this.timerLock) {
this.timerLock = true;
this.timerId = setTimeout(function () { this.timerLock = false; fn() }, delay, args)
}
}

public clear() {
if (this.timerId) { clearTimeout(this.timerId) }
this.timerLock = false;
}
}