瀏覽代碼

ref: update userutils

Sv443 1 年之前
父節點
當前提交
f6b90a111a
共有 5 個文件被更改,包括 28 次插入28 次删除
  1. 4 4
      package-lock.json
  2. 1 1
      package.json
  3. 16 16
      src/config.ts
  4. 5 5
      src/features/lyricsCache.ts
  5. 2 2
      src/menu/menu_old.ts

+ 4 - 4
package-lock.json

@@ -9,7 +9,7 @@
       "version": "1.1.1",
       "license": "AGPL-3.0-only",
       "dependencies": {
-        "@sv443-network/userutils": "^5.0.1",
+        "@sv443-network/userutils": "^6.0.0",
         "fuse.js": "^7.0.0",
         "marked": "^12.0.0",
         "nanoevents": "^9.0.0"
@@ -600,9 +600,9 @@
       ]
     },
     "node_modules/@sv443-network/userutils": {
-      "version": "5.0.1",
-      "resolved": "https://registry.npmjs.org/@sv443-network/userutils/-/userutils-5.0.1.tgz",
-      "integrity": "sha512-RvLjRmn9NsFQ+i1sSz5L61lfBQXuBkxePZ7u4YCin4zoB5jvKmZfHZ8DYkPYppuwie5Djbw0CP+Wvg3ObjHK2A=="
+      "version": "6.0.0",
+      "resolved": "https://registry.npmjs.org/@sv443-network/userutils/-/userutils-6.0.0.tgz",
+      "integrity": "sha512-7+sN92zRTZASs73ZThUx3CLICbrO2sg8r0sO9SK5ipyYcrUt0SmJ3/CWpP1W9MrYRkU35spRKeFeeOlnOqn75g=="
     },
     "node_modules/@tsconfig/node10": {
       "version": "1.0.9",

+ 1 - 1
package.json

@@ -57,7 +57,7 @@
     "openuserjs": "https://openuserjs.org/scripts/Sv443/BetterYTM"
   },
   "dependencies": {
-    "@sv443-network/userutils": "^5.0.1",
+    "@sv443-network/userutils": "^6.0.0",
     "fuse.js": "^7.0.0",
     "marked": "^12.0.0",
     "nanoevents": "^9.0.0"

+ 16 - 16
src/config.ts

@@ -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");
 }

+ 5 - 5
src/features/lyricsCache.ts

@@ -1,4 +1,4 @@
-import { ConfigManager, clamp, compress, decompress } from "@sv443-network/userutils";
+import { DataStore, clamp, compress, decompress } from "@sv443-network/userutils";
 import { compressionFormat } from "../constants";
 import { compressionSupported, log } from "../utils";
 import { emitInterface } from "../interface";
@@ -16,9 +16,9 @@ const maxAddedPenalty = 1000 * 60 * 60 * 24 * 15; // 15 days
 
 let canCompress = true;
 
-const lyricsCacheMgr = new ConfigManager<LyricsCache>({
+const lyricsCacheMgr = new DataStore<LyricsCache>({
   id: "bytm-lyrics-cache",
-  defaultConfig: {
+  defaultData: {
     cache: [],
   },
   formatVersion: 1,
@@ -76,9 +76,9 @@ function deleteLyricsCacheEntry(artist: string, song: string) {
 }
 
 /** Clears the lyrics cache locally and deletes it from persistent storage - the window should be reloaded right after! */
-export function deleteLyricsCache() {
+export async function deleteLyricsCache() {
+  await lyricsCacheMgr.deleteData();
   emitInterface("bytm:lyricsCacheCleared");
-  return lyricsCacheMgr.deleteConfig();
 }
 
 /** Clears the lyrics cache locally and clears it in persistent storage */

+ 2 - 2
src/menu/menu_old.ts

@@ -1,5 +1,5 @@
 import { compress, decompress, debounce, isScrollable } from "@sv443-network/userutils";
-import { defaultConfig, getFeatures, migrations, saveFeatures, setDefaultFeatures } from "../config";
+import { defaultData, getFeatures, migrations, saveFeatures, setDefaultFeatures } from "../config";
 import { buildNumber, compressionFormat, host, mode, scriptInfo } from "../constants";
 import { featInfo, disableBeforeUnload } from "../features/index";
 import { error, getResourceUrl, info, log, resourceToHTMLString, warn, getLocale, hasKey, initTranslations, setLocale, t, compressionSupported, getChangelogHtmlWithDetails, arrayWithSeparators, tp, type TrKey, onInteraction } from "../utils";
@@ -213,7 +213,7 @@ async function addCfgMenu() {
   featuresCont.id = "bytm-menu-opts";
 
   /** Gets called whenever the feature config is changed */
-  const confChanged = debounce(async (key: keyof typeof defaultConfig, initialVal: number | boolean | Record<string, unknown>, newVal: number | boolean | Record<string, unknown>) => {
+  const confChanged = debounce(async (key: keyof typeof defaultData, initialVal: number | boolean | Record<string, unknown>, newVal: number | boolean | Record<string, unknown>) => {
     const fmt = (val: unknown) => typeof val === "object" ? JSON.stringify(val) : String(val);
     info(`Feature config changed at key '${key}', from value '${fmt(initialVal)}' to '${fmt(newVal)}'`);