From df7dd9f4abb9912483c5faa7f2ad649b57971eab Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 19 Jan 2022 02:08:17 -0500 Subject: [PATCH] Avoids any usage for setTimeout/setInterval --- src/commands/closeUnchangedFiles.ts | 2 +- src/partners.ts | 12 +++++++++--- src/statusbar/statusBarController.ts | 7 +++++-- src/system/function.ts | 16 ++++++++-------- src/system/promise.ts | 8 ++++---- src/trackers/documentTracker.ts | 2 +- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/commands/closeUnchangedFiles.ts b/src/commands/closeUnchangedFiles.ts index e984ad1..499bb51 100644 --- a/src/commands/closeUnchangedFiles.ts +++ b/src/commands/closeUnchangedFiles.ts @@ -130,7 +130,7 @@ export class CloseUnchangedFilesCommand extends Command { private waitForEditorChange(timeout: number = 500): Promise { return new Promise(resolve => { - let timer: any | undefined; + let timer: ReturnType | undefined; this._onEditorChangedFn = (editor: TextEditor | undefined) => { if (timer != null) { diff --git a/src/partners.ts b/src/partners.ts index 4fc58ce..01cf40b 100644 --- a/src/partners.ts +++ b/src/partners.ts @@ -12,12 +12,15 @@ export async function installExtension( vsix?: Uri, ): Promise | undefined> { try { - let timer: any = 0; + let timer: ReturnType | undefined = undefined; const extension = new Promise | undefined>(resolve => { const disposable = extensions.onDidChange(() => { const extension = extensions.getExtension(extensionId); if (extension != null) { - clearTimeout(timer); + if (timer != null) { + clearTimeout(timer); + timer = undefined; + } disposable.dispose(); resolve(extension); @@ -33,7 +36,10 @@ export async function installExtension( await commands.executeCommand(BuiltInCommands.InstallExtension, vsix ?? extensionId); // Wait for extension activation until timeout expires - timer = setTimeout(() => tokenSource.cancel(), timeout); + timer = setTimeout(() => { + timer = undefined; + tokenSource.cancel(); + }, timeout); return extension; } catch { diff --git a/src/statusbar/statusBarController.ts b/src/statusbar/statusBarController.ts index 9cd202a..cee920c 100644 --- a/src/statusbar/statusBarController.ts +++ b/src/statusbar/statusBarController.ts @@ -25,7 +25,7 @@ import { LinesChangeEvent } from '../trackers/gitLineTracker'; export class StatusBarController implements Disposable { private _pullRequestCancellation: CancellationTokenSource | undefined; private _tooltipCancellation: CancellationTokenSource | undefined; - private _tooltipDelayTimer: any | undefined; + private _tooltipDelayTimer: ReturnType | undefined; private readonly _disposable: Disposable; private _statusBarBlame: StatusBarItem | undefined; @@ -305,10 +305,13 @@ export class StatusBarController implements Disposable { this._statusBarBlame.tooltip = tooltip; - clearTimeout(this._tooltipDelayTimer); + if (this._tooltipDelayTimer != null) { + clearTimeout(this._tooltipDelayTimer); + } this._tooltipCancellation?.cancel(); this._tooltipDelayTimer = setTimeout(() => { + this._tooltipDelayTimer = undefined; this._tooltipCancellation = new CancellationTokenSource(); void this.updateCommitTooltip( diff --git a/src/system/function.ts b/src/system/function.ts index 61d74da..f846157 100644 --- a/src/system/function.ts +++ b/src/system/function.ts @@ -152,26 +152,26 @@ export function propOf>(o: T, key: K) { } export function interval(fn: (...args: any[]) => void, ms: number): Disposable { - let timer: any | undefined; + let timer: ReturnType | undefined; const disposable = { dispose: () => { - if (timer !== undefined) { + if (timer != null) { clearInterval(timer); timer = undefined; } }, }; - timer = globalThis.setInterval(fn, ms); + timer = setInterval(fn, ms); return disposable; } export function progress(promise: Promise, intervalMs: number, onProgress: () => boolean): Promise { return new Promise((resolve, reject) => { - let timer: any | undefined; - timer = globalThis.setInterval(() => { + let timer: ReturnType | undefined; + timer = setInterval(() => { if (onProgress()) { - if (timer !== undefined) { + if (timer != null) { clearInterval(timer); timer = undefined; } @@ -180,7 +180,7 @@ export function progress(promise: Promise, intervalMs: number, onProgress: promise.then( () => { - if (timer !== undefined) { + if (timer != null) { clearInterval(timer); timer = undefined; } @@ -188,7 +188,7 @@ export function progress(promise: Promise, intervalMs: number, onProgress: resolve(promise); }, ex => { - if (timer !== undefined) { + if (timer != null) { clearInterval(timer); timer = undefined; } diff --git a/src/system/promise.ts b/src/system/promise.ts index 62a113f..9972e65 100644 --- a/src/system/promise.ts +++ b/src/system/promise.ts @@ -28,9 +28,9 @@ export function cancellable( return new Promise((resolve, reject) => { let fulfilled = false; - let timer: any | undefined; + let timer: ReturnType | undefined; if (typeof timeoutOrToken === 'number') { - timer = globalThis.setTimeout(() => { + timer = setTimeout(() => { if (typeof options.onDidCancel === 'function') { options.onDidCancel(resolve, reject); } else { @@ -52,14 +52,14 @@ export function cancellable( promise.then( () => { fulfilled = true; - if (timer !== undefined) { + if (timer != null) { clearTimeout(timer); } resolve(promise); }, ex => { fulfilled = true; - if (timer !== undefined) { + if (timer != null) { clearTimeout(timer); } reject(ex); diff --git a/src/trackers/documentTracker.ts b/src/trackers/documentTracker.ts index f3992f3..aad0ca6 100644 --- a/src/trackers/documentTracker.ts +++ b/src/trackers/documentTracker.ts @@ -96,7 +96,7 @@ export class DocumentTracker implements Disposable { this.onActiveTextEditorChanged(window.activeTextEditor); } - private _timer: any | undefined; + private _timer: ReturnType | undefined; private onActiveTextEditorChanged(editor: TextEditor | undefined) { if (editor != null && !isTextEditor(editor)) return;