-
Notifications
You must be signed in to change notification settings - Fork 23
Add support for multiple facets in useFacetCallback and useFacetEffect #14
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
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fccfa8f
Add support for multiple facets in useFacetCallback
pirelenito e0249be
Adds extra coverage when facets have no value
pirelenito 95882f0
Small tweak in docs
pirelenito d451879
Allow useFacetEffect to accept multiple facets
pirelenito 37f45ea
Keep the original implementation if we have a single facet
pirelenito 33cfbd8
Small tweak in the documentation
pirelenito 0974587
Update all occurrences of useFacetEffect
pirelenito c99dfd0
Merge branch 'main' into use-facet-callback-multiple-facets
pirelenito dbaaa8b
Make it explicit the return
pirelenito e9e1d62
Merge branch 'use-facet-callback-multiple-facets' of github.com:Mojan…
pirelenito e1e52fe
Removing unknown type-casting
pirelenito 609f2bf
Change to useLayoutEffect to match useFacetEffect implementation
pirelenito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -25,7 +25,7 @@ function Performance() { | |
// and effect that does nothing | ||
}, | ||
[], | ||
value, | ||
[value], | ||
) | ||
|
||
return null | ||
|
This file contains hidden or 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 hidden or 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
167 changes: 139 additions & 28 deletions
167
packages/@react-facet/core/src/hooks/useFacetCallback.ts
This file contains hidden or 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,47 +1,158 @@ | ||
import { useCallback, useRef } from 'react' | ||
import { useCallback, useEffect, useRef } from 'react' | ||
import { NoValue } from '..' | ||
import { Facet, NO_VALUE, Option } from '../types' | ||
import { useFacetEffect } from './useFacetEffect' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, V3, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>, Facet<V3>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, V3, V4, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3, v4: V4) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>, Facet<V3>, Facet<V4>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, V3, V4, V5, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3, v4: V4, v5: V5) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>, Facet<V3>, Facet<V4>, Facet<V5>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, V3, V4, V5, V6, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3, v4: V4, v5: V5, v6: V6) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>, Facet<V3>, Facet<V4>, Facet<V5>, Facet<V6>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, V3, V4, V5, V6, V7, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3, v4: V4, v5: V5, v6: V6, v7: V7) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>, Facet<V3>, Facet<V4>, Facet<V5>, Facet<V6>, Facet<V7>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, V3, V4, V5, V6, V7, V8, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3, v4: V4, v5: V5, v6: V6, v7: V7, v8: V8) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>, Facet<V3>, Facet<V4>, Facet<V5>, Facet<V6>, Facet<V7>, Facet<V8>], | ||
): C | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function useFacetCallback<M, V, V1, V2, V3, V4, V5, V6, V7, V8, V9, C extends (...args: any[]) => M | NoValue>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3, v4: V4, v5: V5, v6: V6, v7: V7, v8: V8, v9: V9) => C, | ||
dependencies: unknown[], | ||
facet: [Facet<V>, Facet<V1>, Facet<V2>, Facet<V3>, Facet<V4>, Facet<V5>, Facet<V6>, Facet<V7>, Facet<V8>, Facet<V9>], | ||
): C | ||
|
||
export function useFacetCallback< | ||
M, | ||
V, | ||
V1, | ||
V2, | ||
V3, | ||
V4, | ||
V5, | ||
V6, | ||
V7, | ||
V8, | ||
V9, | ||
V10, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
C extends (...args: any[]) => M | NoValue, | ||
>( | ||
callback: (v: V, v1: V1, v2: V2, v3: V3, v4: V4, v5: V5, v6: V6, v7: V7, v8: V8, v9: V9, v10: V10) => C, | ||
dependencies: unknown[], | ||
facet: [ | ||
Facet<V>, | ||
Facet<V1>, | ||
Facet<V2>, | ||
Facet<V3>, | ||
Facet<V4>, | ||
Facet<V5>, | ||
Facet<V6>, | ||
Facet<V7>, | ||
Facet<V8>, | ||
Facet<V9>, | ||
Facet<V10>, | ||
], | ||
): C | ||
|
||
/** | ||
* Creates a callback that depends on the value of a facet. | ||
* Very similar to `useCallback` from `React` | ||
* | ||
* @param callback function callback that receives the current facet value and the arguments passed to the callback | ||
* @param dependencies variable used by the map that are available in scope (similar as dependencies of useCallback) | ||
* @param facet facet that the callback listens to | ||
* @param callback function callback that receives the current facet values and the arguments passed to the callback | ||
* @param dependencies variable used by the callback that are available in scope (similar as dependencies of useCallback) | ||
* @param facets facets that the callback listens to | ||
* | ||
* We pass the dependencies of the callback as the second argument so we can leverage the eslint-plugin-react-hooks option for additionalHooks. | ||
* Having this as the second argument allows the linter to work. | ||
*/ | ||
export function useFacetCallback<V, T>( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
callback: (value: V) => (...args: any[]) => T | NoValue, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
dependencies: any[], | ||
facet: Facet<V>, | ||
) { | ||
const facetRef = useRef<Option<V>>(facet.get()) | ||
|
||
useFacetEffect( | ||
(value) => { | ||
facetRef.current = value | ||
}, | ||
[], | ||
facet, | ||
) | ||
export function useFacetCallback<M, C extends (...args: unknown[]) => M | NoValue>( | ||
callback: (...args: unknown[]) => C, | ||
dependencies: unknown[], | ||
facets: Facet<unknown>[], | ||
): C { | ||
const facetsRef = useRef<Option<unknown>[]>(facets.map(() => NO_VALUE)) | ||
|
||
useEffect(() => { | ||
const unsubscribes = facets.map((facet, index) => { | ||
return facet.observe((value) => { | ||
facetsRef.current[index] = value | ||
}) | ||
}) | ||
|
||
return () => { | ||
unsubscribes.forEach((unsubscribe) => unsubscribe()) | ||
} | ||
// We care about each individual facet and if any is a different reference | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, facets) | ||
|
||
// We care about each individual dependency and if any is a different reference | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
const callbackMemoized = useCallback(callback, dependencies) | ||
|
||
return useCallback( | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
return useCallback<C>( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(...args: any[]): T | NoValue => { | ||
const value = facetRef.current | ||
((...args: any[]) => { | ||
const values = facetsRef.current | ||
|
||
if (value === NO_VALUE) return NO_VALUE | ||
for (const value of values) { | ||
if (value === NO_VALUE) return NO_VALUE | ||
} | ||
|
||
return callbackMemoized(value)(...args) | ||
}, | ||
[callbackMemoized, facetRef], | ||
return callbackMemoized(...values)(...args) | ||
}) as unknown as C, | ||
[callbackMemoized, facetsRef], | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.