Skip to content

Commit

Permalink
fix delayed capabilities issue
Browse files Browse the repository at this point in the history
  • Loading branch information
avner committed Jun 18, 2023
1 parent 6fd1b1d commit 662fb9e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
18 changes: 18 additions & 0 deletions src/events/installationStateChangedEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,37 @@ export class InstallationStateEmitter implements Disposable {

private internalState = InstallationState.Undefined;

private hadNewInstallState?: boolean;

get state(): InstallationState {
return this.internalState;
}

fire(state: InstallationState) {
this.internalState = state;
if (state === InstallationState.NewInstallation) {
this.hadNewInstallState = true;
} else if (
state === InstallationState.ExistingInstallation &&
!this.hadNewInstallState
) {
this.hadNewInstallState = false;
}
this.emitter.fire(state);
}

get event(): Event<InstallationState> {
return this.emitter.event;
}

get newInstall(): boolean | undefined {
// Installation state is set by the binary fetcher based on the binary path.
// Subsequent launches of the binary within the same session can toggle that state
// that is a problem when capabilities arriving later on and their flows relying on "new install" state will always fail.
// We need to keep this history around for this session
return this.hadNewInstallState;
}

dispose() {
this.emitter.dispose();
}
Expand Down
11 changes: 6 additions & 5 deletions src/registration/forceRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ export function shouldStatusBarBeProminent(): boolean {

if (isForceEnabled.value === undefined) {
const disposable = onDidRefreshCapabilities(() => {
disposable.dispose();
isForceEnabled.value = isCapabilityEnabled(Capability.FORCE_REGISTRATION);
if (isForceEnabled.value) {
disposable.dispose();
}
});
}

export function forceRegistrationIfNeeded() {
if (isForceEnabled.value === true) {
void forceFlowFSM();
} else if (isForceEnabled.value === undefined) {
} else {
// wait for value
const disposable = isForceEnabled.emitter.event((enabled) => {
// at this point has a real value
disposable.dispose();
if (enabled) {
disposable.dispose();
void forceFlowFSM();
}
});
Expand Down Expand Up @@ -81,8 +82,8 @@ function forceFlowFSM() {
// in this case we subscribe to the state change
// delay the notification until we have enough information
const disposable = statePoller.event((change) => {
disposable.dispose();
if (change.currentState?.is_logged_in === false) {
disposable.dispose();
void notifyState();
}
});
Expand Down
30 changes: 10 additions & 20 deletions src/registration/popupTristate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
// 3. never displayed the popup before

import { Disposable, EventEmitter } from "vscode";
import {
InstallationState,
installationState,
} from "../events/installationStateChangedEmitter";
import { installationState } from "../events/installationStateChangedEmitter";
import { statePoller } from "../state/statePoller";

export class PopupTristate implements Disposable {
private installationState = installationState.state;
private newInstall = installationState.newInstall;

private isLoggedIn = statePoller.state.currentState?.is_logged_in;

Expand All @@ -20,10 +17,10 @@ export class PopupTristate implements Disposable {
changedStateEmitter = new EventEmitter<void>();

constructor() {
if (this.installationState === InstallationState.Undefined) {
const dispose = installationState.event((state) => {
if (state !== InstallationState.Undefined) {
this.installationState = state;
if (this.newInstall === undefined) {
const dispose = installationState.event(() => {
if (installationState.newInstall) {
this.newInstall = true;
dispose.dispose();
this.changedStateEmitter.fire();
}
Expand All @@ -32,28 +29,21 @@ export class PopupTristate implements Disposable {

if (!statePoller.state.currentState) {
const disposable = statePoller.event((state) => {
if (state.currentState) {
if (state.currentState?.is_logged_in === false) {
disposable.dispose();
this.isLoggedIn = state.currentState.is_logged_in;
this.isLoggedIn = false;
this.changedStateEmitter.fire();
}
});
}
}

get needWait(): boolean {
return (
this.installationState === InstallationState.Undefined ||
!statePoller.state.currentState
);
return this.newInstall === undefined || this.isLoggedIn === undefined;
}

get shouldDisplay() {
return (
this.installationState === InstallationState.NewInstallation &&
this.isLoggedIn === false &&
!this.didDisplay
);
return this.newInstall && this.isLoggedIn === false && !this.didDisplay;
}

displayed() {
Expand Down

0 comments on commit 662fb9e

Please sign in to comment.