Skip to content
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

Keep useFacetCallback instance same even if facets instances change #119

Merged
merged 4 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/@react-facet/core/src/hooks/useFacetCallback.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { act, render } from '@react-facet/dom-fiber-testing-library'
import { useFacetCallback } from './useFacetCallback'
import { useFacetEffect } from './useFacetEffect'
import { useFacetMap } from './useFacetMap'
import { NO_VALUE } from '../types'
import { createFacet } from '../facet'
import { NO_VALUE, Facet } from '../types'
import { createFacet, createStaticFacet } from '../facet'
import { NoValue } from '..'

it('captures the current value of the facet in a function that can be used as handler', () => {
Expand Down Expand Up @@ -321,4 +321,32 @@ describe('regressions', () => {
expect(result).toBe('valuestring')
})
})

it('always returns the same callback instance, even if the Facet instances change', () => {
let handler: () => void = () => {}
const facetA = createStaticFacet('a')
const facetB = createStaticFacet('b')

const TestComponent = ({ facet }: { facet: Facet<string> }) => {
handler = useFacetCallback(
(a) => () => {
return a
},
[],
[facet],
)

return null
}

const { rerender } = render(<TestComponent facet={facetA} />)
const firstHandler = handler
expect(firstHandler()).toBe('a')

rerender(<TestComponent facet={facetB} />)
const secondHandler = handler
expect(secondHandler()).toBe('b')

expect(firstHandler).toBe(secondHandler)
})
})
12 changes: 8 additions & 4 deletions packages/@react-facet/core/src/hooks/useFacetCallback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useLayoutEffect } from 'react'
import { useCallback, useLayoutEffect, useRef } from 'react'
import { Facet, NO_VALUE, ExtractFacetValues, NoValue } from '../types'

/**
Expand Down Expand Up @@ -59,8 +59,14 @@ export function useFacetCallback<M, Y extends Facet<unknown>[], T extends [...Y]
// eslint-disable-next-line react-hooks/exhaustive-deps
const callbackMemoized = useCallback(callback, dependencies)

// Setup a ref so that the callback instance below can be kept the same
// when Facet instances change across re-renders
const facetsRef = useRef(facets)
facetsRef.current = facets

return useCallback(
(...args: K) => {
const facets = facetsRef.current
const values = facets.map((facet) => facet.get())

for (const value of values) {
Expand All @@ -69,8 +75,6 @@ export function useFacetCallback<M, Y extends Facet<unknown>[], T extends [...Y]

return callbackMemoized(...(values as ExtractFacetValues<T>))(...(args as K))
},
// We care about each individual facet and if any is a different reference
// eslint-disable-next-line react-hooks/exhaustive-deps
[callbackMemoized, defaultReturnValue, ...facets],
[callbackMemoized, defaultReturnValue],
)
}