Skip to content

Commit

Permalink
fix(core): unlink primitives on dispose (#2877)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett committed Jun 9, 2023
1 parent de94c5f commit 64c1b2f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
8 changes: 1 addition & 7 deletions packages/fiber/src/core/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,7 @@ function createRenderer<TCanvas>(_roots: Map<TCanvas, Root>, _getEventPriority?:
}

// Remove references
if (child.__r3f) {
delete ((child as Partial<Instance>).__r3f as Partial<LocalState>).root
delete ((child as Partial<Instance>).__r3f as Partial<LocalState>).objects
delete ((child as Partial<Instance>).__r3f as Partial<LocalState>).handlers
delete ((child as Partial<Instance>).__r3f as Partial<LocalState>).memoizedProps
if (!isPrimitive) delete (child as Partial<Instance>).__r3f
}
delete (child as Partial<Instance>).__r3f

// Dispose item whenever the reconciler feels like it
if (shouldDispose && child.dispose && child.type !== 'Scene') {
Expand Down
39 changes: 39 additions & 0 deletions packages/fiber/tests/core/renderer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -945,4 +945,43 @@ describe('renderer', () => {
expect(store.getState().camera).toBe(camera)
expect(camera.name).not.toBe('test')
})

it('should safely handle updates to the object prop', async () => {
const ref = React.createRef<THREE.Object3D>()
const child = React.createRef<THREE.Object3D>()
const attachedChild = React.createRef<THREE.Object3D>()

const Test = (props: JSX.IntrinsicElements['primitive']) => (
<primitive {...props} ref={ref}>
<object3D ref={child} />
<object3D ref={attachedChild} attach="userData-attach" />
</primitive>
)

const object1 = new THREE.Object3D()
const child1 = new THREE.Object3D()
object1.add(child1)

const object2 = new THREE.Object3D()
const child2 = new THREE.Object3D()
object2.add(child2)

// Initial
await act(async () => root.render(<Test object={object1} />))
expect(ref.current).toBe(object1)
expect(ref.current!.children).toStrictEqual([child1, child.current])
expect(ref.current!.userData.attach).toBe(attachedChild.current)

// Update
await act(async () => root.render(<Test object={object2} />))
expect(ref.current).toBe(object2)
expect(ref.current!.children).toStrictEqual([child2, child.current])
expect(ref.current!.userData.attach).toBe(attachedChild.current)

// Revert
await act(async () => root.render(<Test object={object1} />))
expect(ref.current).toBe(object1)
expect(ref.current!.children).toStrictEqual([child1, child.current])
expect(ref.current!.userData.attach).toBe(attachedChild.current)
})
})

0 comments on commit 64c1b2f

Please sign in to comment.