chore: clean up FSM code

This commit is contained in:
Paul Makles
2022-06-29 10:28:24 +01:00
parent 5f2311b09c
commit 8d505c9564
6 changed files with 44 additions and 20 deletions

View File

@@ -1,6 +1,8 @@
import { action, computed, makeAutoObservable, ObservableMap } from "mobx";
import { Client, Nullable } from "revolt.js";
import { injectController } from "../../lib/window";
import Auth from "../../mobx/stores/Auth";
import { modalController } from "../modals/ModalController";
@@ -35,7 +37,7 @@ class ClientController {
this.logoutCurrent = this.logoutCurrent.bind(this);
// Inject globally
(window as any).clientController = this;
injectController("client", this);
}
/**
@@ -53,12 +55,14 @@ class ClientController {
.emit({
action: "LOGIN",
session: entry.session,
apiUrl: entry.apiUrl,
})
.catch((error) => {
if (error === "Forbidden" || error === "Unauthorized") {
this.sessions.delete(user_id);
auth.removeSession(user_id);
modalController.push({ type: "signed_out" });
session.destroy();
}
});
}

View File

@@ -7,6 +7,7 @@ type Transition =
| {
action: "LOGIN";
session: SessionPrivate;
apiUrl?: string;
}
| {
action:
@@ -70,12 +71,12 @@ export default class Session {
});
}
private createClient() {
private createClient(apiUrl?: string) {
this.client = new Client({
unreads: true,
autoReconnect: false,
onPongTimeout: "EXIT",
apiURL: import.meta.env.VITE_API_URL,
apiURL: apiUrl ?? import.meta.env.VITE_API_URL,
});
this.client.addListener("dropped", this.onDropped);
@@ -110,7 +111,7 @@ export default class Session {
case "LOGIN": {
this.assert("Ready");
this.state = "Connecting";
this.createClient();
this.createClient(data.apiUrl);
try {
await this.client!.useExistingSession(data.session);
@@ -135,10 +136,13 @@ export default class Session {
this.state = "Disconnected";
setTimeout(() => {
this.emit({
action: "RETRY",
});
}, 1500);
// Check we are still disconnected before retrying.
if (this.state === "Disconnected") {
this.emit({
action: "RETRY",
});
}
}, 1000);
}
break;

View File

@@ -9,6 +9,7 @@ import type { Client, API } from "revolt.js";
import { ulid } from "ulid";
import { determineLink } from "../../lib/links";
import { injectController } from "../../lib/window";
import { getApplicationState } from "../../mobx/State";
@@ -52,6 +53,9 @@ class ModalController<T extends Modal> {
rendered: computed,
isVisible: computed,
});
// Inject globally
injectController("modal", this);
}
/**