-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
reconciler.tsx
647 lines (567 loc) Β· 21 KB
/
reconciler.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import * as THREE from 'three'
import * as React from 'react'
import Reconciler from 'react-reconciler'
import {
NoEventPriority,
ContinuousEventPriority,
DiscreteEventPriority,
DefaultEventPriority,
} from 'react-reconciler/constants'
import { unstable_IdlePriority as idlePriority, unstable_scheduleCallback as scheduleCallback } from 'scheduler'
import {
diffProps,
applyProps,
invalidateInstance,
attach,
detach,
prepare,
isObject3D,
findInitialRoot,
IsAllOptional,
} from './utils'
import type { RootStore } from './store'
import { removeInteractivity, type EventHandlers } from './events'
import type { ThreeElement } from '../three-types'
// TODO: upstream to DefinitelyTyped for React 19
// https://github.com/facebook/react/issues/28956
type EventPriority = number
type Fiber = Omit<Reconciler.Fiber, 'alternate'> & { refCleanup: null | (() => void); alternate: Fiber | null }
const createReconciler = Reconciler as unknown as <
Type,
Props,
Container,
Instance,
TextInstance,
SuspenseInstance,
HydratableInstance,
FormInstance,
PublicInstance,
HostContext,
ChildSet,
TimeoutHandle,
NoTimeout,
TransitionStatus,
>(
config: Omit<
Reconciler.HostConfig<
Type,
Props,
Container,
Instance,
TextInstance,
SuspenseInstance,
HydratableInstance,
PublicInstance,
HostContext,
null, // updatePayload
ChildSet,
TimeoutHandle,
NoTimeout
>,
'getCurrentEventPriority' | 'prepareUpdate' | 'commitUpdate'
> & {
/**
* This method should mutate the `instance` and perform prop diffing if needed.
*
* The `internalHandle` data structure is meant to be opaque. If you bend the rules and rely on its internal fields, be aware that it may change significantly between versions. You're taking on additional maintenance risk by reading from it, and giving up all guarantees if you write something to it.
*/
commitUpdate?(
instance: Instance,
type: Type,
prevProps: Props,
nextProps: Props,
internalHandle: Reconciler.OpaqueHandle,
): void
// Undocumented
// https://github.com/facebook/react/pull/26722
NotPendingTransition: TransitionStatus | null
// https://github.com/facebook/react/pull/28751
setCurrentUpdatePriority(newPriority: EventPriority): void
getCurrentUpdatePriority(): EventPriority
resolveUpdatePriority(): EventPriority
// https://github.com/facebook/react/pull/28804
resetFormInstance(form: FormInstance): void
// https://github.com/facebook/react/pull/25105
requestPostPaintCallback(callback: (time: number) => void): void
// https://github.com/facebook/react/pull/26025
shouldAttemptEagerTransition(): boolean
// https://github.com/facebook/react/pull/31528
trackSchedulerEvent(): void
// https://github.com/facebook/react/pull/31008
resolveEventType(): null | string
resolveEventTimeStamp(): number
/**
* This method is called during render to determine if the Host Component type and props require some kind of loading process to complete before committing an update.
*/
maySuspendCommit(type: Type, props: Props): boolean
/**
* This method may be called during render if the Host Component type and props might suspend a commit. It can be used to initiate any work that might shorten the duration of a suspended commit.
*/
preloadInstance(type: Type, props: Props): boolean
/**
* This method is called just before the commit phase. Use it to set up any necessary state while any Host Components that might suspend this commit are evaluated to determine if the commit must be suspended.
*/
startSuspendingCommit(): void
/**
* This method is called after `startSuspendingCommit` for each Host Component that indicated it might suspend a commit.
*/
suspendInstance(type: Type, props: Props): void
/**
* This method is called after all `suspendInstance` calls are complete.
*
* Return `null` if the commit can happen immediately.
*
* Return `(initiateCommit: Function) => Function` if the commit must be suspended. The argument to this callback will initiate the commit when called. The return value is a cancellation function that the Reconciler can use to abort the commit.
*
*/
waitForCommitToBeReady(): ((initiateCommit: Function) => Function) | null
},
) => Reconciler.Reconciler<Container, Instance, TextInstance, SuspenseInstance, PublicInstance>
declare module 'react-reconciler/constants' {
const NoEventPriority = 0
}
export interface Root {
fiber: Reconciler.FiberRoot
store: RootStore
}
export type AttachFnType<O = any> = (parent: any, self: O) => () => void
export type AttachType<O = any> = string | AttachFnType<O>
export type ConstructorRepresentation<T = any> = new (...args: any[]) => T
export interface Catalogue {
[name: string]: ConstructorRepresentation
}
// TODO: handle constructor overloads
// https://github.com/pmndrs/react-three-fiber/pull/2931
// https://github.com/microsoft/TypeScript/issues/37079
export type Args<T> = T extends ConstructorRepresentation
? T extends typeof THREE.Color
? [r: number, g: number, b: number] | [color: THREE.ColorRepresentation]
: ConstructorParameters<T>
: any[]
type ArgsProp<P> = P extends ConstructorRepresentation
? IsAllOptional<ConstructorParameters<P>> extends true
? { args?: Args<P> }
: { args: Args<P> }
: { args: unknown[] }
export type InstanceProps<T = any, P = any> = ArgsProp<P> & {
object?: T
visible?: boolean
dispose?: null
attach?: AttachType<T>
onUpdate?: (self: T) => void
}
export interface Instance<O = any> {
root: RootStore
type: string
parent: Instance | null
children: Instance[]
props: InstanceProps<O> & Record<string, unknown>
object: O & { __r3f?: Instance<O> }
eventCount: number
handlers: Partial<EventHandlers>
attach?: AttachType<O>
previousAttach?: any
isHidden: boolean
}
interface HostConfig {
type: string
props: Instance['props']
container: RootStore
instance: Instance
textInstance: void
suspenseInstance: Instance
hydratableInstance: never
formInstance: never
publicInstance: Instance['object']
hostContext: {}
childSet: never
timeoutHandle: number | undefined
noTimeout: -1
TransitionStatus: null
}
export const catalogue: Catalogue = {}
let i = 0
const isConstructor = (object: unknown): object is ConstructorRepresentation => typeof object === 'function'
export function extend<T extends ConstructorRepresentation>(objects: T): React.ExoticComponent<ThreeElement<T>>
export function extend<T extends Catalogue>(objects: T): void
export function extend<T extends Catalogue | ConstructorRepresentation>(
objects: T,
): React.ExoticComponent<ThreeElement<any>> | void {
if (isConstructor(objects)) {
const Component = `${i++}`
catalogue[Component] = objects
return Component as any
} else {
Object.assign(catalogue, objects)
}
}
function validateInstance(type: string, props: HostConfig['props']): void {
// Get target from catalogue
const name = `${type[0].toUpperCase()}${type.slice(1)}`
const target = catalogue[name]
// Validate element target
if (type !== 'primitive' && !target)
throw new Error(
`R3F: ${name} is not part of the THREE namespace! Did you forget to extend? See: https://docs.pmnd.rs/react-three-fiber/api/objects#using-3rd-party-objects-declaratively`,
)
// Validate primitives
if (type === 'primitive' && !props.object) throw new Error(`R3F: Primitives without 'object' are invalid!`)
// Throw if an object or literal was passed for args
if (props.args !== undefined && !Array.isArray(props.args)) throw new Error('R3F: The args prop must be an array!')
}
function createInstance(type: string, props: HostConfig['props'], root: RootStore): HostConfig['instance'] {
validateInstance(type, props)
// Regenerate the R3F instance for primitives to simulate a new object
if (type === 'primitive' && props.object?.__r3f) delete props.object.__r3f
return prepare(props.object, root, type, props)
}
function hideInstance(instance: HostConfig['instance']): void {
if (!instance.isHidden) {
if (instance.props.attach && instance.parent?.object) {
detach(instance.parent, instance)
} else if (isObject3D(instance.object)) {
instance.object.visible = false
}
instance.isHidden = true
invalidateInstance(instance)
}
}
function unhideInstance(instance: HostConfig['instance']): void {
if (instance.isHidden) {
if (instance.props.attach && instance.parent?.object) {
attach(instance.parent, instance)
} else if (isObject3D(instance.object) && instance.props.visible !== false) {
instance.object.visible = true
}
instance.isHidden = false
invalidateInstance(instance)
}
}
// https://github.com/facebook/react/issues/20271
// This will make sure events and attach are only handled once when trees are complete
function handleContainerEffects(parent: Instance, child: Instance, beforeChild?: Instance) {
// Bail if tree isn't mounted or parent is not a container.
// This ensures that the tree is finalized and React won't discard results to Suspense
const state = child.root.getState()
if (!parent.parent && parent.object !== state.scene) return
// Create & link object on first run
if (!child.object) {
// Get target from catalogue
const name = `${child.type[0].toUpperCase()}${child.type.slice(1)}`
const target = catalogue[name]
// Create object
child.object = child.props.object ?? new target(...(child.props.args ?? []))
child.object.__r3f = child
// Set initial props
applyProps(child.object, child.props)
}
// Append instance
if (child.props.attach) {
attach(parent, child)
} else if (isObject3D(child.object) && isObject3D(parent.object)) {
const childIndex = parent.object.children.indexOf(beforeChild?.object)
if (beforeChild && childIndex !== -1) {
child.object.parent = parent.object
parent.object.children.splice(childIndex, 0, child.object)
child.object.dispatchEvent({ type: 'added' })
parent.object.dispatchEvent({ type: 'childadded', child: child.object })
} else {
parent.object.add(child.object)
}
}
// Link subtree
for (const childInstance of child.children) handleContainerEffects(child, childInstance)
// Tree was updated, request a frame
invalidateInstance(child)
}
function appendChild(parent: HostConfig['instance'], child: HostConfig['instance'] | HostConfig['textInstance']) {
if (!child) return
// Link instances
child.parent = parent
parent.children.push(child)
// Attach tree once complete
handleContainerEffects(parent, child)
}
function insertBefore(
parent: HostConfig['instance'],
child: HostConfig['instance'] | HostConfig['textInstance'],
beforeChild: HostConfig['instance'] | HostConfig['textInstance'],
) {
if (!child || !beforeChild) return
// Link instances
child.parent = parent
const childIndex = parent.children.indexOf(beforeChild)
if (childIndex !== -1) parent.children.splice(childIndex, 0, child)
else parent.children.push(child)
// Attach tree once complete
handleContainerEffects(parent, child, beforeChild)
}
function disposeOnIdle(object: any) {
if (typeof object.dispose === 'function') {
const handleDispose = () => {
try {
object.dispose()
} catch {
// no-op
}
}
// In a testing environment, cleanup immediately
if (typeof IS_REACT_ACT_ENVIRONMENT !== 'undefined') handleDispose()
// Otherwise, using a real GPU so schedule cleanup to prevent stalls
else scheduleCallback(idlePriority, handleDispose)
}
}
function removeChild(
parent: HostConfig['instance'],
child: HostConfig['instance'] | HostConfig['textInstance'],
dispose?: boolean,
) {
if (!child) return
// Unlink instances
child.parent = null
const childIndex = parent.children.indexOf(child)
if (childIndex !== -1) parent.children.splice(childIndex, 1)
// Eagerly tear down tree
if (child.props.attach) {
detach(parent, child)
} else if (isObject3D(child.object) && isObject3D(parent.object)) {
parent.object.remove(child.object)
removeInteractivity(findInitialRoot(child), child.object)
}
// Allow objects to bail out of unmount disposal with dispose={null}
const shouldDispose = child.props.dispose !== null && dispose !== false
// Recursively remove instance children
for (let i = child.children.length - 1; i >= 0; i--) {
const node = child.children[i]
removeChild(child, node, shouldDispose)
}
child.children.length = 0
// Unlink instance object
delete child.object.__r3f
// Dispose object whenever the reconciler feels like it.
// Never dispose of primitives because their state may be kept outside of React!
// In order for an object to be able to dispose it
// - has a dispose method
// - cannot be a <primitive object={...} />
// - cannot be a THREE.Scene, because three has broken its own API
if (shouldDispose && child.type !== 'primitive' && child.object.type !== 'Scene') {
disposeOnIdle(child.object)
}
// Tree was updated, request a frame for top-level instance
if (dispose === undefined) invalidateInstance(child)
}
function setFiberRef(fiber: Fiber, publicInstance: HostConfig['publicInstance']): void {
for (const _fiber of [fiber, fiber.alternate]) {
if (_fiber !== null) {
if (typeof _fiber.ref === 'function') {
_fiber.refCleanup?.()
const cleanup = _fiber.ref(publicInstance)
if (typeof cleanup === 'function') _fiber.refCleanup = cleanup
} else if (_fiber.ref) {
_fiber.ref.current = publicInstance
}
}
}
}
const reconstructed: [oldInstance: HostConfig['instance'], props: HostConfig['props'], fiber: Fiber][] = []
function swapInstances(): void {
// Detach instance
for (const [instance] of reconstructed) {
const parent = instance.parent
if (parent) {
if (instance.props.attach) {
detach(parent, instance)
} else if (isObject3D(instance.object) && isObject3D(parent.object)) {
parent.object.remove(instance.object)
}
for (const child of instance.children) {
if (child.props.attach) {
detach(instance, child)
} else if (isObject3D(child.object) && isObject3D(instance.object)) {
instance.object.remove(child.object)
}
}
}
// If the old instance is hidden, we need to unhide it.
// React assumes it can discard instances since they're pure for DOM.
// This isn't true for us since our lifetimes are impure and longliving.
// So, we manually check if an instance was hidden and unhide it.
if (instance.isHidden) unhideInstance(instance)
// Dispose of old object if able
if (instance.object.__r3f) delete instance.object.__r3f
if (instance.type !== 'primitive') disposeOnIdle(instance.object)
}
// Update instance
for (const [instance, props, fiber] of reconstructed) {
instance.props = props
const parent = instance.parent
if (parent) {
// Get target from catalogue
const name = `${instance.type[0].toUpperCase()}${instance.type.slice(1)}`
const target = catalogue[name]
// Create object
instance.object = instance.props.object ?? new target(...(instance.props.args ?? []))
instance.object.__r3f = instance
setFiberRef(fiber, instance.object)
// Set initial props
applyProps(instance.object, instance.props)
if (instance.props.attach) {
attach(parent, instance)
} else if (isObject3D(instance.object) && isObject3D(parent.object)) {
parent.object.add(instance.object)
}
for (const child of instance.children) {
if (child.props.attach) {
attach(instance, child)
} else if (isObject3D(child.object) && isObject3D(instance.object)) {
instance.object.add(child.object)
}
}
// Tree was updated, request a frame
invalidateInstance(instance)
}
}
reconstructed.length = 0
}
// Don't handle text instances, make it no-op
const handleTextInstance = () => {}
const NO_CONTEXT: HostConfig['hostContext'] = {}
let currentUpdatePriority: number = NoEventPriority
// https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactFiberFlags.js
const NoFlags = 0
const Update = 4
export const reconciler = createReconciler<
HostConfig['type'],
HostConfig['props'],
HostConfig['container'],
HostConfig['instance'],
HostConfig['textInstance'],
HostConfig['suspenseInstance'],
HostConfig['hydratableInstance'],
HostConfig['formInstance'],
HostConfig['publicInstance'],
HostConfig['hostContext'],
HostConfig['childSet'],
HostConfig['timeoutHandle'],
HostConfig['noTimeout'],
HostConfig['TransitionStatus']
>({
isPrimaryRenderer: false,
warnsIfNotActing: false,
supportsMutation: true,
supportsPersistence: false,
supportsHydration: false,
createInstance,
removeChild,
appendChild,
appendInitialChild: appendChild,
insertBefore,
appendChildToContainer(container, child) {
const scene = (container.getState().scene as unknown as Instance<THREE.Scene>['object']).__r3f
if (!child || !scene) return
appendChild(scene, child)
},
removeChildFromContainer(container, child) {
const scene = (container.getState().scene as unknown as Instance<THREE.Scene>['object']).__r3f
if (!child || !scene) return
removeChild(scene, child)
},
insertInContainerBefore(container, child, beforeChild) {
const scene = (container.getState().scene as unknown as Instance<THREE.Scene>['object']).__r3f
if (!child || !beforeChild || !scene) return
insertBefore(scene, child, beforeChild)
},
getRootHostContext: () => NO_CONTEXT,
getChildHostContext: () => NO_CONTEXT,
commitUpdate(
instance: HostConfig['instance'],
type: HostConfig['type'],
oldProps: HostConfig['props'],
newProps: HostConfig['props'],
fiber: Fiber,
) {
validateInstance(type, newProps)
let reconstruct = false
// Reconstruct primitives if object prop changes
if (instance.type === 'primitive' && oldProps.object !== newProps.object) reconstruct = true
// Reconstruct instance if args were added or removed
else if (newProps.args?.length !== oldProps.args?.length) reconstruct = true
// Reconstruct instance if args were changed
else if (newProps.args?.some((value, index) => value !== oldProps.args?.[index])) reconstruct = true
// Reconstruct when args or <primitive object={...} have changes
if (reconstruct) {
reconstructed.push([instance, { ...newProps }, fiber])
} else {
// Create a diff-set, flag if there are any changes
const changedProps = diffProps(instance, newProps)
if (Object.keys(changedProps).length) {
Object.assign(instance.props, changedProps)
applyProps(instance.object, changedProps)
}
}
// Flush reconstructed siblings when we hit the last updated child in a sequence
const isTailSibling = fiber.sibling === null || (fiber.flags & Update) === NoFlags
if (isTailSibling) swapInstances()
},
finalizeInitialChildren: () => false,
commitMount() {},
getPublicInstance: (instance) => instance?.object!,
prepareForCommit: () => null,
preparePortalMount: (container) => prepare(container.getState().scene, container, '', {}),
resetAfterCommit: () => {},
shouldSetTextContent: () => false,
clearContainer: () => false,
hideInstance,
unhideInstance,
createTextInstance: handleTextInstance,
hideTextInstance: handleTextInstance,
unhideTextInstance: handleTextInstance,
scheduleTimeout: (typeof setTimeout === 'function' ? setTimeout : undefined) as any,
cancelTimeout: (typeof clearTimeout === 'function' ? clearTimeout : undefined) as any,
noTimeout: -1,
getInstanceFromNode: () => null,
beforeActiveInstanceBlur() {},
afterActiveInstanceBlur() {},
detachDeletedInstance() {},
prepareScopeUpdate() {},
getInstanceFromScope: () => null,
shouldAttemptEagerTransition: () => false,
trackSchedulerEvent: () => {},
resolveEventType: () => null,
resolveEventTimeStamp: () => -1.1,
requestPostPaintCallback() {},
maySuspendCommit: () => false,
preloadInstance: () => true, // true indicates already loaded
startSuspendingCommit() {},
suspendInstance() {},
waitForCommitToBeReady: () => null,
NotPendingTransition: null,
setCurrentUpdatePriority(newPriority: number) {
currentUpdatePriority = newPriority
},
getCurrentUpdatePriority() {
return currentUpdatePriority
},
resolveUpdatePriority() {
if (currentUpdatePriority !== NoEventPriority) return currentUpdatePriority
switch (typeof window !== 'undefined' && window.event?.type) {
case 'click':
case 'contextmenu':
case 'dblclick':
case 'pointercancel':
case 'pointerdown':
case 'pointerup':
return DiscreteEventPriority
case 'pointermove':
case 'pointerout':
case 'pointerover':
case 'pointerenter':
case 'pointerleave':
case 'wheel':
return ContinuousEventPriority
default:
return DefaultEventPriority
}
},
resetFormInstance() {},
})