-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reivew usesubscription with usedeepcompareeffect
- Loading branch information
Showing
5 changed files
with
154 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as React from 'react' | ||
import { dequal } from 'dequal' | ||
|
||
type UseEffectParams = Parameters<typeof React.useEffect> | ||
type EffectCallback = UseEffectParams[0] | ||
type DependencyList = NonNullable<UseEffectParams[1]> | ||
type UseEffectReturn = ReturnType<typeof React.useEffect> | ||
|
||
export function useDeepCompareMemoize<T>(value: T) { | ||
const ref = React.useRef<T>(value) | ||
const signalRef = React.useRef<number>(0) | ||
|
||
if (!dequal(value, ref.current)) { | ||
ref.current = value | ||
signalRef.current += 1 | ||
} | ||
return React.useMemo(() => ref.current, [signalRef.current]) | ||
} | ||
|
||
function useDeepCompareEffect( | ||
callback: EffectCallback, | ||
dependencies: DependencyList = [] | ||
): UseEffectReturn { | ||
return React.useEffect(callback, useDeepCompareMemoize(dependencies)) | ||
} | ||
|
||
export default useDeepCompareEffect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
packages/graphql-hooks/test-jsdom/unit/useDeepCompareEffect.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import useDeepCompareEffect from '../../src/useDeepCompareEffect' | ||
|
||
describe('useDeepCompareEffect', () => { | ||
let callback: jest.Mock | ||
|
||
beforeEach(() => { | ||
callback = jest.fn() | ||
}) | ||
|
||
describe('onMount', () => { | ||
it('should call the callback once on mount if deps are empty', () => { | ||
renderHook(({ deps }) => useDeepCompareEffect(callback, deps), { | ||
initialProps: { deps: [] } | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should call the callback once on mount if deps are not empty', () => { | ||
renderHook(({ deps }) => useDeepCompareEffect(callback, deps), { | ||
initialProps: { deps: [{ a: 1 }, { b: 1 }] } | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
|
||
it('should call the callback only once on mount if deps are undefined', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: undefined as unknown as unknown[] } | ||
} | ||
) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
rerender({ deps: undefined as unknown as unknown[] }) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should call the callback once if deps change but have same values', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: [{ a: 1, b: 3 }, { c: 2 }] } | ||
} | ||
) | ||
|
||
rerender({ deps: [{ a: 1, b: 3 }, { c: 2 }] }) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should call the callback twice when a dependency changes', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: [{ a: 1 }] } | ||
} | ||
) | ||
|
||
rerender({ deps: [{ a: 2 }] }) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should call the callback once when a dependency contains a list of undefined', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: [undefined, undefined] } | ||
} | ||
) | ||
|
||
rerender({ deps: [undefined, undefined] }) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should call the callback twice when dependency changes from an undefined value to a defined value', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: [undefined] as unknown[] } | ||
} | ||
) | ||
|
||
rerender({ deps: [{ a: 1 }] }) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should call the callback twice when dependency changes from a defined value to an undefined value', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: [{ a: 1 }] as unknown[] } | ||
} | ||
) | ||
|
||
rerender({ deps: [undefined] }) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should call the callback twice when dependency with many levels changes', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: [{ a: { b: { c: 3, d: 4 } } }] as unknown[] } | ||
} | ||
) | ||
|
||
rerender({ deps: [{ a: { b: { c: 3 } } }] }) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should call the callback once when dependency with many levels is cloned', () => { | ||
const { rerender } = renderHook( | ||
({ deps }) => useDeepCompareEffect(callback, deps), | ||
{ | ||
initialProps: { deps: [{ a: { b: { c: 3 } } }] as unknown[] } | ||
} | ||
) | ||
|
||
rerender({ deps: [{ a: { b: { c: 3 } } }] }) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters