feat: get fsm to a working testing state

pull/1049/head
Paul Makles 2022-06-28 13:49:50 +01:00
parent 0ecc3d319b
commit fb466c44ed
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, Link,
Text: Text as any, Text: Text as any,
Trigger: ContextMenuTrigger, Trigger: ContextMenuTrigger,
emitAction: () => {}, emitAction: () => void {},
}; };
/** /**

View File

@ -9,6 +9,7 @@ import { Preloader } from "@revoltchat/ui";
import { useApplicationState } from "../../mobx/State"; import { useApplicationState } from "../../mobx/State";
import { clientController } from "../../controllers/client/ClientController";
import { modalController } from "../../controllers/modals/ModalController"; import { modalController } from "../../controllers/modals/ModalController";
import { registerEvents } from "./events"; import { registerEvents } from "./events";
import { takeError } from "./util"; import { takeError } from "./util";
@ -36,8 +37,8 @@ type Props = {
}; };
export default observer(({ children }: Props) => { export default observer(({ children }: Props) => {
const state = useApplicationState(); // const state = useApplicationState();
const [client, setClient] = useState<Client>(null!); /*const [client, setClient] = useState<Client>(null!);
const [status, setStatus] = useState(ClientStatus.LOADING); const [status, setStatus] = useState(ClientStatus.LOADING);
const [loaded, setLoaded] = useState(false); const [loaded, setLoaded] = useState(false);
@ -84,16 +85,24 @@ export default observer(({ children }: Props) => {
}, [state.auth.getSession()]); }, [state.auth.getSession()]);
useEffect(() => registerEvents(state, setStatus, client), [client]); useEffect(() => registerEvents(state, setStatus, client), [client]);
useEffect(() => state.registerListeners(client), [client]);
if (!loaded || status === ClientStatus.LOADING) { if (!loaded || status === ClientStatus.LOADING) {
return <Preloader type="spinner" />; 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 ( return (
<AppContext.Provider value={client}> <AppContext.Provider value={session.client!}>
<StatusContext.Provider value={status}> <StatusContext.Provider value={ClientStatus.ONLINE}>
<LogOutContext.Provider value={logout}> <LogOutContext.Provider value={() => void {}}>
{children} {children}
</LogOutContext.Provider> </LogOutContext.Provider>
</StatusContext.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 type { Nullable } from "revolt.js";
import Auth from "../../mobx/stores/Auth"; import Auth from "../../mobx/stores/Auth";
@ -30,20 +30,35 @@ class ClientController {
@action hydrate(auth: Auth) { @action hydrate(auth: Auth) {
for (const entry of auth.getAccounts()) { for (const entry of auth.getAccounts()) {
const session = new Session(); const session = new Session();
this.sessions.set(entry.session._id!, session);
session.emit({ session.emit({
action: "LOGIN", action: "LOGIN",
session: entry.session, session: entry.session,
}); });
} }
this.current = this.sessions.keys().next().value ?? null;
} }
getActiveSession() { @computed getActiveSession() {
return this.sessions; return this.sessions.get(this.current!);
} }
isLoggedIn() { @computed isLoggedIn() {
return this.current === null; 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(); 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"; import { Client } from "revolt.js";
type State = "Ready" | "Connecting" | "Online" | "Disconnected" | "Offline"; type State = "Ready" | "Connecting" | "Online" | "Disconnected" | "Offline";
@ -34,6 +34,17 @@ export default class Session {
window.addEventListener("offline", this.onOffline); 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() { private onOnline() {
this.emit({ this.emit({
action: "ONLINE", action: "ONLINE",
@ -90,6 +101,8 @@ export default class Session {
} }
@action async emit(data: Transition) { @action async emit(data: Transition) {
console.info("Handle event:", data);
switch (data.action) { switch (data.action) {
// Login with session // Login with session
case "LOGIN": { 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 { mapToRecord } from "../../lib/conversion";
import { clientController } from "../../controllers/client/ClientController";
import Persistent from "../interfaces/Persistent"; import Persistent from "../interfaces/Persistent";
import Store from "../interfaces/Store"; import Store from "../interfaces/Store";
@ -110,7 +111,8 @@ export default class Auth implements Store, Persistent<Data> {
* Check whether we are currently logged in. * Check whether we are currently logged in.
* @returns Whether we are logged in * @returns Whether we are logged in
*/ */
/*@computed isLoggedIn() { @computed isLoggedIn() {
return this.current !== null; // ! FIXME: temp proxy info
}*/ return clientController.getActiveSession()?.ready;
}
} }