From acf204d0e3a02d35cb1cf79b41ba3e15b64fecc6 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 9 Aug 2025 09:15:54 +0300 Subject: [PATCH] fix(react/bulk_actions): spaced update triggering too fast --- apps/client/src/widgets/react/hooks.tsx | 27 ++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx index 15849972d..d645e37df 100644 --- a/apps/client/src/widgets/react/hooks.tsx +++ b/apps/client/src/widgets/react/hooks.tsx @@ -1,4 +1,4 @@ -import { useContext, useEffect, useMemo, useState } from "preact/hooks"; +import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "preact/hooks"; import { EventData, EventNames } from "../../components/app_context"; import { ParentComponent } from "./ReactBasicWidget"; import SpacedUpdate from "../../services/spaced_update"; @@ -33,9 +33,26 @@ export default function useTriliumEvent(eventName: T, hand } export function useSpacedUpdate(callback: () => Promise, interval = 1000) { - const spacedUpdate = useMemo(() => { - return new SpacedUpdate(callback, interval); - }, [callback, interval]); + const callbackRef = useRef(callback); + const spacedUpdateRef = useRef(); - return spacedUpdate; + // Update callback ref when it changes + useEffect(() => { + callbackRef.current = callback; + }); + + // Create SpacedUpdate instance only once + if (!spacedUpdateRef.current) { + spacedUpdateRef.current = new SpacedUpdate( + () => callbackRef.current(), + interval + ); + } + + // Update interval if it changes + useEffect(() => { + spacedUpdateRef.current?.setUpdateInterval(interval); + }, [interval]); + + return spacedUpdateRef.current; } \ No newline at end of file