Skip to content

Commit

Permalink
Merge branch 'add-tabnine-chat-state' into fix-login-states
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/state/deriveState.ts
  • Loading branch information
ofekby committed Dec 4, 2023
2 parents e4fc120 + 81eb018 commit 2e02606
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 109 deletions.
49 changes: 27 additions & 22 deletions src/binary/binaryStateSingleton.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import { Disposable } from "vscode";
import EventEmitterBasedState from "../utils/EventEmitterBasedState";
import EventEmitterBasedState from "../state/EventEmitterBasedState";
import { State } from "./state";
import { getState, tabNineProcess } from "./requests/requests";
import { Logger } from "../utils/logger";

const SESSION_POLL_INTERVAL = 10_000;
import { BINARY_STATE_POLLING_INTERVAL_MILLISECONDS } from "../globals/consts";

export class BinaryState extends EventEmitterBasedState<State> {
private intervalDisposabled: Disposable | null = null;

start(): Disposable {
if (!this.intervalDisposabled) {
let interval: NodeJS.Timeout | undefined;
void tabNineProcess.onReady.then(() => {
interval = setInterval(() => {
void this.checkForUpdates();
}, SESSION_POLL_INTERVAL);
});

this.intervalDisposabled = new Disposable(() => {
if (interval) {
clearInterval(interval);
}
});
}

return this.intervalDisposabled;
private intervalDisposable: Disposable | null = null;

constructor() {
super();

let interval: NodeJS.Timeout | undefined;
void tabNineProcess.onReady.then(() => {
interval = setInterval(() => {
void this.checkForUpdates();
}, BINARY_STATE_POLLING_INTERVAL_MILLISECONDS);
});

this.intervalDisposable = new Disposable(() => {
if (interval) {
clearInterval(interval);
}
});
}

async checkForUpdates() {
Expand All @@ -35,6 +32,14 @@ export class BinaryState extends EventEmitterBasedState<State> {
Logger.warn("Failed to refetch state", error);
}
}

dispose(): void {
super.dispose();

if (this.intervalDisposable) {
this.intervalDisposable.dispose();
}
}
}

async function getStateOrNull() {
Expand Down
2 changes: 1 addition & 1 deletion src/enterprise/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export async function activate(
setTabnineExtensionContext(context);
context.subscriptions.push(await setEnterpriseContext());
context.subscriptions.push(new WorkspaceUpdater());
context.subscriptions.push(BINARY_STATE);

initReporter(new LogReporter());
const statusBar = new StatusBar(context);
Expand Down Expand Up @@ -157,7 +158,6 @@ function registerAuthenticationProviders(
): void {
const provider = new TabnineAuthenticationProvider();
context.subscriptions.push(
BINARY_STATE.start(),
vscode.authentication.registerAuthenticationProvider(
BRAND_NAME,
ENTERPRISE_BRAND_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ChatEnabledState, {
ChatEnabledStateData,
ChatStates,
} from "../../tabnineChatWidget/ChatEnabledState";
import EventEmitterBasedNonNullState from "../../utils/EventEmitterBasedNonNullState";
import EventEmitterBasedNonNullState from "../../state/EventEmitterBasedNonNullState";
import getUserInfo from "../requests/UserInfo";

export default class SelfHostedChatEnabledState
Expand Down
4 changes: 1 addition & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import EventName from "./reports/EventName";
import registerTabnineChatWidgetWebview from "./tabnineChatWidget/tabnineChatWidgetWebview";
import { forceRegistrationIfNeeded } from "./registration/forceRegistration";
import { installationState } from "./events/installationStateChangedEmitter";
import { statePoller } from "./state/statePoller";
import { Logger } from "./utils/logger";
import { callForLogin } from "./authentication/authentication.api";
import { emptyStateWelcomeView } from "./tabnineChatWidget/webviews/emptyStateChatWelcomeView";
Expand All @@ -69,7 +68,7 @@ export async function activate(
context.subscriptions.push(handleSelection(context));
context.subscriptions.push(handleUninstall(() => uponUninstall(context)));
context.subscriptions.push(installationState);
context.subscriptions.push(statePoller);
context.subscriptions.push(BINARY_STATE);
context.subscriptions.push(activeTextEditorState);
context.subscriptions.push(new WorkspaceUpdater());
registerCodeReview();
Expand Down Expand Up @@ -114,7 +113,6 @@ async function backgroundInit(context: vscode.ExtensionContext) {
isAuthenticationApiSupported()
) {
context.subscriptions.push(
BINARY_STATE.start(),
vscode.authentication.registerAuthenticationProvider(
BRAND_NAME,
BRAND_NAME,
Expand Down
2 changes: 1 addition & 1 deletion src/globals/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const BINARY_STATUS_BAR_FIRST_MESSAGE_POLLING_INTERVAL = +(
process.env.BINARY_NOTIFICATION_POLLING_INTERVAL || 10_000
); // 10 seconds

export const BINARY_STATE_POLLING_INTERVAL_MILLISECONDS = 1_000;
export const BINARY_STATE_POLLING_INTERVAL_MILLISECONDS = 2_000;

export const STATUS_BAR_NOTIFICATION_PERIOD = +(
process.env.STATUS_BAR_NOTIFICATION_PERIOD || 2 * 60 * 1_000
Expand Down
27 changes: 15 additions & 12 deletions src/registration/forceRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
isEnabled,
onDidRefreshCapabilities,
} from "../capabilities/capabilities";
import { statePoller } from "../state/statePoller";
import {
installationState,
InstallationState,
Expand All @@ -14,6 +13,7 @@ import { Publisher } from "../utils/publisher";
import { PopupTristate } from "./popupTristate";
import { callForLogin } from "../authentication/authentication.api";
import { fireEvent } from "../binary/requests/requests";
import BINARY_STATE from "../binary/binaryStateSingleton";

const isForceEnabled = new Publisher(isEnabled(Capability.FORCE_REGISTRATION));

Expand All @@ -29,8 +29,8 @@ export function shouldBlockCompletions(): boolean {
export function shouldStatusBarBeProminent(): boolean {
return (
isForceEnabled.value === true &&
statePoller.state.currentState?.service_level === "Free" &&
!(statePoller.state.currentState?.is_logged_in ?? false)
BINARY_STATE.get()?.service_level === "Free" &&
!(BINARY_STATE.get()?.is_logged_in ?? false)
);
}

Expand Down Expand Up @@ -75,14 +75,14 @@ function forceFlowFSM() {
}

if (installationState.state === InstallationState.ExistingInstallation) {
if (statePoller.state.currentState?.is_logged_in === false) {
if (BINARY_STATE.get()?.is_logged_in === false) {
void notifyState();
} else if (!statePoller.state.currentState) {
} else if (!BINARY_STATE.get()) {
// still waiting for a state message from the binary -
// in this case we subscribe to the state change
// delay the notification until we have enough information
const disposable = statePoller.event((change) => {
if (change.currentState?.is_logged_in === false) {
const disposable = BINARY_STATE.onChange((state) => {
if (state.is_logged_in === false) {
disposable.dispose();
void notifyState();
}
Expand Down Expand Up @@ -146,13 +146,16 @@ function awaitLoginNotification() {
});
}
}
if (statePoller.state.currentState?.is_logged_in) {
presentWhenFocused(statePoller.state.currentState.user_name);

const state = BINARY_STATE.get();

if (state?.is_logged_in && state?.user_name) {
presentWhenFocused(state.user_name);
} else {
const disposable = statePoller.event((change) => {
if (change.currentState?.is_logged_in) {
const disposable = BINARY_STATE.onChange((currentState) => {
if (currentState.is_logged_in) {
disposable.dispose();
presentWhenFocused(change.currentState.user_name);
presentWhenFocused(currentState.user_name);
}
});
}
Expand Down
12 changes: 7 additions & 5 deletions src/registration/popupTristate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

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

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

private isLoggedIn = statePoller.state.currentState?.is_logged_in;
private isLoggedIn = BINARY_STATE.get()?.is_logged_in;

private didDisplay = false;

Expand All @@ -27,9 +27,11 @@ export class PopupTristate implements Disposable {
});
}

if (!statePoller.state.currentState) {
const disposable = statePoller.event((state) => {
if (state.currentState?.is_logged_in === false) {
const currentState = BINARY_STATE.get();

if (!currentState) {
const disposable = BINARY_STATE.onChange((state) => {
if (state.is_logged_in === false) {
disposable.dispose();
this.isLoggedIn = false;
this.changedStateEmitter.fire();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Mutex } from "await-semaphore";
import { isEqual } from "underscore";
import { Disposable, EventEmitter } from "vscode";

export default class EventEmitterBasedState<T> {
export default class EventEmitterBasedState<T> implements Disposable {
private value: T | null = null;

private eventEmitter = new EventEmitter<T>();
Expand Down Expand Up @@ -43,4 +43,8 @@ export default class EventEmitterBasedState<T> {

return this.eventEmitter.event(subscription);
}

dispose() {
this.eventEmitter.dispose();
}
}
1 change: 1 addition & 0 deletions src/utils/deriveState.ts → src/state/deriveState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function deriveState<I, O>(
}

dispose() {
super.dispose();
this.useStateDisposabled.dispose();
}
}
Expand Down
58 changes: 0 additions & 58 deletions src/state/statePoller.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/statusBar/pollStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
import handleStatus, {
disposeStatusBarCommand,
} from "./statusBarActionHandler";
import { statePoller } from "../state/statePoller";
import BINARY_STATE from "../binary/binaryStateSingleton";

let statusPollingInterval: NodeJS.Timeout | null = null;

export default function pollStatuses(
context: vscode.ExtensionContext
): vscode.Disposable {
const statePollerDisposable = statePoller.event((change) => {
setServiceLevel(change.currentState?.service_level);
const statePollerDisposable = BINARY_STATE.onChange((state) => {
setServiceLevel(state.service_level);
});
context.subscriptions.push(statePollerDisposable);
statusPollingInterval = setInterval(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/tabnineChatWidget/SaasChatEnabledState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
isCapabilityEnabled,
onDidRefreshCapabilities,
} from "../capabilities/capabilities";
import EventEmitterBasedNonNullState from "../utils/EventEmitterBasedNonNullState";
import { useDerviedState } from "../utils/deriveState";
import EventEmitterBasedNonNullState from "../state/EventEmitterBasedNonNullState";
import { useDerviedState } from "../state/deriveState";
import BINARY_STATE from "../binary/binaryStateSingleton";
import { State } from "../binary/state";

Expand Down

0 comments on commit 2e02606

Please sign in to comment.