From 64750314eff747cab5362de253d9a867d8b8f32c Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 7 Nov 2022 16:04:48 -0500 Subject: [PATCH] Avoids thrashing on checkin calls --- src/plus/subscription/subscriptionService.ts | 1 + src/webviews/home/homeWebviewView.ts | 24 +++++++++++++++++--- src/webviews/webviewViewBase.ts | 2 ++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/plus/subscription/subscriptionService.ts b/src/plus/subscription/subscriptionService.ts index 13c7c0bb20584..30ed70c9b14fa 100644 --- a/src/plus/subscription/subscriptionService.ts +++ b/src/plus/subscription/subscriptionService.ts @@ -522,6 +522,7 @@ export class SubscriptionService implements Disposable { } private _lastCheckInDate: Date | undefined; + @gate(s => s.account.id) private async checkInAndValidate(session: AuthenticationSession, showSlowProgress: boolean = false): Promise { if (!showSlowProgress) return this.checkInAndValidateCore(session); diff --git a/src/webviews/home/homeWebviewView.ts b/src/webviews/home/homeWebviewView.ts index a8eb16b5be075..119c6fc6e9ae3 100644 --- a/src/webviews/home/homeWebviewView.ts +++ b/src/webviews/home/homeWebviewView.ts @@ -10,6 +10,8 @@ import type { SubscriptionChangeEvent } from '../../plus/subscription/subscripti import { ensurePlusFeaturesEnabled } from '../../plus/subscription/utils'; import type { Subscription } from '../../subscription'; import { executeCoreCommand, registerCommand } from '../../system/command'; +import type { Deferrable } from '../../system/function'; +import { debounce } from '../../system/function'; import type { IpcMessage } from '../protocol'; import { onIpc } from '../protocol'; import { WebviewViewBase } from '../webviewViewBase'; @@ -45,13 +47,19 @@ export class HomeWebviewView extends WebviewViewBase { } protected override onVisibilityChanged(visible: boolean): void { - if (!visible) return; + if (!visible) { + this._validateSubscriptionDebounced?.cancel(); + return; + } queueMicrotask(() => void this.validateSubscription()); } protected override onWindowFocusChanged(focused: boolean): void { - if (!focused) return; + if (!focused || !this.visible) { + this._validateSubscriptionDebounced?.cancel(); + return; + } queueMicrotask(() => void this.validateSubscription()); } @@ -198,8 +206,18 @@ export class HomeWebviewView extends WebviewViewBase { }); } - private _validating: Promise | undefined; + private _validateSubscriptionDebounced: Deferrable | undefined = undefined; + private async validateSubscription(): Promise { + if (this._validateSubscriptionDebounced == null) { + this._validateSubscriptionDebounced = debounce(this.validateSubscriptionCore, 1000); + } + + await this._validateSubscriptionDebounced(); + } + + private _validating: Promise | undefined; + private async validateSubscriptionCore() { if (this._validating == null) { this._validating = this.container.subscription.validate(); try { diff --git a/src/webviews/webviewViewBase.ts b/src/webviews/webviewViewBase.ts index a4b7e0c46baeb..2cbcabc7aef13 100644 --- a/src/webviews/webviewViewBase.ts +++ b/src/webviews/webviewViewBase.ts @@ -155,6 +155,8 @@ export abstract class WebviewViewBase implements } private onWindowStateChanged(e: WindowState) { + if (!this.visible) return; + this.onWindowFocusChanged?.(e.focused); }