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

[v9] refactor!: instance descriptors, dynamically map ThreeElements #2465

Merged
merged 68 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
40d36bb
refactor: use instance descriptors
CodyJasonBennett Aug 27, 2022
5923e1f
fix: remove duplicate args check, align text warning
CodyJasonBennett Aug 27, 2022
816d231
fix: don't overwrite containers in prepare
CodyJasonBennett Aug 27, 2022
cd36cfb
fix: type applyProps, flatten reconciler
CodyJasonBennett Aug 28, 2022
0fdf940
fix: don't recursively remove on swap
CodyJasonBennett Aug 28, 2022
dc4e3b1
refactor(types): automate ThreeElements
CodyJasonBennett Aug 28, 2022
16092cf
fix: don't overwrite dispose
CodyJasonBennett Aug 28, 2022
98d53e2
fix: don't leak react internals into instance props
CodyJasonBennett Aug 29, 2022
1a31024
fix: also exclude primitive props
CodyJasonBennett Aug 29, 2022
5799b6b
chore: cleanup event types
CodyJasonBennett Aug 29, 2022
53c24d1
fix: add back three-types, export ThreeElements as interface
CodyJasonBennett Aug 29, 2022
ab97027
chore(types): cleanup
CodyJasonBennett Aug 29, 2022
3ef4354
fix: check obj type in dispose, reconstruct with args length
CodyJasonBennett Aug 29, 2022
a8e9225
fix(types): move primitive to ThreeElements
CodyJasonBennett Aug 29, 2022
8ffad25
chore(types): cleanup helper signatures, require object in primitives
CodyJasonBennett Aug 30, 2022
02af69a
chore(docs): update onUpdate, TS sections
CodyJasonBennett Aug 30, 2022
903392e
chore(types): cleanup
CodyJasonBennett Aug 30, 2022
cb1c6b9
fix(types): ensure instanceprops override nodeprops
CodyJasonBennett Aug 31, 2022
90fefb9
fix(types): don't exclude functions from JSX
CodyJasonBennett Aug 31, 2022
c360b3b
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 1, 2022
f3c1320
fix: unlink instances on unmount, null-check unmounted containers
CodyJasonBennett Sep 1, 2022
d415383
refactor(RTTR): use Instance type, drop `is`
CodyJasonBennett Sep 1, 2022
6425146
chore(RTTR): remove any cast
CodyJasonBennett Sep 1, 2022
18f9e2b
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 1, 2022
b59c9c8
chore(types): prefer InstanceType, node must extend constructor signa…
CodyJasonBennett Sep 1, 2022
830c101
fix(types): allow unknown props in instanceprops
CodyJasonBennett Sep 2, 2022
ec08f4a
fix(types): pass prototype to instance props
CodyJasonBennett Sep 2, 2022
bcf1450
[v9] fix: handle container effects on completed trees, track instance…
CodyJasonBennett Sep 2, 2022
e62c4fd
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 9, 2022
a9ec8ed
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 9, 2022
f47366d
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 9, 2022
9b4ab11
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 9, 2022
2941762
chore: fix conflicts
CodyJasonBennett Sep 9, 2022
ba41289
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 9, 2022
562bdf1
chore: fix conflict
CodyJasonBennett Sep 9, 2022
1ea33c7
fix(useInstanceHandle): update types to reflect descriptors
CodyJasonBennett Sep 9, 2022
66206d1
refactor(types)!: Node => ThreeElement
CodyJasonBennett Sep 9, 2022
21a98c8
refactor(types)!: remove ReactThreeFiber namespace
CodyJasonBennett Sep 9, 2022
ea8da68
fix(applyProps): overwrite atomic properties if incompatible
CodyJasonBennett Sep 9, 2022
9a1519b
chore(tests): add resolve and applyProps cases
CodyJasonBennett Sep 9, 2022
bd6c9d1
chore: fix conflicts
CodyJasonBennett Sep 9, 2022
5712ec1
chore(tests): add utils coverage
CodyJasonBennett Sep 9, 2022
a5c53ad
chore(tests): mock Zustand store in utils
CodyJasonBennett Sep 9, 2022
2e7a951
chore(utils): cleanup
CodyJasonBennett Sep 9, 2022
fd3ea46
chore(tests): harden renderer lifecycle tests
CodyJasonBennett Sep 10, 2022
f357028
chore: mock scheduler/unstable_scheduleCallback
CodyJasonBennett Sep 10, 2022
9289a30
fix(renderer): don't mutate while removing recursively
CodyJasonBennett Sep 10, 2022
e58afbb
chore(tests): remove scheduler workaround
CodyJasonBennett Sep 10, 2022
c6aa939
fix(core): unlink on no preference
CodyJasonBennett Sep 10, 2022
690f112
chore(tests): harden primitive children dispose case
CodyJasonBennett Sep 10, 2022
7d1db95
fix(applyProps): don't try to construct literals on HMR
CodyJasonBennett Sep 10, 2022
972106c
refactor(applyProps): pick from root obj on HMR
CodyJasonBennett Sep 10, 2022
5cb947b
fix(types): primitive accepts truthy non-literal types
CodyJasonBennett Sep 10, 2022
eeaebd3
fix(renderer): revalidate args on prop change
CodyJasonBennett Sep 10, 2022
556da62
fix(core): don't mutate on swap, de-dup events
CodyJasonBennett Sep 10, 2022
d09a5d6
fix(applyProps): handle event handler HMR
CodyJasonBennett Sep 10, 2022
de5c80f
chore(tests): prop diffing & updates, harden reconstruct, isolate rec…
CodyJasonBennett Sep 10, 2022
d51a426
chore(tests): cleanup
CodyJasonBennett Sep 10, 2022
78f0c53
chore: cleanup removeChild
CodyJasonBennett Sep 10, 2022
23f250d
fix(applyProps): loosen literal check
CodyJasonBennett Sep 10, 2022
e311e86
chore(applyProps): cleanup
CodyJasonBennett Sep 10, 2022
54319d2
fix(applyProps): validate scalar type
CodyJasonBennett Sep 10, 2022
870b6db
chore(tests): add undefined, layers applyProps cases
CodyJasonBennett Sep 10, 2022
b5081a6
refactor(utils): move HMR to diffProps
CodyJasonBennett Sep 12, 2022
817f427
fix(core): replace old instance on reconstruct
CodyJasonBennett Sep 12, 2022
a969026
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 13, 2022
ab801cc
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 13, 2022
ee6bf62
Merge branch 'v9' into refactor/instance-descriptors
CodyJasonBennett Sep 15, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/src/demos/Lines.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef, useEffect, useState, useCallback, useContext, useMemo } from 'react'
import { extend, Canvas, useThree, ReactThreeFiber } from '@react-three/fiber'
import { extend, Canvas, useThree } from '@react-three/fiber'
import { OrbitControls } from 'three-stdlib'
extend({ OrbitControls })

Expand Down
2 changes: 1 addition & 1 deletion example/src/demos/Pointcloud.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef, useEffect, useState, useCallback, useContext, useMemo } from 'react'
import { extend, Canvas, useThree, ReactThreeFiber } from '@react-three/fiber'
import { extend, Canvas, useThree } from '@react-three/fiber'
import * as THREE from 'three'
import { OrbitControls } from 'three-stdlib'
extend({ OrbitControls })
Expand Down
7 changes: 3 additions & 4 deletions example/typings/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ReactThreeFiber } from '@react-three/fiber'
import { Node } from '@react-three/fiber'
import { OrbitControls } from 'three-stdlib'

import { DotMaterial } from '../src/demos/Pointcloud'

declare module '@react-three/fiber' {
interface ThreeElements {
orbitControls: ReactThreeFiber.Node<OrbitControls, typeof OrbitControls>
dotMaterial: ReactThreeFiber.MaterialNode<DotMaterial, typeof DotMaterial>
orbitControls: Node<typeof OrbitControls>
dotMaterial: Node<typeof DotMaterial>
}
}
18 changes: 10 additions & 8 deletions packages/fiber/src/core/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as THREE from 'three'
import { ContinuousEventPriority, DiscreteEventPriority, DefaultEventPriority } from 'react-reconciler/constants'
import { getRootState } from './utils'
import type { UseBoundStore } from 'zustand'
import type { Instance } from './renderer'
import type { Instance } from './types'
import type { RootState } from './store'

export interface Intersection extends THREE.Intersection {
Expand Down Expand Up @@ -173,7 +173,7 @@ export function createEvents(store: UseBoundStore<RootState>) {
function filterPointerEvents(objects: THREE.Object3D[]) {
return objects.filter((obj) =>
['Move', 'Over', 'Enter', 'Out', 'Leave'].some(
(name) => (obj as unknown as Instance).__r3f?.handlers[('onPointer' + name) as keyof EventHandlers],
(name) => ((obj as any).__r3f as Instance | undefined)?.handlers[('onPointer' + name) as keyof EventHandlers],
),
)
}
Expand Down Expand Up @@ -239,7 +239,8 @@ export function createEvents(store: UseBoundStore<RootState>) {
let eventObject: THREE.Object3D | null = hit.object
// Bubble event up
while (eventObject) {
if ((eventObject as unknown as Instance).__r3f?.eventCount) intersections.push({ ...hit, eventObject })
if (((eventObject as any).__r3f as Instance | undefined)?.eventCount)
intersections.push({ ...hit, eventObject })
eventObject = eventObject.parent
}
}
Expand Down Expand Up @@ -369,10 +370,10 @@ export function createEvents(store: UseBoundStore<RootState>) {
)
) {
const eventObject = hoveredObj.eventObject
const instance = (eventObject as unknown as Instance).__r3f
const handlers = instance?.handlers
const instance = (eventObject as any).__r3f as Instance | undefined
internal.hovered.delete(makeId(hoveredObj))
if (instance?.eventCount) {
const handlers = instance.handlers
// Clear out intersects, they are outdated by now
const data = { ...hoveredObj, intersections }
handlers.onPointerOut?.(data as ThreeEvent<PointerEvent>)
Expand Down Expand Up @@ -434,10 +435,11 @@ export function createEvents(store: UseBoundStore<RootState>) {

handleIntersects(hits, event, delta, (data: ThreeEvent<DomEvent>) => {
const eventObject = data.eventObject
const instance = (eventObject as unknown as Instance).__r3f
const handlers = instance?.handlers
const instance = (eventObject as any).__r3f as Instance | undefined

// Check presence of handlers
if (!instance?.eventCount) return
const handlers = instance.handlers

if (isPointerMove) {
// Move event ...
Expand Down Expand Up @@ -488,7 +490,7 @@ export function createEvents(store: UseBoundStore<RootState>) {

function pointerMissed(event: MouseEvent, objects: THREE.Object3D[]) {
objects.forEach((object: THREE.Object3D) =>
(object as unknown as Instance).__r3f?.handlers.onPointerMissed?.(event),
((object as any).__r3f as Instance | undefined)?.handlers.onPointerMissed?.(event),
)
}

Expand Down
16 changes: 7 additions & 9 deletions packages/fiber/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as React from 'react'
import { ConcurrentRoot } from 'react-reconciler/constants'
import create, { StoreApi, UseBoundStore } from 'zustand'

import * as ReactThreeFiber from '../three-types'
import {
Renderer,
createStore,
Expand All @@ -19,9 +18,9 @@ import {
FrameloopLegacy,
Frameloop,
} from './store'
import { createRenderer, extend, Root } from './renderer'
import { reconciler, extend, Root } from './renderer'
import { createLoop, addEffect, addAfterEffect, addTail } from './loop'
import { getEventPriority, EventManager, ComputeFunction } from './events'
import { EventManager, ComputeFunction } from './events'
import {
is,
dispose,
Expand All @@ -31,14 +30,15 @@ import {
useIsomorphicLayoutEffect,
Camera,
updateCamera,
applyProps,
} from './utils'
import { useStore } from './hooks'
import { Stage, Lifecycle, Stages } from './stages'
import { OffscreenCanvas } from 'three'
import { InstanceProps } from './types'

const roots = new Map<Element, Root>()
const { invalidate, advance } = createLoop(roots)
const { reconciler, applyProps } = createRenderer(roots, getEventPriority)
const shallowLoose = { objects: 'shallow', strict: false } as EquConfig

type Properties<T> = Pick<T, { [K in keyof T]: T[K] extends (_: any) => any ? never : K }[keyof T]>
Expand Down Expand Up @@ -87,11 +87,9 @@ export type RenderProps<TCanvas extends Element> = {
/** A `THREE.Camera` instance or props that go into the default camera */
camera?: (
| Camera
| Partial<
ReactThreeFiber.Object3DNode<THREE.Camera, typeof THREE.Camera> &
ReactThreeFiber.Object3DNode<THREE.PerspectiveCamera, typeof THREE.PerspectiveCamera> &
ReactThreeFiber.Object3DNode<THREE.OrthographicCamera, typeof THREE.OrthographicCamera>
>
| InstanceProps<typeof THREE.Camera>
| InstanceProps<typeof THREE.PerspectiveCamera>
| InstanceProps<typeof THREE.OrthographicCamera>
) & {
/** Flags the camera as manual, putting projection into your own hands */
manual?: boolean
Expand Down
Loading