mirror of
https://github.com/stoatchat/for-legacy-web.git
synced 2026-03-07 09:25:27 +00:00
feat: get fsm to a working testing state
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user