Skip to content

Commit

Permalink
refactor(RTTR): get memoized props from instance fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Feb 14, 2023
1 parent 151abf0 commit bce99ae
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
7 changes: 6 additions & 1 deletion packages/fiber/src/core/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type Root = { fiber: Reconciler.FiberRoot; store: UseBoundStore<RootState

export type LocalState = {
type: string
fiber: Reconciler.Fiber | null
root: UseBoundStore<RootState>
// objects and parent are used when children are added with `attach` instead of being added to the Object3D scene graph
objects: Instance[]
Expand Down Expand Up @@ -88,6 +89,8 @@ function createRenderer<TCanvas>(_roots: Map<TCanvas, Root>, _getEventPriority?:
type: string,
{ args = [], attach, ...props }: InstanceProps,
root: UseBoundStore<RootState>,
_hostContext: null,
fiber: Reconciler.Fiber,
) {
let name = `${type[0].toUpperCase()}${type.slice(1)}`
let instance: Instance
Expand All @@ -113,6 +116,7 @@ function createRenderer<TCanvas>(_roots: Map<TCanvas, Root>, _getEventPriority?:
type,
root,
attach,
fiber,
})
}

Expand All @@ -127,6 +131,7 @@ function createRenderer<TCanvas>(_roots: Map<TCanvas, Root>, _getEventPriority?:
// why it passes "true" here
// There is no reason to apply props to injects
if (name !== 'inject') applyProps(instance, props)
instance.__r3f.fiber = fiber
return instance
}

Expand Down Expand Up @@ -250,7 +255,7 @@ function createRenderer<TCanvas>(_roots: Map<TCanvas, Root>, _getEventPriority?:
const parent = instance.__r3f?.parent
if (!parent) return

const newInstance = createInstance(type, newProps, instance.__r3f.root)
const newInstance = createInstance(type, newProps, instance.__r3f.root, null, fiber)

// https://github.com/pmndrs/react-three-fiber/issues/1348
// When args change the instance has to be re-constructed, which then
Expand Down
1 change: 1 addition & 0 deletions packages/fiber/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export function prepare<T = THREE.Object3D>(object: T, state?: Partial<LocalStat
handlers: {},
objects: [],
parent: null,
fiber: null,
...state,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Array [
-1,
1,
],
"attach": "attributes-position",
"count": 6,
"itemSize": 3,
},
Expand All @@ -40,13 +41,15 @@ Array [
],
"props": Object {
"args": Array [],
"attach": "geometry",
},
"type": "bufferGeometry",
},
Object {
"children": Array [],
"props": Object {
"args": Array [],
"attach": "material",
"color": "hotpink",
},
"type": "meshBasicMaterial",
Expand All @@ -65,6 +68,7 @@ Array [
0,
255,
],
"attach": "background",
},
"type": "color",
},
Expand Down Expand Up @@ -94,6 +98,7 @@ Array [
0,
0,
],
"attach": "background",
},
"type": "color",
},
Expand All @@ -113,6 +118,7 @@ Array [
0,
255,
],
"attach": "background",
},
"type": "color",
},
Expand All @@ -132,6 +138,7 @@ Array [
0,
0,
],
"attach": "background",
},
"type": "color",
},
Expand Down Expand Up @@ -304,8 +311,8 @@ Array [
"children": Array [],
"props": Object {
"args": Array [
2,
2,
6,
6,
],
},
"type": "boxGeometry",
Expand Down
4 changes: 2 additions & 2 deletions packages/test-renderer/src/createTestInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Object3D } from 'three'

import type { MockInstance, MockScene, Obj, TestInstanceChildOpts } from './types/internal'

import { expectOne, matchProps, findAll } from './helpers/testInstance'
import { expectOne, matchProps, findAll, getMemoizedProps } from './helpers/testInstance'

export class ReactThreeTestInstance<TInstance extends Object3D = Object3D> {
_fiber: MockInstance
Expand All @@ -20,7 +20,7 @@ export class ReactThreeTestInstance<TInstance extends Object3D = Object3D> {
}

public get props(): Obj {
return this._fiber.__r3f.memoizedProps
return getMemoizedProps(this._fiber)
}

public get parent(): ReactThreeTestInstance | null {
Expand Down
18 changes: 17 additions & 1 deletion packages/test-renderer/src/helpers/testInstance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { ReactThreeTestInstance } from '../createTestInstance'
import type { Obj } from '../types/internal'
import type { MockInstance, Obj } from '../types/internal'

const REACT_INTERNAL_PROPS = ['children', 'key', 'ref']

export function getMemoizedProps(instance: MockInstance): Record<string, unknown> {
const props: Record<string, unknown> = { args: [] }

// Gets only instance props from instance Fiber
const fiber = instance.__r3f.fiber?.alternate ?? instance.__r3f.fiber
if (fiber) {
for (const key in fiber.memoizedProps) {
if (!REACT_INTERNAL_PROPS.includes(key)) props[key] = fiber.memoizedProps[key]
}
}

return props
}

export const expectOne = <TItem>(items: TItem[], msg: string) => {
if (items.length === 1) {
Expand Down
5 changes: 3 additions & 2 deletions packages/test-renderer/src/helpers/tree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { TreeNode, Tree } from '../types/public'
import type { MockSceneChild, MockScene } from '../types/internal'
import { lowerCaseFirstLetter } from './strings'
import { getMemoizedProps } from './testInstance'

const treeObjectFactory = (
type: TreeNode['type'],
Expand All @@ -16,7 +17,7 @@ const toTreeBranch = (obj: MockSceneChild[]): TreeNode[] =>
obj.map((child) => {
return treeObjectFactory(
lowerCaseFirstLetter(child.type || child.constructor.name),
{ ...child.__r3f.memoizedProps },
{ ...getMemoizedProps(child) },
toTreeBranch([...(child.children || []), ...child.__r3f.objects]),
)
})
Expand All @@ -25,7 +26,7 @@ export const toTree = (root: MockScene): Tree =>
root.children.map((obj) =>
treeObjectFactory(
lowerCaseFirstLetter(obj.type),
{ ...obj.__r3f.memoizedProps },
{ ...getMemoizedProps(obj) },
toTreeBranch([...(obj.children as MockSceneChild[]), ...(obj.__r3f.objects as MockSceneChild[])]),
),
)

0 comments on commit bce99ae

Please sign in to comment.