mirror of
https://github.com/revoltchat/revite.git
synced 2024-11-29 02:10:59 -05:00
fix(mobx): properly persist login state
This commit is contained in:
parent
ef1ddb7771
commit
189d0b5ff9
5 changed files with 12 additions and 4 deletions
|
@ -11,6 +11,7 @@ import Locale from "./Locale";
|
||||||
import Theme from "./Theme";
|
import Theme from "./Theme";
|
||||||
import Intermediate from "./intermediate/Intermediate";
|
import Intermediate from "./intermediate/Intermediate";
|
||||||
import Client from "./revoltjs/RevoltClient";
|
import Client from "./revoltjs/RevoltClient";
|
||||||
|
import SyncManager from "./revoltjs/SyncManager";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This component provides all of the application's context layers.
|
* This component provides all of the application's context layers.
|
||||||
|
@ -29,7 +30,10 @@ export default function Context({ children }: { children: Children }) {
|
||||||
<Router basename={import.meta.env.BASE_URL}>
|
<Router basename={import.meta.env.BASE_URL}>
|
||||||
<Locale>
|
<Locale>
|
||||||
<Intermediate>
|
<Intermediate>
|
||||||
<Client>{children}</Client>
|
<Client>
|
||||||
|
{children}
|
||||||
|
<SyncManager />
|
||||||
|
</Client>
|
||||||
</Intermediate>
|
</Intermediate>
|
||||||
</Locale>
|
</Locale>
|
||||||
<Theme />
|
<Theme />
|
||||||
|
|
|
@ -15,7 +15,9 @@ export default function SyncManager() {
|
||||||
|
|
||||||
// Sync settings from Revolt.
|
// Sync settings from Revolt.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
state.sync.pull(client);
|
if (client) {
|
||||||
|
state.sync.pull(client);
|
||||||
|
}
|
||||||
}, [client]);
|
}, [client]);
|
||||||
|
|
||||||
// Keep data synced.
|
// Keep data synced.
|
||||||
|
@ -23,6 +25,7 @@ export default function SyncManager() {
|
||||||
|
|
||||||
// Take data updates from Revolt.
|
// Take data updates from Revolt.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!client) return;
|
||||||
function onPacket(packet: ClientboundNotification) {
|
function onPacket(packet: ClientboundNotification) {
|
||||||
if (packet.type === "UserSettingsUpdate") {
|
if (packet.type === "UserSettingsUpdate") {
|
||||||
state.sync.apply(packet.update);
|
state.sync.apply(packet.update);
|
||||||
|
|
|
@ -112,7 +112,7 @@ export default class State {
|
||||||
* Register reaction listeners for persistent data stores.
|
* Register reaction listeners for persistent data stores.
|
||||||
* @returns Function to dispose of listeners
|
* @returns Function to dispose of listeners
|
||||||
*/
|
*/
|
||||||
registerListeners(client: Client) {
|
registerListeners(client?: Client) {
|
||||||
const listeners = this.persistent.map(([id, store]) => {
|
const listeners = this.persistent.map(([id, store]) => {
|
||||||
return reaction(
|
return reaction(
|
||||||
() => stringify(store.toJSON()),
|
() => stringify(store.toJSON()),
|
||||||
|
@ -120,6 +120,7 @@ export default class State {
|
||||||
try {
|
try {
|
||||||
await localforage.setItem(id, JSON.parse(value));
|
await localforage.setItem(id, JSON.parse(value));
|
||||||
if (id === "sync") return;
|
if (id === "sync") return;
|
||||||
|
if (!client) return;
|
||||||
|
|
||||||
const revision = +new Date();
|
const revision = +new Date();
|
||||||
switch (id) {
|
switch (id) {
|
||||||
|
|
|
@ -85,6 +85,7 @@ export default class Auth implements Store, Persistent<Data> {
|
||||||
@action setSession(session: Session) {
|
@action setSession(session: Session) {
|
||||||
this.sessions.set(session.user_id, { session });
|
this.sessions.set(session.user_id, { session });
|
||||||
this.current = session.user_id;
|
this.current = session.user_id;
|
||||||
|
console.log(this.sessions, this.current);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -129,7 +129,6 @@ export default function App() {
|
||||||
<Popovers />
|
<Popovers />
|
||||||
<Notifications />
|
<Notifications />
|
||||||
<StateMonitor />
|
<StateMonitor />
|
||||||
<SyncManager />
|
|
||||||
</OverlappingPanels>
|
</OverlappingPanels>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in a new issue