From 94779cb72bb8dfb89db2a95756a6e389a64fe72e Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Mon, 6 May 2024 22:46:02 +0100 Subject: [PATCH] Fix non-reactive debounce --- src/state/feed-feedback.tsx | 47 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/state/feed-feedback.tsx b/src/state/feed-feedback.tsx index bca57ec28f..70b436fdea 100644 --- a/src/state/feed-feedback.tsx +++ b/src/state/feed-feedback.tsx @@ -28,31 +28,30 @@ export function useFeedFeedback(feed: FeedDescriptor, hasSession: boolean) { const queue = React.useRef>(new Set()) const history = React.useRef>(new Set()) - const [sendToFeed] = React.useState(() => - debounce( - () => { - const proxyAgent = getAgent().withProxy( - // @ts-ignore TODO need to update withProxy() to support this key -prf - 'bsky_fg', - // TODO when we start sending to other feeds, we need to grab their DID -prf - 'did:web:discover.bsky.app', - ) as BskyAgent - proxyAgent.app.bsky.feed - .sendInteractions({ - interactions: Array.from(queue.current).map(toInteraction), - }) - .catch((e: any) => { - logger.warn('Failed to send feed interactions', {error: e}) - }) + const sendToFeedNotDebounced = React.useCallback(() => { + const proxyAgent = getAgent().withProxy( + // @ts-ignore TODO need to update withProxy() to support this key -prf + 'bsky_fg', + // TODO when we start sending to other feeds, we need to grab their DID -prf + 'did:web:discover.bsky.app', + ) as BskyAgent + proxyAgent.app.bsky.feed + .sendInteractions({ + interactions: Array.from(queue.current).map(toInteraction), + }) + .catch((e: any) => { + logger.warn('Failed to send feed interactions', {error: e}) + }) + + for (const v of queue.current) { + history.current.add(v) + } + queue.current.clear() + }, [getAgent]) - for (const v of queue.current) { - history.current.add(v) - } - queue.current.clear() - }, - 15e3, - {maxWait: 60e3}, - ), + const sendToFeed = React.useMemo( + () => debounce(sendToFeedNotDebounced, 15e3, {maxWait: 60e3}), + [sendToFeedNotDebounced], ) React.useEffect(() => {