diff --git a/.changeset/large-kings-fetch.md b/.changeset/large-kings-fetch.md new file mode 100644 index 0000000..10ff509 --- /dev/null +++ b/.changeset/large-kings-fetch.md @@ -0,0 +1,5 @@ +--- +"@sv443-network/userutils": patch +--- + +Fixed `Debouncer` TS types as to not break backwards compat diff --git a/lib/Debouncer.ts b/lib/Debouncer.ts index 2e94381..73c0c19 100644 --- a/lib/Debouncer.ts +++ b/lib/Debouncer.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + /** * @module lib/Debouncer * This module contains the Debouncer class and debounce function that allow you to reduce the amount of calls in rapidly firing event listeners and such - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer) @@ -23,12 +25,15 @@ import { NanoEmitter } from "./NanoEmitter.js"; */ export type DebouncerType = "immediate" | "idle"; -export type DebouncerFunc = (...args: TArgs[]) => void | unknown; +type AnyFunc = (...args: any) => any; + +/** The debounced function type that is returned by the {@linkcode debounce} function */ +export type DebouncedFunction = ((...args: Parameters) => ReturnType) & { debouncer: Debouncer }; /** Event map for the {@linkcode Debouncer} */ -export type DebouncerEventMap = { +export type DebouncerEventMap = { /** Emitted when the debouncer calls all registered listeners, as a pub-sub alternative */ - call: DebouncerFunc; + call: TFunc; /** Emitted when the timeout or edge type is changed after the instance was created */ change: (timeout: number, type: DebouncerType) => void; }; @@ -44,15 +49,15 @@ export type DebouncerEventMap = { * - `call` - emitted when the debouncer calls all listeners - use this as a pub-sub alternative to the default callback-style listeners * - `change` - emitted when the timeout or edge type is changed after the instance was created */ -export class Debouncer extends NanoEmitter> { +export class Debouncer extends NanoEmitter> { /** All registered listener functions and the time they were attached */ - protected listeners: DebouncerFunc[] = []; + protected listeners: TFunc[] = []; /** The currently active timeout */ protected activeTimeout: ReturnType | undefined; /** The latest queued call */ - protected queuedCall: DebouncerFunc | undefined; + protected queuedCall: (() => void) | undefined; /** * Creates a new debouncer with the specified timeout and edge type. @@ -66,12 +71,12 @@ export class Debouncer extends NanoEmitter> { //#region listeners /** Adds a listener function that will be called on timeout */ - public addListener(fn: DebouncerFunc): void { + public addListener(fn: TFunc): void { this.listeners.push(fn); } /** Removes the listener with the specified function reference */ - public removeListener(fn: DebouncerFunc): void { + public removeListener(fn: TFunc): void { const idx = this.listeners.findIndex((l) => l === fn); idx !== -1 && this.listeners.splice(idx, 1); } @@ -113,9 +118,9 @@ export class Debouncer extends NanoEmitter> { //#region call /** Use this to call the debouncer with the specified arguments that will be passed to all listener functions registered with {@linkcode addListener()} */ - public call(...args: TArgs[]): void { + public call(...args: Parameters): void { /** When called, calls all registered listeners */ - const cl = (...a: TArgs[]) => { + const cl = (...a: Parameters) => { this.queuedCall = undefined; this.emit("call", ...a); this.listeners.forEach((l) => l.apply(this, a)); @@ -168,18 +173,15 @@ export class Debouncer extends NanoEmitter> { * * Refer to the {@linkcode Debouncer} class definition or the [Debouncer documentation](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#debouncer) for more information. */ -export function debounce< - TFunc extends DebouncerFunc, - TArgs, -> ( +export function debounce any>( fn: TFunc, timeout = 200, type: DebouncerType = "immediate" -): DebouncerFunc & { debouncer: Debouncer } { - const debouncer = new Debouncer(timeout, type); +): DebouncedFunction { + const debouncer = new Debouncer(timeout, type); debouncer.addListener(fn); - const func = (...args: TArgs[]) => debouncer.call(...args); + const func = (((...args: Parameters) => debouncer.call(...args))) as DebouncedFunction; func.debouncer = debouncer; return func;