feat: get fsm to a working testing state

This commit is contained in:
Paul Makles 2022-06-28 13:49:50 +01:00
parent 80f4bb3d98
commit ce88fab714
6 changed files with 63 additions and 16 deletions

2
external/lang vendored

@ -1 +1 @@
Subproject commit def08f210e9edc4f203cb38611fd270761102860
Subproject commit 50838167d7d253de9d08715e6a6070c3ddc9fcc2

View file

@ -20,7 +20,7 @@ const uiContext = {
Link,
Text: Text as any,
Trigger: ContextMenuTrigger,
emitAction: () => {},
emitAction: () => void {},
};
/**

View file

@ -9,6 +9,7 @@ import { Preloader } from "@revoltchat/ui";
import { useApplicationState } from "../../mobx/State";
import { clientController } from "../../controllers/client/ClientController";
import { modalController } from "../../controllers/modals/ModalController";
import { registerEvents } from "./events";
import { takeError } from "./util";
@ -36,8 +37,8 @@ type Props = {
};
export default observer(({ children }: Props) => {
const state = useApplicationState();
const [client, setClient] = useState<Client>(null!);
// const state = useApplicationState();
/*const [client, setClient] = useState<Client>(null!);
const [status, setStatus] = useState(ClientStatus.LOADING);
const [loaded, setLoaded] = useState(false);
@ -84,16 +85,24 @@ export default observer(({ children }: Props) => {
}, [state.auth.getSession()]);
useEffect(() => registerEvents(state, setStatus, client), [client]);
useEffect(() => state.registerListeners(client), [client]);
if (!loaded || status === ClientStatus.LOADING) {
return <Preloader type="spinner" />;
}*/
const session = clientController.getActiveSession();
if (!session?.ready) {
return <Preloader type="spinner" />;
}
const client = session.client!;
const state = useApplicationState();
useEffect(() => state.registerListeners(client), [state, client]);
return (
<AppContext.Provider value={client}>
<StatusContext.Provider value={status}>
<LogOutContext.Provider value={logout}>
<AppContext.Provider value={session.client!}>
<StatusContext.Provider value={ClientStatus.ONLINE}>
<LogOutContext.Provider value={() => void {}}>
{children}
</LogOutContext.Provider>
</StatusContext.Provider>

View file

@ -1,4 +1,4 @@
import { action, makeAutoObservable, ObservableMap } from "mobx";
import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
import type { Nullable } from "revolt.js";
import Auth from "../../mobx/stores/Auth";
@ -30,20 +30,35 @@ class ClientController {
@action hydrate(auth: Auth) {
for (const entry of auth.getAccounts()) {
const session = new Session();
this.sessions.set(entry.session._id!, session);
session.emit({
action: "LOGIN",
session: entry.session,
});
}
this.current = this.sessions.keys().next().value ?? null;
}
getActiveSession() {
return this.sessions;
@computed getActiveSession() {
return this.sessions.get(this.current!);
}
isLoggedIn() {
@computed isLoggedIn() {
return this.current === null;
}
@action logout(user_id: string) {
const session = this.sessions.get(user_id);
if (session) {
this.sessions.delete(user_id);
if (user_id === this.current) {
this.current = this.sessions.keys().next().value ?? null;
}
session.destroy();
}
}
}
export const clientController = new ClientController();

View file

@ -1,4 +1,4 @@
import { action, makeAutoObservable } from "mobx";
import { action, computed, makeAutoObservable } from "mobx";
import { Client } from "revolt.js";
type State = "Ready" | "Connecting" | "Online" | "Disconnected" | "Offline";
@ -34,6 +34,17 @@ export default class Session {
window.addEventListener("offline", this.onOffline);
}
/**
* Initiate logout and destroy client.
*/
@action destroy() {
if (this.client) {
this.client.logout(false);
this.state = "Ready";
this.client = null;
}
}
private onOnline() {
this.emit({
action: "ONLINE",
@ -90,6 +101,8 @@ export default class Session {
}
@action async emit(data: Transition) {
console.info("Handle event:", data);
switch (data.action) {
// Login with session
case "LOGIN": {
@ -161,4 +174,12 @@ export default class Session {
}
}
}
/**
* Whether we are ready to render.
* @returns Boolean
*/
@computed get ready() {
return this.client?.user;
}
}

View file

@ -2,6 +2,7 @@ import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
import { mapToRecord } from "../../lib/conversion";
import { clientController } from "../../controllers/client/ClientController";
import Persistent from "../interfaces/Persistent";
import Store from "../interfaces/Store";
@ -110,7 +111,8 @@ export default class Auth implements Store, Persistent<Data> {
* Check whether we are currently logged in.
* @returns Whether we are logged in
*/
/*@computed isLoggedIn() {
return this.current !== null;
}*/
@computed isLoggedIn() {
// ! FIXME: temp proxy info
return clientController.getActiveSession()?.ready;
}
}