Skip to content

Commit

Permalink
Fix non-reactive debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed May 6, 2024
1 parent de44efa commit 94779cb
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/state/feed-feedback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,30 @@ export function useFeedFeedback(feed: FeedDescriptor, hasSession: boolean) {
const queue = React.useRef<Set<string>>(new Set())
const history = React.useRef<Set<string>>(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(() => {
Expand Down

0 comments on commit 94779cb

Please sign in to comment.