-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mblink/ht-useStableCallback-useStableMemo
useStableCallback and useStableMemo
- Loading branch information
Showing
12 changed files
with
119 additions
and
33 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as Eq from 'fp-ts/Eq'; | ||
import { useCallback } from 'react'; | ||
import { useEqMemoize } from './useEqMemoize'; | ||
|
||
export const useStableCallback = <A extends ReadonlyArray<unknown>, T extends (...args: any[]) => any>( | ||
callback: T, | ||
dependencies: A, | ||
eq: Eq.Eq<A> | ||
): T => { | ||
return useCallback(callback, useEqMemoize(dependencies, eq)); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { useStable, useStableE, useStableO } from './state'; | ||
export { useStableEffect } from './effect'; | ||
export { useStableEffect } from './effect'; | ||
export { useStableCallback } from './callback'; | ||
export { useStableMemo } from './memo'; |
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,11 @@ | ||
import * as Eq from 'fp-ts/Eq'; | ||
import { useEqMemoize } from './useEqMemoize'; | ||
import { useMemo } from 'react'; | ||
|
||
export const useStableMemo = <A extends ReadonlyArray<unknown>, T>( | ||
factory: () => T, | ||
dependencies: A, | ||
eq: Eq.Eq<A> | ||
): T => { | ||
return useMemo(factory, useEqMemoize(dependencies, eq)); | ||
}; |
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,19 @@ | ||
import * as Eq from 'fp-ts/Eq'; | ||
import { useRef } from 'react'; | ||
|
||
// Use effect prior art comes from https://github.com/kentcdodds/use-deep-compare-effect/blob/master/src/index.ts | ||
export const useEqMemoize = <A extends ReadonlyArray<unknown>>(value: A, eq: Eq.Eq<A>) => { | ||
const ref = useRef<A>(); | ||
const signalRef = useRef<number>(0); | ||
|
||
if (ref.current == null) { | ||
ref.current = value; | ||
} | ||
|
||
if (!eq.equals(value, ref.current)) { | ||
ref.current = value; | ||
signalRef.current += 1; | ||
} | ||
|
||
return [signalRef.current]; | ||
}; |
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,30 @@ | ||
import * as Eq from 'fp-ts/Eq'; | ||
import * as O from 'fp-ts/Option'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useStableCallback } from '../src/index'; | ||
|
||
const o1a = O.some(1); | ||
const o1b = O.some(1); | ||
const o2 = O.some(2); | ||
const nEq = Eq.getTupleEq(O.getEq(Eq.eqNumber)); | ||
|
||
describe('useStableCallback', () => { | ||
test('should not return a new function if the values are the same', () => { | ||
let o = o1a; | ||
const { result, rerender } = renderHook(() => useStableCallback(() => o, [o], nEq)); | ||
o = o1b; | ||
rerender(); | ||
expect(result.all[0]).toStrictEqual(result.all[1]); | ||
expect(result.current()).toStrictEqual(o1a); | ||
}); | ||
|
||
test('should return a new function if the values are different', () => { | ||
let o = o1a; | ||
const { result, rerender } = renderHook(() => useStableCallback((a: number) => O.getOrElse(() => 0)(o) + a, [o], nEq)); | ||
expect(result.current(1)).toStrictEqual(2); | ||
o = o2; | ||
rerender(); | ||
expect(result.all[0]).not.toStrictEqual(result.all[1]); | ||
expect(result.current(1)).toStrictEqual(3); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as Eq from 'fp-ts/Eq'; | ||
import * as O from 'fp-ts/Option'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useStableMemo } from '../src/index'; | ||
|
||
const o1a = O.some(1); | ||
const o1b = O.some(1); | ||
const o2 = O.some(2); | ||
const nEq = Eq.getTupleEq(O.getEq(Eq.eqNumber)); | ||
|
||
describe('useStableMemo', () => { | ||
test('should not return a value if the values are the same', () => { | ||
let o = o1a; | ||
const { result, rerender } = renderHook(() => useStableMemo(() => o, [o], nEq)); | ||
o = o1b; | ||
rerender(); | ||
expect(result.current).toStrictEqual(o1a); | ||
}); | ||
|
||
test('should return a new value if the values are different', () => { | ||
let o = o1a; | ||
const { result, rerender } = renderHook(() => useStableMemo(() => o, [o], nEq)); | ||
o = o2; | ||
rerender(); | ||
expect(result.all[0]).not.toStrictEqual(result.all[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