Skip to content

Build your personal knowledge base with Trilium Notes

chore(deps): drop debounce, reuse the local services/debounce

Elian Doranfbe0e4523ccd
diff --git a/apps/client/package.json b/apps/client/package.json
index f9c73bcbd6..9e2edd249a 100644
--- a/apps/client/package.json
+++ b/apps/client/package.json
@@ -49,7 +49,6 @@
"boxicons": "2.1.4",
"clsx": "2.1.1",
"color": "5.0.3",
- "debounce": "3.0.0",
"diff": "9.0.0",
"dompurify": "3.4.7",
"draggabilly": "3.0.0",
diff --git a/apps/client/src/services/debounce.spec.ts b/apps/client/src/services/debounce.spec.ts
index 5ec4469f7c..1b80630c79 100644
--- a/apps/client/src/services/debounce.spec.ts
+++ b/apps/client/src/services/debounce.spec.ts
@@ -2,10 +2,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import debounce from "./debounce.js";
-// The source types the returned function with no declared params (it forwards `arguments`),
-// so widen it to a variadic callable for the tests.
-type Debounced = ((...args: any[]) => any) & { clear(): void; flush(): void };
-
describe("debounce", () => {
beforeEach(() => {
vi.useFakeTimers();
@@ -17,7 +13,7 @@ describe("debounce", () => {
it("defers a trailing call until the wait elapses after the last invocation", () => {
const fn = vi.fn();
- const debounced = debounce(fn, 100) as Debounced;
+ const debounced = debounce(fn, 100);
debounced("a");
// Re-invoking before the wait elapses keeps deferring (reschedule branch in `later`).
@@ -32,12 +28,24 @@ describe("debounce", () => {
expect(fn).toHaveBeenCalledWith("b");
});
+ it("forwards multiple positional arguments to the wrapped function", () => {
+ // Mirrors the snippets.ts call site, which invokes the debounced handler
+ // with two arguments — the case the old 0-param typing couldn't express.
+ const fn = vi.fn((a: string, b: number) => `${a}:${b}`);
+ const debounced = debounce(fn, 100);
+
+ debounced("x", 7);
+ vi.advanceTimersByTime(100);
+ expect(fn).toHaveBeenCalledTimes(1);
+ expect(fn).toHaveBeenCalledWith("x", 7);
+ });
+
it("preserves `this` context and returns the previous trailing result", () => {
const ctx = { value: 42 };
const fn = vi.fn(function (this: typeof ctx, n: number) {
return this.value + n;
});
- const debounced = debounce(fn, 100) as Debounced;
+ const debounced = debounce(fn, 100);
// First trailing invocation: nothing computed yet, returns undefined.
expect(debounced.call(ctx, 1)).toBeUndefined();
@@ -53,7 +61,7 @@ describe("debounce", () => {
it("invokes on the leading edge when `immediate` is true and does not re-fire on trailing", () => {
const fn = vi.fn((x: string) => x.toUpperCase());
- const debounced = debounce(fn, 100, true) as Debounced;
+ const debounced = debounce(fn, 100, true);
// Leading call fires synchronously (callNow branch) and returns the result.
expect(debounced("hi")).toBe("HI");
@@ -75,7 +83,7 @@ describe("debounce", () => {
it("defaults the wait to 100ms when called with a nullish wait", () => {
const fn = vi.fn();
// Force the `null == waitMs` branch (the type says number, but runtime guards null/undefined).
- const debounced = debounce(fn, null as unknown as number) as Debounced;
+ const debounced = debounce(fn, null as unknown as number);
debounced();
vi.advanceTimersByTime(99);
@@ -86,7 +94,7 @@ describe("debounce", () => {
it("clear() cancels a pending trailing call and is a no-op when nothing is pending", () => {
const fn = vi.fn();
- const debounced = debounce(fn, 100) as Debounced;
+ const debounced = debounce(fn, 100);
debounced();
debounced.clear();
@@ -100,7 +108,7 @@ describe("debounce", () => {
it("flush() invokes immediately with the latest args and is a no-op when nothing is pending", () => {
const fn = vi.fn((x: number) => x * 2);
- const debounced = debounce(fn, 100) as Debounced;
+ const debounced = debounce(fn, 100);
debounced(5);
expect(debounced.flush()).toBeUndefined(); // flush returns nothing
@@ -118,7 +126,7 @@ describe("debounce", () => {
it("flush() does not re-fire after a leading-edge call when `immediate` is true", () => {
const fn = vi.fn((x: string) => x.toUpperCase());
- const debounced = debounce(fn, 100, true) as Debounced;
+ const debounced = debounce(fn, 100, true);
// Leading-edge call fires once and nulls `args` while the trailing timer stays live.
expect(debounced("hi")).toBe("HI");
@@ -136,7 +144,7 @@ describe("debounce", () => {
it("reschedules in `later` when the system clock jumps backwards (last < 0)", () => {
const fn = vi.fn();
- const debounced = debounce(fn, 100) as Debounced;
+ const debounced = debounce(fn, 100);
const nowSpy = vi.spyOn(Date, "now");
// timestamp recorded at invocation time = 1000.
diff --git a/apps/client/src/services/debounce.ts b/apps/client/src/services/debounce.ts
index 143b37ec0a..8e20f6302f 100644
--- a/apps/client/src/services/debounce.ts
+++ b/apps/client/src/services/debounce.ts
@@ -12,12 +12,25 @@
* @param whether to execute at the beginning (`false`)
* @api public
*/
-function debounce<T>(func: (...args: any[]) => T, waitMs: number, immediate: boolean = false) {
+type AnyFunction = (...args: any[]) => any;
+
+/**
+ * The wrapper returned by {@link debounce}: it accepts the same arguments as the
+ * wrapped function and returns its last result (or `undefined` before the first
+ * trailing call), plus `clear`/`flush` controls.
+ */
+export interface DebouncedFunction<F extends AnyFunction> {
+ (...args: Parameters<F>): ReturnType<F> | undefined;
+ clear(): void;
+ flush(): void;
+}
+
+function debounce<F extends AnyFunction>(func: F, waitMs: number, immediate: boolean = false): DebouncedFunction<F> {
let timeout: any; // TODO: fix once we split client and server.
let args: unknown[] | null;
let context: unknown;
let timestamp: number;
- let result: T;
+ let result: ReturnType<F>;
if (null == waitMs) waitMs = 100;
function later() {
diff --git a/apps/client/src/widgets/type_widgets/text/snippets.ts b/apps/client/src/widgets/type_widgets/text/snippets.ts
index 3a48192dee..ce0b013a08 100644
--- a/apps/client/src/widgets/type_widgets/text/snippets.ts
+++ b/apps/client/src/widgets/type_widgets/text/snippets.ts
@@ -1,4 +1,4 @@
-import debounce from "debounce";
+import debounce from "../../../services/debounce.js";
import froca from "../../../services/froca.js";
import type LoadResults from "../../../services/load_results.js";
import search from "../../../services/search.js";
diff --git a/apps/standalone/package.json b/apps/standalone/package.json
index 151ca2f726..b94e8a8358 100644
--- a/apps/standalone/package.json
+++ b/apps/standalone/package.json
@@ -40,7 +40,6 @@
"boxicons": "2.1.4",
"clsx": "2.1.1",
"color": "5.0.3",
- "debounce": "3.0.0",
"draggabilly": "3.0.0",
"fflate": "0.8.3",
"force-graph": "1.51.4",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 60cc32823d..71dae7e446 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -290,9 +290,6 @@ importers:
color:
specifier: 5.0.3
version: 5.0.3
- debounce:
- specifier: 3.0.0
- version: 3.0.0
diff:
specifier: 9.0.0
version: 9.0.0
@@ -921,9 +918,6 @@ importers:
color:
specifier: 5.0.3
version: 5.0.3
- debounce:
- specifier: 3.0.0
- version: 3.0.0
draggabilly:
specifier: 3.0.0
version: 3.0.0
@@ -7774,10 +7768,6 @@ packages:
resolution: {integrity: sha512-XRRe6Glud4rd/ZGQfiV1ruXSfbvfJedlV9Y6zOlP+2K04vBYiJEte6stfFkCP03aMnY5tsipamumUjL14fofug==}
- resolution: {integrity: sha512-64byRbF0/AirwbuHqB3/ZpMG9/nckDa6ZA0yd6UnaQNwbbemCOwvz2sL5sjXLHhZHADyiwLm0M5qMhltUUx+TA==}
- engines: {node: '>=20'}
-
resolution: {integrity: sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==}
peerDependencies:
@@ -22072,8 +22062,6 @@ snapshots:
-
dependencies:
ms: 2.0.0