Răsfoiți Sursa

ref: update userutils

Sv443 8 luni în urmă
părinte
comite
9adcac8c11

+ 1 - 1
package.json

@@ -64,7 +64,7 @@
     "openuserjs": "https://openuserjs.org/scripts/Sv443/BetterYTM"
   },
   "dependencies": {
-    "@sv443-network/userutils": "^7.0.1",
+    "@sv443-network/userutils": "^7.1.0",
     "compare-versions": "^6.1.0",
     "marked": "^12.0.2",
     "nanoevents": "^9.0.0"

+ 7 - 5
pnpm-lock.yaml

@@ -9,8 +9,8 @@ importers:
   .:
     dependencies:
       '@sv443-network/userutils':
-        specifier: ^7.0.1
-        version: 7.0.1
+        specifier: ^7.1.0
+        version: 7.1.0
       compare-versions:
         specifier: ^6.1.0
         version: 6.1.0
@@ -1669,8 +1669,8 @@ packages:
   '@storybook/[email protected]':
     resolution: {integrity: sha512-UJ97iqI+0Mk13I6ayd3TaBfSFBkWnEauwTnFMQe1dN/L3wTh8laOBaLa0Vr3utRSnt2b5hpcw/nq7azB/Gx4Yw==}
 
-  '@sv443-network/userutils@7.0.1':
-    resolution: {integrity: sha512-qPDJAl2eY3rtXeFo4qBMf2DdO++eMiWGVTvtdm3lZTHNxZSqJRC/zUxBaNsc4GMf1gXZQ7hadkUmdBn1p2eFjg==}
+  '@sv443-network/[email protected].0':
+    resolution: {integrity: sha512-VhiuiKLMsEuT6tlVU+kDmJ+uA2IbHIoN6dNJE7NTcX92YcfacGkVcdlU6QqOBuRnvn2QELeH9MM/ZQp8vYpK1w==}
 
   '@testing-library/[email protected]':
     resolution: {integrity: sha512-FlS4ZWlp97iiNWig0Muq8p+3rVDjRiYE+YKGbAqXOu9nwJFFOdL00kFpz42M+4huzYi86vAK1sOOfyOG45muIQ==}
@@ -6600,7 +6600,9 @@ snapshots:
       '@types/express': 4.17.21
       file-system-cache: 2.3.0
 
-  '@sv443-network/[email protected]': {}
+  '@sv443-network/[email protected]':
+    dependencies:
+      nanoevents: 9.0.0
 
   '@testing-library/[email protected]':
     dependencies:

+ 1 - 1
src/components/BytmDialog.ts

@@ -1,5 +1,5 @@
 // hoist the class declaration because either rollup or babel is being a hoe
-import { NanoEmitter } from "../utils/NanoEmitter.js";
+import { NanoEmitter } from "@sv443-network/userutils";
 import { clearInner, getDomain, getResourceUrl, onInteraction, warn } from "../utils/index.js";
 import { t } from "../utils/translations.js";
 import { emitInterface } from "../interface.js";

+ 6 - 4
src/index.ts

@@ -265,7 +265,7 @@ async function onDomLoad() {
     info(`Done initializing all ${ftInit.length} features after ${Math.floor(Date.now() - initStartTs)}ms`);
 
     try {
-      registerDevMenuCommands();
+      registerDevCommands();
     }
     catch(e) {
       warn("Couldn't register dev menu commands:", e);
@@ -301,7 +301,7 @@ function initGlobalCssVars() {
 //#region dev menu cmds
 
 /** Registers dev commands using `GM.registerMenuCommand` */
-function registerDevMenuCommands() {
+function registerDevCommands() {
   if(mode !== "development")
     return;
 
@@ -433,13 +433,13 @@ function registerDevMenuCommands() {
     }
   });
 
-  GM.registerMenuCommand("Export data using DataStoreSerializer", async () => {
+  GM.registerMenuCommand("Export using DataStoreSerializer", async () => {
     const ser = await storeSerializer.serialize();
     dbg("Serialized data stores:", JSON.stringify(JSON.parse(ser)));
     alert("See console.");
   });
 
-  GM.registerMenuCommand("Import data using DataStoreSerializer", async () => {
+  GM.registerMenuCommand("Import using DataStoreSerializer", async () => {
     const input = prompt("Enter the serialized data to import:");
     if(input && input.length > 0) {
       await storeSerializer.deserialize(input);
@@ -447,6 +447,8 @@ function registerDevMenuCommands() {
     }
   });
 
+  GM.registerMenuCommand("Throw Error", () => error("Test error thrown by user command:", new Error("Test error")));
+
   log("Registered dev menu commands");
 }
 

+ 2 - 2
src/interface.ts

@@ -1,7 +1,7 @@
 import * as UserUtils from "@sv443-network/userutils";
 import * as compareVersions from "compare-versions";
 import { mode, branch, host, buildNumber, compressionFormat, scriptInfo } from "./constants.js";
-import { getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, NanoEmitter, t, tp, type TrLocale, info, error, onInteraction, getThumbnailUrl, getBestThumbnailUrl, fetchVideoVotes } from "./utils/index.js";
+import { getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, t, tp, type TrLocale, info, error, onInteraction, getThumbnailUrl, getBestThumbnailUrl, fetchVideoVotes } from "./utils/index.js";
 import { addSelectorListener } from "./observers.js";
 import { getFeatures, setFeatures } from "./config.js";
 import { autoLikeStore, featInfo, fetchLyricsUrlTop, getLyricsCacheEntry, sanitizeArtists, sanitizeSong } from "./features/index.js";
@@ -9,7 +9,7 @@ import { allSiteEvents, type SiteEventsMap } from "./siteEvents.js";
 import { LogLevel, type FeatureConfig, type FeatureInfo, type LyricsCacheEntry, type PluginDef, type PluginInfo, type PluginRegisterResult, type PluginDefResolvable, type PluginEventMap, type PluginItem, type BytmObject, type AutoLikeData, type InterfaceFunctions } from "./types.js";
 import { BytmDialog, ExImDialog, createCircularBtn, createHotkeyInput, createRipple, createToggleInput, showIconToast, showToast } from "./components/index.js";
 
-const { getUnsafeWindow, randomId } = UserUtils;
+const { getUnsafeWindow, randomId, NanoEmitter } = UserUtils;
 
 //#region interface globals
 

+ 2 - 1
src/siteEvents.ts

@@ -1,4 +1,5 @@
-import { NanoEmitter, error, getDomain, info } from "./utils/index.js";
+import { NanoEmitter } from "@sv443-network/userutils";
+import { error, getDomain, info } from "./utils/index.js";
 import { FeatureConfig } from "./types.js";
 import { emitInterface } from "./interface.js";
 import { addSelectorListener, globserversReady } from "./observers.js";

+ 2 - 1
src/types.ts

@@ -1,9 +1,10 @@
+import { NanoEmitter } from "@sv443-network/userutils";
 import type * as consts from "./constants.js";
 import type { scriptInfo } from "./constants.js";
 import type { addSelectorListener } from "./observers.js";
 import type resources from "../assets/resources.json";
 import type locales from "../assets/locales.json";
-import type { getResourceUrl, getSessionId, getVideoTime, TrLocale, t, tp, NanoEmitter, fetchVideoVotes, onInteraction, getThumbnailUrl, getBestThumbnailUrl, getLocale, hasKey, hasKeyFor } from "./utils/index.js";
+import type { getResourceUrl, getSessionId, getVideoTime, TrLocale, t, tp, fetchVideoVotes, onInteraction, getThumbnailUrl, getBestThumbnailUrl, getLocale, hasKey, hasKeyFor } from "./utils/index.js";
 import type { SiteEventsMap } from "./siteEvents.js";
 import type { InterfaceEventsMap, getAutoLikeDataInterface, getFeaturesInterface, getPluginInfo, registerPlugin, saveAutoLikeDataInterface, saveFeaturesInterface, setLocaleInterface } from "./interface.js";
 import type { BytmDialog, ExImDialog, createCircularBtn, createHotkeyInput, createRipple, createToggleInput, showIconToast, showToast } from "./components/index.js";

+ 0 - 70
src/utils/NanoEmitter.ts

@@ -1,70 +0,0 @@
-import { createNanoEvents, type DefaultEvents, type EventsMap, type Unsubscribe } from "nanoevents";
-
-export interface NanoEmitterOptions {
-  /** If set to true, allows emitting events through the public method emit() */
-  publicEmit: boolean;
-}
-
-/** Class that can be extended or instantiated by itself to create an event emitter with helper methods and a strongly typed event map */
-export class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
-  protected readonly events = createNanoEvents<TEvtMap>();
-  protected eventUnsubscribes: Unsubscribe[] = [];
-  protected emitterOptions: NanoEmitterOptions;
-
-  constructor(options: Partial<NanoEmitterOptions> = {}) {
-    this.emitterOptions = {
-      publicEmit: false,
-      ...options,
-    };
-  }
-
-  /** Subscribes to an event - returns a function that unsubscribes the event listener */
-  public on<TKey extends keyof TEvtMap>(event: TKey | "_", cb: TEvtMap[TKey]) {
-    // eslint-disable-next-line prefer-const
-    let unsub: Unsubscribe | undefined;
-
-    const unsubProxy = () => {
-      if(!unsub)
-        return;
-      unsub();
-      this.eventUnsubscribes = this.eventUnsubscribes.filter(u => u !== unsub);
-    };
-
-    unsub = this.events.on(event, cb);
-
-    this.eventUnsubscribes.push(unsub);
-    return unsubProxy;
-  }
-
-  /** Subscribes to an event and calls the callback or resolves the Promise only once */
-  public once<TKey extends keyof TEvtMap>(event: TKey | "_", cb?: TEvtMap[TKey]): Promise<Parameters<TEvtMap[TKey]>> {
-    return new Promise((resolve) => {
-      // eslint-disable-next-line prefer-const
-      let unsub: Unsubscribe | undefined;
-
-      const onceProxy = ((...args: Parameters<TEvtMap[TKey]>) => {
-        unsub!();
-        cb?.(...args);
-        resolve(args);
-      }) as TEvtMap[TKey];
-
-      unsub = this.on(event, onceProxy);
-    });
-  }
-
-  /** Emits an event on this instance - Needs `publicEmit` to be set to true in the constructor! */
-  public emit<TKey extends keyof TEvtMap>(event: TKey, ...args: Parameters<TEvtMap[TKey]>) {
-    if(this.emitterOptions.publicEmit) {
-      this.events.emit(event, ...args);
-      return true;
-    }
-    return false;
-  }
-
-  /** Unsubscribes all event listeners */
-  public unsubscribeAll() {
-    for(const unsub of this.eventUnsubscribes)
-      unsub();
-    this.eventUnsubscribes = [];
-  }
-}

+ 0 - 1
src/utils/index.ts

@@ -2,6 +2,5 @@ export * from "./dom.js";
 export * from "./input.js";
 export * from "./logging.js";
 export * from "./misc.js";
-export * from "./NanoEmitter.js";
 export * from "./translations.js";
 export * from "./xhr.js";

+ 0 - 2
src/utils/logging.ts

@@ -68,8 +68,6 @@ export function error(...args: unknown[]): void {
     });
 }
 
-document.addEventListener("DOMContentLoaded", () => error("Ayo chungus", new TypeError("chungus moment encountered")));
-
 /** Logs all passed values to the console with a debug-specific prefix */
 export function dbg(...args: unknown[]): void {
   console.log(consPrefixDbg, ...args);