|
@@ -1,4 +1,4 @@
|
|
|
-import { ConfigManager, compress, type ConfigMigrationsDict, decompress } from "@sv443-network/userutils";
|
|
|
+import { DataStore, compress, type ConfigMigrationsDict, decompress } from "@sv443-network/userutils";
|
|
|
import { featInfo } from "./features/index";
|
|
|
import { compressionSupported, info, log } from "./utils";
|
|
|
import { emitSiteEvent } from "./siteEvents";
|
|
@@ -69,7 +69,7 @@ function getFeatureDefault<TKey extends keyof typeof featInfo>(key: TKey): typeo
|
|
|
return featInfo[key].default;
|
|
|
}
|
|
|
|
|
|
-export const defaultConfig = (Object.keys(featInfo) as (keyof typeof featInfo)[])
|
|
|
+export const defaultData = (Object.keys(featInfo) as (keyof typeof featInfo)[])
|
|
|
.reduce<Partial<FeatureConfig>>((acc, key) => {
|
|
|
acc[key] = featInfo[key].default as unknown as undefined;
|
|
|
return acc;
|
|
@@ -77,25 +77,25 @@ export const defaultConfig = (Object.keys(featInfo) as (keyof typeof featInfo)[]
|
|
|
|
|
|
let canCompress = true;
|
|
|
|
|
|
-const cfgMgr = new ConfigManager({
|
|
|
+const bytmCfgStore = new DataStore({
|
|
|
id: "bytm-config",
|
|
|
formatVersion,
|
|
|
- defaultConfig,
|
|
|
+ defaultData,
|
|
|
migrations,
|
|
|
encodeData: (data) => canCompress ? compress(data, compressionFormat, "string") : data,
|
|
|
decodeData: (data) => canCompress ? decompress(data, compressionFormat, "string") : data,
|
|
|
});
|
|
|
|
|
|
-/** Initializes the ConfigManager instance and loads persistent data into memory */
|
|
|
+/** Initializes the DataStore instance and loads persistent data into memory */
|
|
|
export async function initConfig() {
|
|
|
canCompress = await compressionSupported();
|
|
|
- const oldFmtVer = Number(await GM.getValue(`_uucfgver-${cfgMgr.id}`, NaN));
|
|
|
- const data = await cfgMgr.loadData();
|
|
|
- log(`Initialized ConfigManager (format version = ${cfgMgr.formatVersion})`);
|
|
|
+ const oldFmtVer = Number(await GM.getValue(`_uucfgver-${bytmCfgStore.id}`, NaN));
|
|
|
+ const data = await bytmCfgStore.loadData();
|
|
|
+ log(`Initialized DataStore (format version = ${bytmCfgStore.formatVersion})`);
|
|
|
if(isNaN(oldFmtVer))
|
|
|
info("Config data initialized with default values");
|
|
|
- else if(oldFmtVer !== cfgMgr.formatVersion)
|
|
|
- info(`Config data migrated from version ${oldFmtVer} to ${cfgMgr.formatVersion}`);
|
|
|
+ else if(oldFmtVer !== bytmCfgStore.formatVersion)
|
|
|
+ info(`Config data migrated from version ${oldFmtVer} to ${bytmCfgStore.formatVersion}`);
|
|
|
|
|
|
emitInterface("bytm:configReady", getFeaturesInterface());
|
|
|
|
|
@@ -104,27 +104,27 @@ export async function initConfig() {
|
|
|
|
|
|
/** Returns the current feature config from the in-memory cache as a copy */
|
|
|
export function getFeatures() {
|
|
|
- return cfgMgr.getData();
|
|
|
+ return bytmCfgStore.getData();
|
|
|
}
|
|
|
|
|
|
/** Saves the feature config synchronously to the in-memory cache and asynchronously to the persistent storage */
|
|
|
export function saveFeatures(featureConf: FeatureConfig) {
|
|
|
- const res = cfgMgr.setData(featureConf);
|
|
|
- emitSiteEvent("configChanged", cfgMgr.getData());
|
|
|
+ const res = bytmCfgStore.setData(featureConf);
|
|
|
+ emitSiteEvent("configChanged", bytmCfgStore.getData());
|
|
|
info("Saved new feature config:", featureConf);
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
/** Saves the default feature config synchronously to the in-memory cache and asynchronously to persistent storage */
|
|
|
export function setDefaultFeatures() {
|
|
|
- const res = cfgMgr.saveDefaultData();
|
|
|
- emitSiteEvent("configChanged", cfgMgr.getData());
|
|
|
+ const res = bytmCfgStore.saveDefaultData();
|
|
|
+ emitSiteEvent("configChanged", bytmCfgStore.getData());
|
|
|
info("Reset feature config to its default values");
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
/** Clears the feature config from the persistent storage - since the cache will be out of whack, this should only be run before a site re-/unload */
|
|
|
export async function clearConfig() {
|
|
|
- await cfgMgr.deleteConfig();
|
|
|
+ await bytmCfgStore.deleteData();
|
|
|
info("Deleted config from persistent storage");
|
|
|
}
|