Browse Source

ref: comments & readme badges

Sv443 3 weeks ago
parent
commit
6c131cb010
4 changed files with 75 additions and 22 deletions
  1. 8 8
      README-summary.md
  2. 8 8
      README.md
  3. 6 1
      lib/Mixins.ts
  4. 53 5
      lib/NanoEmitter.ts

+ 8 - 8
README-summary.md

@@ -1,6 +1,6 @@
 ## UserUtils
 General purpose DOM/GreaseMonkey library that allows you to register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and much more.  
-Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via `@require`  
+Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via `@require` or `<script>`  
 The library works in any DOM environment with or without the [GreaseMonkey API](https://wiki.greasespot.net/Greasemonkey_Manual:API), but some features will be unavailable or limited.  
   
 You may want to check out my [template for userscripts in TypeScript](https://github.com/Sv443/Userscript.ts) that you can use to get started quickly. It also includes this library by default.  
@@ -9,14 +9,14 @@ If you like using this library, please consider [supporting the development ❤
 
 <br>
 
-[![tree shaking support badge](https://badgen.net/bundlephobia/tree-shaking/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
-[![minified bundle size badge](https://badgen.net/bundlephobia/min/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
-[![minified and gzipped bundle size badge](https://badgen.net/bundlephobia/minzip/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
-![code coverage percentage](https://img.shields.io/coverallsCoverage/github/Sv443-Network/UserUtils?branch=main)
+[![Tree shaking support badge](https://badgen.net/bundlephobia/tree-shaking/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
+[![Code coverage percentage badge](https://img.shields.io/coverallsCoverage/github/Sv443-Network/UserUtils?branch=main)](https://coveralls.io/github/Sv443-Network/UserUtils)
+[![Minified bundle size badge](https://badgen.net/bundlephobia/min/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
+[![Minified and gzipped bundle size badge](https://badgen.net/bundlephobia/minzip/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
 
-[![github stargazers badge](https://badgen.net/github/stars/Sv443-Network/UserUtils?icon=github)](https://github.com/Sv443-Network/UserUtils/stargazers)
-[![discord server badge](https://badgen.net/discord/online-members/aBH4uRG?icon=discord)](https://dc.sv443.net/)
-[![Support development on Github Sponsors](https://img.shields.io/github/sponsors/Sv443)](https://github.com/sponsors/Sv443)
+[![Discord server badge](https://badgen.net/discord/online-members/aBH4uRG?icon=discord)](https://dc.sv443.net/)
+[![Github stargazers badge](https://badgen.net/github/stars/Sv443-Network/UserUtils?icon=github)](https://github.com/Sv443-Network/UserUtils/stargazers)
+[![Support development on Github Sponsors badge](https://img.shields.io/github/sponsors/Sv443?icon=github)](https://github.com/sponsors/Sv443)
 
 <br>
 

+ 8 - 8
README.md

@@ -3,7 +3,7 @@
 <!-- #region Description -->
 ## UserUtils
 General purpose DOM/GreaseMonkey library that allows you to register listeners for when CSS selectors exist, intercept events, create persistent & synchronous data stores, modify the DOM more easily and much more.  
-Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via `@require`  
+Contains builtin TypeScript declarations. Supports ESM and CJS imports via a bundler and global declaration via `@require` or `<script>`  
 The library works in any DOM environment with or without the [GreaseMonkey API](https://wiki.greasespot.net/Greasemonkey_Manual:API), but some features will be unavailable or limited.  
   
 You may want to check out my [template for userscripts in TypeScript](https://github.com/Sv443/Userscript.ts) that you can use to get started quickly. It also includes this library by default.  
@@ -11,14 +11,14 @@ If you like using this library, please consider [supporting the development ❤
 
 <br>
 
-[![tree shaking support badge](https://badgen.net/bundlephobia/tree-shaking/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
-[![minified bundle size badge](https://badgen.net/bundlephobia/min/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
-[![minified and gzipped bundle size badge](https://badgen.net/bundlephobia/minzip/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
-![code coverage percentage](https://img.shields.io/coverallsCoverage/github/Sv443-Network/UserUtils?branch=main)
+[![Tree shaking support badge](https://badgen.net/bundlephobia/tree-shaking/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
+[![Code coverage percentage badge](https://img.shields.io/coverallsCoverage/github/Sv443-Network/UserUtils?branch=main)](https://coveralls.io/github/Sv443-Network/UserUtils)
+[![Minified bundle size badge](https://badgen.net/bundlephobia/min/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
+[![Minified and gzipped bundle size badge](https://badgen.net/bundlephobia/minzip/@sv443-network/userutils)](https://bundlephobia.com/package/@sv443-network/userutils)
 
-[![github stargazers badge](https://badgen.net/github/stars/Sv443-Network/UserUtils?icon=github)](https://github.com/Sv443-Network/UserUtils/stargazers)
-[![discord server badge](https://badgen.net/discord/online-members/aBH4uRG?icon=discord)](https://dc.sv443.net/)
-[![Support development on Github Sponsors](https://img.shields.io/github/sponsors/Sv443)](https://github.com/sponsors/Sv443)
+[![Discord server badge](https://badgen.net/discord/online-members/aBH4uRG?icon=discord)](https://dc.sv443.net/)
+[![Github stargazers badge](https://badgen.net/github/stars/Sv443-Network/UserUtils?icon=github)](https://github.com/Sv443-Network/UserUtils/stargazers)
+[![Support development on Github Sponsors badge](https://img.shields.io/github/sponsors/Sv443?icon=github)](https://github.com/sponsors/Sv443)
 
 <sup>
 View the documentation of previous major releases:

+ 6 - 1
lib/Mixins.ts

@@ -44,7 +44,12 @@ export type MixinConfig = {
 
 /** Configuration object for the Mixins class */
 export type MixinsConstructorConfig = {
-  /** If true, when no priority is specified, an auto-incrementing integer priority will be used (unique per mixin key). Defaults to false. */
+  /**
+   * If true, when no priority is specified, an auto-incrementing integer priority will be used, starting at `defaultPriority` or 0 (unique per mixin key). Defaults to false.  
+   * If a priority value is already used, it will be incremented until a unique value is found.  
+   * This is useful to ensure that mixins are applied in the order they were added, even if they don't specify a priority.  
+   * It also allows for a finer level of interjection when the priority is a floating point number.
+   */
   autoIncrementPriority: boolean;
   /** The default priority for mixins that do not specify one. Defaults to 0. */
   defaultPriority: number;

+ 53 - 5
lib/NanoEmitter.ts

@@ -10,12 +10,16 @@ export interface NanoEmitterOptions {
   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 */
+/**
+ * Class that can be extended or instantiated by itself to create a lightweight event emitter with helper methods and a strongly typed event map.  
+ * If extended from, you can use `this.events.emit()` to emit events, even if the `emit()` method doesn't work because `publicEmit` is not set to true in the constructor.
+ */
 export class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
   protected readonly events: Emitter<TEvtMap> = createNanoEvents<TEvtMap>();
   protected eventUnsubscribes: Unsubscribe[] = [];
   protected emitterOptions: NanoEmitterOptions;
 
+  /** Creates a new instance of NanoEmitter - a lightweight event emitter with helper methods and a strongly typed event map */
   constructor(options: Partial<NanoEmitterOptions> = {}) {
     this.emitterOptions = {
       publicEmit: false,
@@ -23,7 +27,27 @@ export class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
     };
   }
 
-  /** Subscribes to an event - returns a function that unsubscribes the event listener */
+  /**
+   * Subscribes to an event and calls the callback when it's emitted.  
+   * @param event The event to subscribe to. Use `as "_"` in case your event names aren't thoroughly typed (like when using a template literal, e.g. \`event-${val}\` as "_")
+   * @returns Returns a function that can be called to unsubscribe the event listener
+   * @example ```ts
+   * const emitter = new NanoEmitter<{
+   *   foo: (bar: string) => void;
+   * }>({
+   *   publicEmit: true,
+   * });
+   * 
+   * let i = 0;
+   * const unsub = emitter.on("foo", (bar) => {
+   *   // unsubscribe after 10 events:
+   *   if(++i === 10) unsub();
+   *   console.log(bar);
+   * });
+   * 
+   * emitter.emit("foo", "bar");
+   * ```
+   */
   public on<TKey extends keyof TEvtMap>(event: TKey | "_", cb: TEvtMap[TKey]): () => void {
     // eslint-disable-next-line prefer-const
     let unsub: Unsubscribe | undefined;
@@ -41,7 +65,24 @@ export class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
     return unsubProxy;
   }
 
-  /** Subscribes to an event and calls the callback or resolves the Promise only once */
+  /**
+   * Subscribes to an event and calls the callback or resolves the Promise only once when it's emitted.  
+   * @param event The event to subscribe to. Use `as "_"` in case your event names aren't thoroughly typed (like when using a template literal, e.g. \`event-${val}\` as "_")
+   * @param cb The callback to call when the event is emitted - if provided or not, the returned Promise will resolve with the event arguments
+   * @returns Returns a Promise that resolves with the event arguments when the event is emitted
+   * @example ```ts
+   * const emitter = new NanoEmitter<{
+   *   foo: (bar: string) => void;
+   * }>();
+   * 
+   * // Promise syntax:
+   * const [bar] = await emitter.once("foo");
+   * console.log(bar);
+   * 
+   * // Callback syntax:
+   * emitter.once("foo", (bar) => console.log(bar));
+   * ```
+   */
   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
@@ -54,10 +95,17 @@ export class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
       }) as TEvtMap[TKey];
 
       unsub = this.events.on(event, onceProxy);
+      this.eventUnsubscribes.push(unsub);
     });
   }
 
-  /** Emits an event on this instance - Needs `publicEmit` to be set to true in the constructor! */
+  /**
+   * Emits an event on this instance.  
+   * ⚠️ Needs `publicEmit` to be set to true in the NanoEmitter constructor or super() call!
+   * @param event The event to emit
+   * @param args The arguments to pass to the event listeners
+   * @returns Returns true if `publicEmit` is true and the event was emitted successfully
+   */
   public emit<TKey extends keyof TEvtMap>(event: TKey, ...args: Parameters<TEvtMap[TKey]>): boolean {
     if(this.emitterOptions.publicEmit) {
       this.events.emit(event, ...args);
@@ -66,7 +114,7 @@ export class NanoEmitter<TEvtMap extends EventsMap = DefaultEvents> {
     return false;
   }
 
-  /** Unsubscribes all event listeners */
+  /** Unsubscribes all event listeners from this instance */
   public unsubscribeAll(): void {
     for(const unsub of this.eventUnsubscribes)
       unsub();