mirror of
https://github.com/stoatchat/for-legacy-web.git
synced 2026-03-07 09:25:27 +00:00
feat(mobx): add sync back (do not look at the code)
This commit is contained in:
@@ -4,11 +4,14 @@ import {
|
||||
makeAutoObservable,
|
||||
ObservableMap,
|
||||
ObservableSet,
|
||||
runInAction,
|
||||
} from "mobx";
|
||||
import { Client } from "revolt.js";
|
||||
import { UserSettings } from "revolt.js/node_modules/revolt-api/types/Sync";
|
||||
|
||||
import { mapToRecord } from "../../lib/conversion";
|
||||
|
||||
import State from "../State";
|
||||
import Persistent from "../interfaces/Persistent";
|
||||
import Store from "../interfaces/Store";
|
||||
|
||||
@@ -32,13 +35,15 @@ export interface Data {
|
||||
* Handles syncing settings data.
|
||||
*/
|
||||
export default class Sync implements Store, Persistent<Data> {
|
||||
private state: State;
|
||||
private disabled: ObservableSet<SyncKeys>;
|
||||
private revision: ObservableMap<SyncKeys, number>;
|
||||
private revision: ObservableMap<string, number>;
|
||||
|
||||
/**
|
||||
* Construct new Sync store.
|
||||
*/
|
||||
constructor() {
|
||||
constructor(state: State) {
|
||||
this.state = state;
|
||||
this.disabled = new ObservableSet();
|
||||
this.revision = new ObservableMap();
|
||||
makeAutoObservable(this);
|
||||
@@ -62,6 +67,12 @@ export default class Sync implements Store, Persistent<Data> {
|
||||
this.disabled.add(key as SyncKeys);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.revision) {
|
||||
for (const key of Object.keys(data.revision)) {
|
||||
this.setRevision(key, data.revision[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@action enable(key: SyncKeys) {
|
||||
@@ -81,12 +92,74 @@ export default class Sync implements Store, Persistent<Data> {
|
||||
}
|
||||
|
||||
@computed isEnabled(key: SyncKeys) {
|
||||
return !this.disabled.has(key);
|
||||
return !this.disabled.has(key) && SYNC_KEYS.includes(key);
|
||||
}
|
||||
|
||||
@action setRevision(key: string, revision: number) {
|
||||
if (revision < (this.getRevision(key) ?? 0)) return;
|
||||
this.revision.set(key, revision);
|
||||
}
|
||||
|
||||
@computed getRevision(key: string) {
|
||||
return this.revision.get(key);
|
||||
}
|
||||
|
||||
@action apply(data: UserSettings) {
|
||||
const tryRead = (key: string) => {
|
||||
if (key in data) {
|
||||
const revision = data[key][0];
|
||||
if (revision <= (this.getRevision(key) ?? 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let parsed;
|
||||
try {
|
||||
parsed = JSON.parse(data[key][1]);
|
||||
} catch (err) {
|
||||
parsed = data[key][1];
|
||||
}
|
||||
|
||||
return [revision, parsed];
|
||||
}
|
||||
};
|
||||
|
||||
runInAction(() => {
|
||||
const appearance = tryRead("appearance");
|
||||
if (appearance) {
|
||||
this.state.setDisabled("appearance");
|
||||
this.state.settings.apply(
|
||||
"appearance",
|
||||
appearance[1],
|
||||
appearance[0],
|
||||
);
|
||||
this.setRevision("appearance", appearance[0]);
|
||||
}
|
||||
|
||||
const theme = tryRead("theme");
|
||||
if (theme) {
|
||||
this.state.setDisabled("theme");
|
||||
this.state.settings.apply("theme", theme[1], theme[0]);
|
||||
this.setRevision("theme", theme[0]);
|
||||
}
|
||||
|
||||
const notifications = tryRead("notifications");
|
||||
if (notifications) {
|
||||
this.state.setDisabled("notifications");
|
||||
this.state.notifications.apply(
|
||||
"notifications",
|
||||
notifications[1],
|
||||
notifications[0],
|
||||
);
|
||||
this.setRevision("notifications", notifications[0]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async pull(client: Client) {
|
||||
const data = await client.syncFetchSettings(
|
||||
SYNC_KEYS.filter(this.isEnabled),
|
||||
);
|
||||
|
||||
this.apply(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user