Skip to content

A number of StrictMode fixes and updates #99

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

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
fix:(useDebouncedCallback): Clean up timeout logic in strict mode
  • Loading branch information
jquense committed Nov 22, 2024
commit ddeb67d1385d9cd4814eab1e65933f2659f9d1da
11 changes: 3 additions & 8 deletions src/useDebouncedCallback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useMemo, useRef } from 'react'
import useTimeout from './useTimeout'
import useEventCallback from './useEventCallback'
import useWillUnmount from './useWillUnmount'
import useMounted from './useMounted'

export interface UseDebouncedCallbackOptions {
wait: number
Expand Down Expand Up @@ -71,11 +71,6 @@ function useDebouncedCallback<TCallback extends (...args: any[]) => any>(

const timeout = useTimeout()

useWillUnmount(() => {
clearTimeout(timerRef.current)
isTimerSetRef.current = false
})

return useMemo(() => {
const hasMaxWait = !!maxWait

Expand Down Expand Up @@ -168,14 +163,14 @@ function useDebouncedCallback<TCallback extends (...args: any[]) => any>(
if (hasMaxWait) {
// Handle invocations in a tight loop.
isTimerSetRef.current = true
timerRef.current = setTimeout(timerExpired, wait)
timeout.set(timerExpired, wait)
return invokeFunc(lastCallTimeRef.current)
}
}

if (!isTimerSetRef.current) {
isTimerSetRef.current = true
timerRef.current = setTimeout(timerExpired, wait)
timerRef.current = timeout.set(timerExpired, wait)
}

return returnValueRef.current
Expand Down
33 changes: 24 additions & 9 deletions test/useDebouncedCallback.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ describe('useDebouncedCallback', () => {
jest.useFakeTimers()
})

afterEach(() => {
act(() => {
jest.runAllTimers()
})
})

it('should return a function that debounces input callback', () => {
const callback = jest.fn()

Expand All @@ -21,7 +27,9 @@ describe('useDebouncedCallback', () => {

expect(callback).not.toHaveBeenCalled()

jest.runOnlyPendingTimers()
act(() => {
jest.runOnlyPendingTimers()
})

expect(callback).toHaveBeenCalledTimes(1)
expect(callback).toHaveBeenCalledWith(3)
Expand All @@ -47,7 +55,6 @@ describe('useDebouncedCallback', () => {
act(() => {
jest.runOnlyPendingTimers()
})

expect(callback).toHaveBeenCalledTimes(1)
})

Expand Down Expand Up @@ -90,16 +97,13 @@ describe('useDebouncedCallback', () => {
result.current()
result.current()
result.current()

setTimeout(() => {
result.current()
}, 1001)
})

expect(callback).toHaveBeenCalledTimes(1)

act(() => {
jest.advanceTimersByTime(1001)
result.current()
})

expect(callback).toHaveBeenCalledTimes(3)
Expand Down Expand Up @@ -137,17 +141,25 @@ describe('useDebouncedCallback', () => {
const callback = jest.fn(() => 42)

const { result } = renderHook(() => useDebouncedCallback(callback, 1000))
let retVal

act(() => {
retVal = result.current()
})

const retVal = result.current()
expect(callback).toHaveBeenCalledTimes(0)
expect(retVal).toBeUndefined()

act(() => {
jest.runAllTimers()
})

expect(callback).toHaveBeenCalledTimes(1)

const subsequentResult = result.current()
let subsequentResult
act(() => {
subsequentResult = result.current()
})

expect(callback).toHaveBeenCalledTimes(1)
expect(subsequentResult).toBe(42)
Expand All @@ -160,7 +172,10 @@ describe('useDebouncedCallback', () => {
useDebouncedCallback(callback, { wait: 1000, leading: true }),
)

const retVal = result.current()
let retVal
act(() => {
retVal = result.current()
})

expect(callback).toHaveBeenCalledTimes(1)
expect(retVal).toEqual(42)
Expand Down
12 changes: 7 additions & 5 deletions test/useDebouncedState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ describe('useDebouncedState', () => {
const wrapper = render(<Wrapper />)
expect(wrapper.getByText('0')).toBeTruthy()

outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
act(() => {
outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
outerSetValue((cur: number) => cur + 1)
})

expect(wrapper.getByText('0')).toBeTruthy()

Expand Down