-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
381 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Disposable } from "vscode"; | ||
import EventEmitterBasedState from "../../state/EventEmitterBasedState"; | ||
import getUserInfo, { UserInfo } from "../requests/UserInfo"; | ||
import { useDerviedState } from "../../state/deriveState"; | ||
import BINARY_STATE from "../../binary/binaryStateSingleton"; | ||
|
||
class UserInfoState extends EventEmitterBasedState<UserInfo> { | ||
toDispose: Disposable; | ||
|
||
constructor() { | ||
super(); | ||
|
||
this.toDispose = useDerviedState( | ||
BINARY_STATE, | ||
(s) => s.is_logged_in, | ||
() => { | ||
void this.asyncSet(fetchUserState); | ||
} | ||
); | ||
} | ||
} | ||
|
||
async function fetchUserState(): Promise<UserInfo | null> { | ||
return (await getUserInfo()) ?? null; | ||
} | ||
|
||
const USER_INFO_STATE = new UserInfoState(); | ||
export default USER_INFO_STATE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { Disposable } from "vscode"; | ||
import EventEmitterBasedNonNullState from "../../state/EventEmitterBasedNonNullState"; | ||
import { tabNineProcess } from "../../binary/requests/requests"; | ||
import { | ||
PromiseStateData, | ||
convertPromiseToState, | ||
triggeredPromiseState, | ||
useDerviedState, | ||
} from "../../state/deriveState"; | ||
import BINARY_STATE from "../../binary/binaryStateSingleton"; | ||
import { rejectOnTimeout } from "../../utils/utils"; | ||
import { completionsState } from "../../state/completionsState"; | ||
import { isHealthyServer } from "../update/isHealthyServer"; | ||
import { UserInfo } from "../requests/UserInfo"; | ||
import USER_INFO_STATE from "../lifecycle/UserInfoState"; | ||
import calculateStatusBarState, { | ||
INITIAL_STATE, | ||
StatusBarStateData, | ||
} from "./calculateStatusBarState"; | ||
|
||
export default class StatusBarState extends EventEmitterBasedNonNullState<StatusBarStateData> { | ||
private toDispose: Disposable; | ||
|
||
private processStartedState = triggeredPromiseState(() => | ||
rejectOnTimeout(tabNineProcess.onReady, 10_000) | ||
); | ||
|
||
constructor() { | ||
super(INITIAL_STATE); | ||
|
||
const serverHealthOnPluginStartState = convertPromiseToState( | ||
isHealthyServer() | ||
); | ||
|
||
const startedProcessDisposable = this.processStartedState.onChange( | ||
(startedState) => { | ||
this.updateState( | ||
BINARY_STATE.get()?.cloud_connection_health_status, | ||
startedState, | ||
completionsState.value, | ||
serverHealthOnPluginStartState.get(), | ||
USER_INFO_STATE.get() | ||
); | ||
} | ||
); | ||
const serverHealthDisposable = serverHealthOnPluginStartState.onChange( | ||
(isHealthy) => { | ||
this.updateState( | ||
BINARY_STATE.get()?.cloud_connection_health_status, | ||
this.processStartedState.get(), | ||
completionsState.value, | ||
isHealthy, | ||
USER_INFO_STATE.get() | ||
); | ||
} | ||
); | ||
|
||
this.updateState( | ||
BINARY_STATE.get()?.cloud_connection_health_status, | ||
this.processStartedState.get(), | ||
completionsState.value, | ||
serverHealthOnPluginStartState.get(), | ||
USER_INFO_STATE.get() | ||
); | ||
|
||
const stateDisposable = useDerviedState( | ||
BINARY_STATE, | ||
(s) => s.cloud_connection_health_status, | ||
(cloudConnection) => { | ||
this.updateState( | ||
cloudConnection, | ||
this.processStartedState.get(), | ||
completionsState.value, | ||
serverHealthOnPluginStartState.get(), | ||
USER_INFO_STATE.get() | ||
); | ||
} | ||
); | ||
|
||
const userInfoStateDisposable = USER_INFO_STATE.onChange((userInfo) => { | ||
this.updateState( | ||
BINARY_STATE.get()?.cloud_connection_health_status, | ||
this.processStartedState.get(), | ||
completionsState.value, | ||
serverHealthOnPluginStartState.get(), | ||
userInfo | ||
); | ||
}); | ||
|
||
completionsState.on("changed", () => { | ||
this.updateState( | ||
BINARY_STATE.get()?.cloud_connection_health_status, | ||
this.processStartedState.get(), | ||
completionsState.value, | ||
serverHealthOnPluginStartState.get(), | ||
USER_INFO_STATE.get() | ||
); | ||
}); | ||
|
||
this.toDispose = Disposable.from( | ||
userInfoStateDisposable, | ||
stateDisposable, | ||
startedProcessDisposable, | ||
serverHealthDisposable, | ||
this.processStartedState | ||
); | ||
} | ||
|
||
private updateState( | ||
cloudConnection: "Ok" | string | undefined | null, | ||
processStartedState: PromiseStateData<unknown>, | ||
isCompletionsEnabled: boolean, | ||
serverHealthOnPluginStart: PromiseStateData<boolean>, | ||
userInfo: UserInfo | null | ||
) { | ||
this.set( | ||
calculateStatusBarState( | ||
cloudConnection, | ||
processStartedState, | ||
isCompletionsEnabled, | ||
serverHealthOnPluginStart, | ||
userInfo | ||
) | ||
); | ||
} | ||
|
||
startWaitingForProcess() { | ||
this.processStartedState.trigger(); | ||
} | ||
|
||
dispose(): void { | ||
super.dispose(); | ||
this.toDispose.dispose(); | ||
} | ||
} |
Oops, something went wrong.