Skip to content

Commit

Permalink
Fix incorrect initial values with useFacetMemo.
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonicus committed Jan 11, 2024
1 parent 1108578 commit 5904ab8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/@react-facet/core/src/components/Map.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ReactElement, useDeferredValue } from 'react'
import React, { ReactElement } from 'react'
import { useFacetMemo } from '../hooks/useFacetMemo'
import { useFacetUnwrap } from '../hooks/useFacetUnwrap'
import { useFacetMap } from '../hooks/useFacetMap'
Expand All @@ -12,7 +12,7 @@ export type MapProps<T> = {

export const Map = <T,>({ array, children, equalityCheck }: MapProps<T>) => {
// When mounting lists, we always want to defer
const countValue = useDeferredValue(useFacetUnwrap(useFacetMap((array) => array.length, [], [array])))
const countValue = useFacetUnwrap(useFacetMap((array) => array.length, [], [array]))

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions packages/@react-facet/core/src/components/Mount.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactElement, useDeferredValue } from 'react'
import { ReactElement } from 'react'
import { useFacetUnwrap } from '../hooks/useFacetUnwrap'
import { Facet } from '../types'

Expand All @@ -9,6 +9,6 @@ type MountProps = {
}

export const Mount = ({ when, children, condition = true }: MountProps) => {
const whenValue = useDeferredValue(useFacetUnwrap(when))
const whenValue = useFacetUnwrap(when)
return whenValue === condition ? children : null
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export function mapFacetArrayCached<M>(
fn: (...value: unknown[]) => M | NoValue,
equalityCheck?: EqualityCheck<M>,
): Facet<M> {
const initialValues = facets.map((facet) => facet.get())
const hasAllValues = initialValues.reduce<boolean>((prev, curr) => prev && curr !== NO_VALUE, true)
return createFacet<M>({
// pass the equalityCheck to the mapIntoObserveArray to prevent even triggering the observable
startSubscription: mapIntoObserveArray(facets, fn, equalityCheck),
initialValue: NO_VALUE,
initialValue: hasAllValues ? fn(...initialValues) : NO_VALUE,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ export function mapFacetSingleCached<T, M>(
fn: (value: T) => M | NoValue,
equalityCheck?: EqualityCheck<M>,
): Facet<M> {
const initialValue = facets.get()
return createFacet<M>({
// pass the equalityCheck to the mapIntoObserveSingle to prevent even triggering the observable
startSubscription: mapIntoObserveSingle(facets, fn, equalityCheck),
initialValue: NO_VALUE,
initialValue: initialValue !== NO_VALUE ? fn(initialValue) : NO_VALUE,
})
}
3 changes: 2 additions & 1 deletion packages/@react-facet/dom-fiber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"prepublish": "yarn build"
},
"dependencies": {
"react-reconciler": "^0.28.0"
"react-reconciler": "^0.28.0",
"scheduler": "^0.22.0"
},
"peerDependencies": {
"@react-facet/core": "0.5.3",
Expand Down
3 changes: 3 additions & 0 deletions packages/@react-facet/dom-fiber/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export * from './types'
export * from './createPortal'
export * from './createFiberRoot'
export * from './createReconciler'
import scheduler from 'scheduler'

// scheduler.unstable_forceFrameRate(120)

export type RootType = {
render(children: ReactElement): void
Expand Down

0 comments on commit 5904ab8

Please sign in to comment.