Files
discord-decomp/_bundle/Module590.js
T
2026-11-01 12:12:55 +02:00

259 lines
8.1 KiB
JavaScript

// Metro module 590 (f591).
import { Store } from "./Module591";
import { Storage } from "./Module595";
import Module10 from "./Module10";
import throttle from "./throttle";
let prototype, UserAgnosticStore;
const _excluded = ["_state", "_version"];
let c534_6 = {
_state: void 0,
_version: void 0
};
let c534_7 = null;
class PersistedStore extends Store {
static allPersistKeys = new Set();
static userAgnosticPersistKeys = new Set();
static _writePromises = new Map();
static _writeResolvers = new Map();
static disableWrites = false;
static disableWrite = false;
static throttleDelay = 0;
constructor(a0, a1, a2) {
super(a0, a1, a2);
let c12380_0 = this;
this._version = this.getClass().migrations == null ? 0 : this.getClass().migrations.length;
this.callback = (a0) => {
const persistKey2 = c12380_0.getClass().persistKey;
c12380_0.persist();
PersistedStore._writePromises.delete(persistKey2);
PersistedStore._writeResolvers.delete(persistKey2);
a0();
};
this.throttledCallback = throttle((a0) => c12380_0.callback(a0), this.getClass().throttleDelay, {
leading: false
});
if (typeof this.getClass().persistKey !== "string") {
throw new Error(`${this.getClass().name} initialized without a \`persistKey\`. Add one so we know where to save your stuff!`);
}
if (typeof this.initialize !== "function") {
throw new Error(`${this.getClass().name} initialized without an \`initialize\` method. Add one that accepts the initial cached state.`);
}
if (typeof this.getState !== "function") {
throw new Error(`${this.getClass().name} initialized without a \`getState\` method. Add one that returns the full state of the store for persistance to work.`);
}
this.addChangeListener(() => this.asyncPersist());
}
getClass() {
return this.constructor;
}
static clearAll(a0) {
c534_7 = a0;
if (PersistedStore._clearAllPromise == null) {
PersistedStore._clearAllPromise = new Promise((c23801_0) => {
requestIdleCallback(() => {
PersistedStore.clearPersistQueue(a0);
PersistedStore.allPersistKeys.forEach((allPersistKey) => {
if (PersistedStore.shouldClear(a0, allPersistKey)) {
Storage.remove(allPersistKey);
}
});
Store.getAll().forEach((item) => {
if (item instanceof PersistedStore && PersistedStore.shouldClear(a0, item.getClass().persistKey)) {
item._isInitialized = false;
item.initializeIfNeeded();
}
});
PersistedStore._clearAllPromise = null;
c23801_0();
}, {
timeout: 500
});
});
}
return PersistedStore._clearAllPromise;
}
static shouldClear(a0, a1) {
const omit = a0.omit;
if (omit == null ? void 0 : omit.includes(a1)) {
return false;
}
const type = a0.type;
if (type === "all") {
return true;
}
if (type === "user-data-only") {
return !PersistedStore.userAgnosticPersistKeys.has(a1);
}
a0.type;
return false;
}
static clearPersistQueue(c12382_0) {
PersistedStore._writeResolvers.forEach((_writeResolver, index) => {
const [r24, r25] = _writeResolver;
if (PersistedStore.shouldClear(c12382_0, index)) {
PersistedStore._writePromises.delete(index);
PersistedStore._writeResolvers.delete(index);
cancelIdleCallback(r25);
r24(false);
}
});
PersistedStore._writePromises.clear();
PersistedStore._writeResolvers.clear();
}
static getAllStates() {
return Promise.all(Array.from(PersistedStore._writePromises.values())).then(() => {
const r0 = {};
let c23802_0 = r0;
PersistedStore.allPersistKeys.forEach((allPersistKey) => {
let r12 = Storage.get(allPersistKey);
if (r12 == null) {
r12 = c534_6;
}
c23802_0[allPersistKey] = r12._state;
});
return r0;
});
}
static initializeAll(c12383_0) {
Store.getAll().forEach((item) => {
if (item instanceof PersistedStore) {
const persistKey2 = item.getClass().persistKey;
if (!c12383_0.hasOwnProperty(persistKey2)) {
return void 0;
}
item.initializeFromState(c12383_0[persistKey2]);
}
});
}
initializeFromState(a0) {
if (this.initialize(a0)) {
this.asyncPersist();
}
if (this._isInitialized) {
this.emitChange();
} else {
PersistedStore.allPersistKeys.add(this.getClass().persistKey);
this._isInitialized = true;
}
}
static destroy() {
c534_7 = null;
Store.destroy();
PersistedStore.clearPersistQueue({
type: "all"
});
PersistedStore.allPersistKeys.clear();
PersistedStore.userAgnosticPersistKeys.clear();
}
initializeIfNeeded() {
if (!this._isInitialized) {
const r11 = Date.now();
PersistedStore.allPersistKeys.add(this.getClass().persistKey);
const r23 = PersistedStore.migrateAndReadStoreState(this.getClass().persistKey, this.getClass().migrations);
const { state, requiresPersist } = r23;
if (this.initialize(state)) {
this.asyncPersist();
}
if (requiresPersist) {
this.asyncPersist();
}
this._isInitialized = true;
const r35 = Date.now() - r11;
if (r35 > 5) {
Module10.mark("🦥", this.getName() + ".initialize()", r35);
}
}
}
static migrateAndReadStoreState(a0, a1) {
if (c534_7 != null && PersistedStore.shouldClear(c534_7, a0)) {
Storage.remove(a0);
return {
state: void 0,
requiresPersist: false
};
}
let r9 = PersistedStore._clearAllPromise != null ? null : Storage.get(a0);
if (r9 == null) {
r9 = c534_6;
}
let _state = r9._state;
const { _version, ...rest } = r9;
const r29 = a1 == null ? 0 : a1.length;
if (r29 !== 0 && _version !== r29 && a1 != null) {
let r36 = _version;
if (r36 == null) {
r36 = 0;
}
if (_version == null) {
_state = rest;
}
let r37 = _state;
let state = r37;
while (r36 < r29) {
r37 = (0, a1[r36])(r37);
r36 = r36 + 1;
state = r37;
}
return {
state,
requiresPersist: true
};
}
return Object.values(rest).length > 0 ? {
state: rest,
requiresPersist: true
} : {
state: _state,
requiresPersist: false
};
}
asyncPersist() {
const class2 = this.getClass();
const { persistKey, disableWrite, throttleDelay } = class2;
if (!PersistedStore.disableWrites && !disableWrite) {
let r17 = PersistedStore._writePromises.get(persistKey);
if (r17 == null) {
const promise = new Promise((resolve) => {
PersistedStore._writeResolvers.set(persistKey, [resolve, requestIdleCallback(throttleDelay > 0 ? () => this.throttledCallback(resolve) : () => this.callback(resolve), {
timeout: 500
})]);
});
PersistedStore._writePromises.set(persistKey, promise);
r17 = promise;
}
return r17;
}
return Promise.resolve(false);
}
persist() {
Storage.set(this.getClass().persistKey, {
_state: this.getState(),
_version: this._version
});
}
clear() {
Storage.remove(this.getClass().persistKey);
}
}
const UserAgnosticStore2 = class UserAgnosticStore extends PersistedStore {
initializeFromState(a0) {
PersistedStore.userAgnosticPersistKeys.add(this.getClass().persistKey);
return Reflect.get(Object.getPrototypeOf(prototype), "initializeFromState", this).call(this, a0);
}
initializeIfNeeded() {
PersistedStore.userAgnosticPersistKeys.add(this.getClass().persistKey);
return Reflect.get(Object.getPrototypeOf(prototype), "initializeIfNeeded", this).call(this);
}
getState() {
return this.getUserAgnosticState();
}
};
const prototype2 = UserAgnosticStore2.prototype;
UserAgnosticStore = UserAgnosticStore2;
prototype = prototype2;
export { PersistedStore };
class DeviceSettingsStore extends UserAgnosticStore {}
export { DeviceSettingsStore };
class OfflineCacheStore extends UserAgnosticStore {}
export { OfflineCacheStore };