chore: deprecate RevoltClient context

This commit is contained in:
Paul Makles
2022-06-29 16:02:35 +01:00
parent 0e86f19da2
commit 0261fec676
13 changed files with 118 additions and 108 deletions

View File

@@ -58,8 +58,9 @@ class ClientController {
}
@action pickNextSession() {
this.current =
this.current ?? this.sessions.keys().next().value ?? null;
this.switchAccount(
this.current ?? this.sessions.keys().next().value ?? null,
);
}
/**
@@ -82,6 +83,15 @@ class ClientController {
return this.sessions.get(this.current!);
}
/**
* Get the currently ready client
* @returns Ready Client
*/
@computed getReadyClient() {
const session = this.getActiveSession();
return session && session.ready ? session.client! : undefined;
}
/**
* Get an unauthenticated instance of the Revolt.js Client
* @returns API Client
@@ -111,7 +121,15 @@ class ClientController {
* @returns Whether we are logged in
*/
@computed isLoggedIn() {
return this.current === null;
return this.current !== null;
}
/**
* Check whether we are currently ready
* @returns Whether we are ready to render
*/
@computed isReady() {
return this.getActiveSession()?.ready;
}
/**
@@ -127,6 +145,7 @@ class ClientController {
const session = new Session();
this.sessions.set(user_id, session);
this.pickNextSession();
session
.emit({
@@ -144,8 +163,6 @@ class ClientController {
session.destroy();
}
});
this.pickNextSession();
}
/**

View File

@@ -270,6 +270,6 @@ export default class Session {
* @returns Boolean
*/
@computed get ready() {
return this.client?.user;
return !!this.client?.user;
}
}

View File

@@ -0,0 +1,27 @@
import { observer } from "mobx-react-lite";
import { useEffect } from "preact/hooks";
import { Preloader } from "@revoltchat/ui";
import { state } from "../../../mobx/State";
import { clientController } from "../ClientController";
/**
* Prevent render until the client is ready to display.
* Also binds listeners from state to the current client.
*/
const Binder: React.FC = ({ children }) => {
const client = clientController.getReadyClient();
useEffect(() => state.registerListeners(client!), [client]);
// Block render if client is getting ready to work.
if (clientController.isLoggedIn() && !clientController.isReady()) {
return <Preloader type="spinner" />;
}
return <>{children}</>;
};
export default observer(Binder);