chore(refactor): remove SyncManager

This commit is contained in:
Paul Makles
2022-06-29 16:41:26 +01:00
parent 1fcb3cedc1
commit 45692999bf
5 changed files with 52 additions and 82 deletions

View File

@@ -1,8 +1,11 @@
// @ts-expect-error No typings.
import stringify from "json-stringify-deterministic";
import localforage from "localforage";
import { makeAutoObservable, reaction, runInAction } from "mobx";
import { Client } from "revolt.js";
import { action, makeAutoObservable, reaction, runInAction } from "mobx";
import { Client, ClientboundNotification } from "revolt.js";
import { reportError } from "../lib/ErrorBoundary";
import { injectWindow } from "../lib/window";
import { clientController } from "../controllers/client/ClientController";
import Persistent from "./interfaces/Persistent";
@@ -69,6 +72,10 @@ export default class State {
this.register();
this.setDisabled = this.setDisabled.bind(this);
this.onPacket = this.onPacket.bind(this);
// Inject into window
injectWindow("state", this);
}
/**
@@ -125,6 +132,20 @@ export default class State {
}
}
/**
* Consume packets from the client.
* @param packet Inbound Packet
*/
@action onPacket(packet: ClientboundNotification) {
if (packet.type === "UserSettingsUpdate") {
try {
this.sync.apply(packet.update);
} catch (err) {
reportError(err as any, "failed_sync_apply");
}
}
}
/**
* Register reaction listeners for persistent data stores.
* @returns Function to dispose of listeners
@@ -132,11 +153,17 @@ export default class State {
registerListeners(client?: Client) {
// If a client is present currently, expose it and provide it to plugins.
if (client) {
// this.client = client;
this.plugins.onClient(client);
// Register message listener for clearing queue.
// this.client.addListener("message", this.queue.onMessage);
client.addListener("message", this.queue.onMessage);
// Register listener for incoming packets.
client.addListener("packet", this.onPacket);
// Sync settings from remote server.
state.sync
.pull(client)
.catch(console.error)
.finally(() => state.changelog.checkForUpdates());
}
// Register all the listeners required for saving and syncing state.
@@ -222,14 +249,12 @@ export default class State {
});
return () => {
/*// Remove any listeners attached to client.
// Remove any listeners attached to client.
if (client) {
client.removeListener("message", this.queue.onMessage);
client.removeListener("packet", this.onPacket);
}
// Stop exposing the client.
this.client = undefined;*/
// Wipe all listeners.
listeners.forEach((x) => x());
};
@@ -282,13 +307,7 @@ export default class State {
}
}
export let state: State;
export async function hydrateState() {
state = new State();
(window as any).state = state;
await state.hydrate();
}
export const state = new State();
/**
* Get the application state

View File

@@ -41,7 +41,6 @@ type Plugin = {
* ```typescript
* function (state: State) {
* return {
* onClient: (client: Client) => {},
* onUnload: () => {}
* }
* }
@@ -59,7 +58,6 @@ type Plugin = {
type Instance = {
format: 1;
onClient?: (client: Client) => void;
onUnload?: () => void;
};
@@ -231,13 +229,4 @@ export default class Plugins implements Store, Persistent<Data> {
localforage.removeItem("revite:plugins");
window.location.reload();
}
/**
* Push client through to plugins
*/
onClient(client: Client) {
for (const instance of this.instances.values()) {
instance.onClient?.(client);
}
}
}