Skip to content

Commit

Permalink
Avoids thrashing on checkin calls
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Nov 7, 2022
1 parent c7d0ad6 commit 6475031
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/plus/subscription/subscriptionService.ts
Expand Up @@ -522,6 +522,7 @@ export class SubscriptionService implements Disposable {
}

private _lastCheckInDate: Date | undefined;
@gate<SubscriptionService['checkInAndValidate']>(s => s.account.id)
private async checkInAndValidate(session: AuthenticationSession, showSlowProgress: boolean = false): Promise<void> {
if (!showSlowProgress) return this.checkInAndValidateCore(session);

Expand Down
24 changes: 21 additions & 3 deletions src/webviews/home/homeWebviewView.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -45,13 +47,19 @@ export class HomeWebviewView extends WebviewViewBase<State> {
}

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());
}
Expand Down Expand Up @@ -198,8 +206,18 @@ export class HomeWebviewView extends WebviewViewBase<State> {
});
}

private _validating: Promise<void> | undefined;
private _validateSubscriptionDebounced: Deferrable<HomeWebviewView['validateSubscription']> | undefined = undefined;

private async validateSubscription(): Promise<void> {
if (this._validateSubscriptionDebounced == null) {
this._validateSubscriptionDebounced = debounce(this.validateSubscriptionCore, 1000);
}

await this._validateSubscriptionDebounced();
}

private _validating: Promise<void> | undefined;
private async validateSubscriptionCore() {
if (this._validating == null) {
this._validating = this.container.subscription.validate();
try {
Expand Down
2 changes: 2 additions & 0 deletions src/webviews/webviewViewBase.ts
Expand Up @@ -155,6 +155,8 @@ export abstract class WebviewViewBase<State, SerializedState = State> implements
}

private onWindowStateChanged(e: WindowState) {
if (!this.visible) return;

this.onWindowFocusChanged?.(e.focused);
}

Expand Down

0 comments on commit 6475031

Please sign in to comment.