Browse Source

Avoids any usage for setTimeout/setInterval

main
Eric Amodio 2 years ago
parent
commit
df7dd9f4ab
6 changed files with 28 additions and 19 deletions
  1. +1
    -1
      src/commands/closeUnchangedFiles.ts
  2. +9
    -3
      src/partners.ts
  3. +5
    -2
      src/statusbar/statusBarController.ts
  4. +8
    -8
      src/system/function.ts
  5. +4
    -4
      src/system/promise.ts
  6. +1
    -1
      src/trackers/documentTracker.ts

+ 1
- 1
src/commands/closeUnchangedFiles.ts View File

@ -130,7 +130,7 @@ export class CloseUnchangedFilesCommand extends Command {
private waitForEditorChange(timeout: number = 500): Promise<TextEditor | undefined> { private waitForEditorChange(timeout: number = 500): Promise<TextEditor | undefined> {
return new Promise<TextEditor | undefined>(resolve => { return new Promise<TextEditor | undefined>(resolve => {
let timer: any | undefined;
let timer: ReturnTypean><typeof setTimeout> | undefined;
this._onEditorChangedFn = (editor: TextEditor | undefined) => { this._onEditorChangedFn = (editor: TextEditor | undefined) => {
if (timer != null) { if (timer != null) {

+ 9
- 3
src/partners.ts View File

@ -12,12 +12,15 @@ export async function installExtension(
vsix?: Uri, vsix?: Uri,
): Promise<Extension<T> | undefined> { ): Promise<Extension<T> | undefined> {
try { try {
let timer: any = 0;
let timer: ReturnType<typeof setTimeout> | undefined = undefined;
const extension = new Promise<Extension<any> | undefined>(resolve => { const extension = new Promise<Extension<any> | undefined>(resolve => {
const disposable = extensions.onDidChange(() => { const disposable = extensions.onDidChange(() => {
const extension = extensions.getExtension(extensionId); const extension = extensions.getExtension(extensionId);
if (extension != null) { if (extension != null) {
clearTimeout(timer);
if (timer != null) {
clearTimeout(timer);
timer = undefined;
}
disposable.dispose(); disposable.dispose();
resolve(extension); resolve(extension);
@ -33,7 +36,10 @@ export async function installExtension(
await commands.executeCommand(BuiltInCommands.InstallExtension, vsix ?? extensionId); await commands.executeCommand(BuiltInCommands.InstallExtension, vsix ?? extensionId);
// Wait for extension activation until timeout expires // Wait for extension activation until timeout expires
timer = setTimeout(() => tokenSource.cancel(), timeout);
timer = setTimeout(() => {
timer = undefined;
tokenSource.cancel();
}, timeout);
return extension; return extension;
} catch { } catch {

+ 5
- 2
src/statusbar/statusBarController.ts View File

@ -25,7 +25,7 @@ import { LinesChangeEvent } from '../trackers/gitLineTracker';
export class StatusBarController implements Disposable { export class StatusBarController implements Disposable {
private _pullRequestCancellation: CancellationTokenSource | undefined; private _pullRequestCancellation: CancellationTokenSource | undefined;
private _tooltipCancellation: CancellationTokenSource | undefined; private _tooltipCancellation: CancellationTokenSource | undefined;
private _tooltipDelayTimer: any | undefined;
private _tooltipDelayTimer: ReturnTypean><typeof setTimeout> | undefined;
private readonly _disposable: Disposable; private readonly _disposable: Disposable;
private _statusBarBlame: StatusBarItem | undefined; private _statusBarBlame: StatusBarItem | undefined;
@ -305,10 +305,13 @@ export class StatusBarController implements Disposable {
this._statusBarBlame.tooltip = tooltip; this._statusBarBlame.tooltip = tooltip;
clearTimeout(this._tooltipDelayTimer);
if (this._tooltipDelayTimer != null) {
clearTimeout(this._tooltipDelayTimer);
}
this._tooltipCancellation?.cancel(); this._tooltipCancellation?.cancel();
this._tooltipDelayTimer = setTimeout(() => { this._tooltipDelayTimer = setTimeout(() => {
this._tooltipDelayTimer = undefined;
this._tooltipCancellation = new CancellationTokenSource(); this._tooltipCancellation = new CancellationTokenSource();
void this.updateCommitTooltip( void this.updateCommitTooltip(

+ 8
- 8
src/system/function.ts View File

@ -152,26 +152,26 @@ export function propOf>(o: T, key: K) {
} }
export function interval(fn: (...args: any[]) => void, ms: number): Disposable { export function interval(fn: (...args: any[]) => void, ms: number): Disposable {
let timer: any | undefined;
let timer: ReturnTypean><typeof setInterval> | undefined;
const disposable = { const disposable = {
dispose: () => { dispose: () => {
if (timer !== undefined) {
if (timer != null) {
clearInterval(timer); clearInterval(timer);
timer = undefined; timer = undefined;
} }
}, },
}; };
timer = globalThis.setInterval(fn, ms);
timer = setInterval(fn, ms);
return disposable; return disposable;
} }
export function progress<T>(promise: Promise<T>, intervalMs: number, onProgress: () => boolean): Promise<T> { export function progress<T>(promise: Promise<T>, intervalMs: number, onProgress: () => boolean): Promise<T> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let timer: any | undefined;
timer = globalThis.setInterval(() => {
let timer: ReturnTypean><typeof setInterval> | undefined;
timer = setInterval(() => {
if (onProgress()) { if (onProgress()) {
if (timer !== undefined) {
if (timer != null) {
clearInterval(timer); clearInterval(timer);
timer = undefined; timer = undefined;
} }
@ -180,7 +180,7 @@ export function progress(promise: Promise, intervalMs: number, onProgress:
promise.then( promise.then(
() => { () => {
if (timer !== undefined) {
if (timer != null) {
clearInterval(timer); clearInterval(timer);
timer = undefined; timer = undefined;
} }
@ -188,7 +188,7 @@ export function progress(promise: Promise, intervalMs: number, onProgress:
resolve(promise); resolve(promise);
}, },
ex => { ex => {
if (timer !== undefined) {
if (timer != null) {
clearInterval(timer); clearInterval(timer);
timer = undefined; timer = undefined;
} }

+ 4
- 4
src/system/promise.ts View File

@ -28,9 +28,9 @@ export function cancellable(
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let fulfilled = false; let fulfilled = false;
let timer: any | undefined;
let timer: ReturnTypean><typeof setTimeout> | undefined;
if (typeof timeoutOrToken === 'number') { if (typeof timeoutOrToken === 'number') {
timer = globalThis.setTimeout(() => {
timer = setTimeout(() => {
if (typeof options.onDidCancel === 'function') { if (typeof options.onDidCancel === 'function') {
options.onDidCancel(resolve, reject); options.onDidCancel(resolve, reject);
} else { } else {
@ -52,14 +52,14 @@ export function cancellable(
promise.then( promise.then(
() => { () => {
fulfilled = true; fulfilled = true;
if (timer !== undefined) {
if (timer != null) {
clearTimeout(timer); clearTimeout(timer);
} }
resolve(promise); resolve(promise);
}, },
ex => { ex => {
fulfilled = true; fulfilled = true;
if (timer !== undefined) {
if (timer != null) {
clearTimeout(timer); clearTimeout(timer);
} }
reject(ex); reject(ex);

+ 1
- 1
src/trackers/documentTracker.ts View File

@ -96,7 +96,7 @@ export class DocumentTracker implements Disposable {
this.onActiveTextEditorChanged(window.activeTextEditor); this.onActiveTextEditorChanged(window.activeTextEditor);
} }
private _timer: any | undefined;
private _timer: ReturnTypean><typeof setTimeout> | undefined;
private onActiveTextEditorChanged(editor: TextEditor | undefined) { private onActiveTextEditorChanged(editor: TextEditor | undefined) {
if (editor != null && !isTextEditor(editor)) return; if (editor != null && !isTextEditor(editor)) return;

Loading…
Cancel
Save