Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v9.0.0 #74

Merged
merged 33 commits into from
Jan 22, 2025
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
18a46e9
feat: reworked translation system
Sv443 Dec 23, 2024
6ba1fc3
fix: exports order
Sv443 Dec 26, 2024
b28f943
feat: start working on new debouncer class
Sv443 Dec 26, 2024
f2574cb
chore: update deps
Sv443 Jan 11, 2025
c780ae6
feat: debugger with tsx
Sv443 Jan 11, 2025
fff7b00
ref: debouncer stuff
Sv443 Jan 11, 2025
4f52ccd
feat: fix .d.ts whitespaces tool
Sv443 Jan 11, 2025
61706e0
ref: logging in cli tools
Sv443 Jan 11, 2025
0c2e8d2
ref: replace ts-node with node --import tsx
Sv443 Jan 11, 2025
2b62b14
ref: tsdoc stuff
Sv443 Jan 11, 2025
a93a1c9
docs: more clear DataStore documentation
Sv443 Jan 11, 2025
057c1ef
ref: replace any with unknown
Sv443 Jan 11, 2025
2cf8656
feat: complete translation system overhaul
Sv443 Jan 14, 2025
18a2a2a
chore: tsdoc comment newlines changeset
Sv443 Jan 14, 2025
61ac745
feat: unfinished debouncer stuff
Sv443 Jan 14, 2025
9a9f46a
feat: launch cfg stuff
Sv443 Jan 15, 2025
95a6ef8
docs: NanoEmitter comments
Sv443 Jan 15, 2025
addc119
feat: queuedImmediate Debouncer done
Sv443 Jan 15, 2025
4709526
ref!: rename debouncer edge types & remove discardingImmediate
Sv443 Jan 15, 2025
6e00171
feat: new debounce type sketch
Sv443 Jan 15, 2025
858d501
fix: selectorobserver default debounce type
Sv443 Jan 15, 2025
4ef0932
ref: remove old debounce & change links
Sv443 Jan 15, 2025
422622d
docs: debouncer docs & rename usage to signature
Sv443 Jan 22, 2025
e95d835
docs: fix stuff
Sv443 Jan 22, 2025
962e8cd
ref: separate docs file (#71) & edit links
Sv443 Jan 22, 2025
217e160
fix: randRange with enhancedEntropy only returning first digit
Sv443 Jan 22, 2025
a85f9c6
feat: changesets
Sv443 Jan 22, 2025
007aa9d
docs: fix StringGen and ValueGen documentation
Sv443 Jan 22, 2025
5bf974d
feat: add argument `additionalProps` to `openInNewTab()`
Sv443 Jan 22, 2025
8be8ba6
docs: change description
Sv443 Jan 22, 2025
dfa265e
docs: LooseUnion in translation docs
Sv443 Jan 22, 2025
99d268d
fix: don't backtrack on translation key if nested value was not found
Sv443 Jan 22, 2025
4b0ab30
ref: changeset
Sv443 Jan 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: unfinished debouncer stuff
  • Loading branch information
Sv443 committed Jan 14, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 61ac7458f8dab2401fec732478f597003831d026
71 changes: 63 additions & 8 deletions lib/Debouncer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NanoEmitter } from "./NanoEmitter.js";

//#region types

/**
* The type of edge to use for the debouncer - [see this issue for an explanation and diagram.](https://github.com/Sv443-Network/UserUtils/issues/46)
* - `queuedImmediate`
@@ -19,6 +21,10 @@ export type DebouncerType = "queuedImmediate" | "queuedIdle" | "discardingImmedi

export type DebouncerFunc<TArgs> = (...args: TArgs[]) => void | unknown;

export type DebouncerListener<TArgs> = [time: number, fn: DebouncerFunc<TArgs>];

//#region class

/**
* A debouncer that calls all listeners after a specified timeout, discarding all calls in-between.
* It is very useful for event listeners that fire quickly, like `input` or `mousemove`, to prevent the listeners from being called too often and hogging resources.
@@ -33,10 +39,13 @@ export class Debouncer<TArgs> extends NanoEmitter<{
change: (timeout: number, type: DebouncerType) => void;
}> {
/** All registered listener functions and the time they were attached */
protected listeners: [time: number, fn: DebouncerFunc<TArgs>][] = [];
protected listeners: DebouncerListener<TArgs>[] = [];

/** The latest call that was queued */
protected queuedListener: typeof this.listeners[number] | undefined = undefined;
protected queuedListener: DebouncerListener<TArgs> | undefined = undefined;

/** Whether the timeout is currently active */
protected timeoutActive = false;

/**
* Creates a new debouncer with the specified timeout and edge type.
@@ -65,12 +74,7 @@ export class Debouncer<TArgs> extends NanoEmitter<{
this.listeners = [];
}

//#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 ["TODO:", args];
}
//#region timeout

/** Sets the timeout for the debouncer */
public setTimeout(timeout: number) {
@@ -82,6 +86,13 @@ export class Debouncer<TArgs> extends NanoEmitter<{
return this.timeout;
}

/** Whether the timeout is currently active, meaning any latest call to the {@linkcode call()} method will be queued */
public isTimeoutActive() {
return this.timeoutActive;
}

//#region type

/** Sets the edge type for the debouncer */
public setType(type: DebouncerType) {
this.emit("change", this.timeout, this.type = type);
@@ -91,8 +102,50 @@ export class Debouncer<TArgs> extends NanoEmitter<{
public getType() {
return this.type;
}

//#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[]) {
const cl = () => {
this.emit("call", ...args);
this.timeoutActive = false;
this.queuedListener = undefined;
};

if(this.timeoutActive) {
this.queuedListener = [Date.now(), cl];
return;
}

this.timeoutActive = true;

switch(this.type) {
case "queuedImmediate":
cl();
setTimeout(() => this.timeoutActive = false, this.timeout);
break;
case "queuedIdle":
setTimeout(() => {
if(this.queuedListener) {
this.emit("call", ...args);
this.timeoutActive = false;
this.queuedListener = undefined;
}
}, this.timeout);
break;
case "discardingImmediate":
setTimeout(() => this.timeoutActive = false, this.timeout);
cl();
break;
default:
throw new Error(`Unknown debouncer edge type: ${this.type}`);
}
}
}

//#region debounce fn

/**
* Creates a {@linkcode Debouncer} instance with the specified timeout and edge type and attaches the passed function as a listener.
* The returned function can be called with any arguments and will execute the `call()` method of the debouncer.
@@ -115,6 +168,8 @@ export function debounce<
return func;
}

//#region #DBG old

// TODO:FIXME: https://github.com/Sv443-Network/UserUtils/issues/46
/**
* Calls the passed {@linkcode func} after the specified {@linkcode timeout} in ms (defaults to 300).
1 change: 1 addition & 0 deletions lib/math.ts
Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ export function randRange(...args: (number | boolean | undefined)[]): number {
throw new TypeError("Parameter \"min\" can't be bigger than \"max\"");

if(enhancedEntropy) {
// TODO:FIXME: doesn't work
const uintArr = new Uint8Array(1);
crypto.getRandomValues(uintArr);
return Number(Array.from(
Loading