This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathReact3Renderer.js
1156 lines (984 loc) · 37 KB
/
React3Renderer.js
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as THREE from 'three';
import reactElementWrapper from 'react/lib/ReactElement';
import ReactCurrentOwner from 'react/lib/ReactCurrentOwner';
import { Component as ReactComponent } from 'react/lib/ReactBaseClasses';
import KeyEscapeUtils from 'react/lib/KeyEscapeUtils';
import emptyObject from 'fbjs/lib/emptyObject';
import invariant from 'fbjs/lib/invariant';
import warning from 'fbjs/lib/warning';
import ReactInstanceMap from 'react-dom/lib/ReactInstanceMap';
import ReactReconciler from 'react-dom/lib/ReactReconciler';
import ReactUpdates from 'react-dom/lib/ReactUpdates';
import ReactUpdateQueue from 'react-dom/lib/ReactUpdateQueue';
import ReactInjection from 'react-dom/lib/ReactInjection';
import ReactReconcileTransaction from 'react-dom/lib/ReactReconcileTransaction';
import ReactDefaultBatchingStrategy from 'react-dom/lib/ReactDefaultBatchingStrategy';
import traverseAllChildren from 'react-dom/lib/traverseAllChildren';
import getHostComponentFromComposite from 'react-dom/lib/getHostComponentFromComposite';
import shouldUpdateReactComponent from 'react-dom/lib/shouldUpdateReactComponent';
import ReactInstrumentation from 'react-dom/lib/ReactInstrumentation';
import react3ContainerInfo from './React3ContainerInfo';
import EventDispatcher from './utils/EventDispatcher';
import InternalComponent from './InternalComponent';
import React3ComponentTree from './React3ComponentTree';
import ElementDescriptorContainer from './ElementDescriptorContainer';
import React3CompositeComponentWrapper from './React3CompositeComponentWrapper';
import ID_PROPERTY_NAME from './utils/idPropertyName';
import removeDevTool from './utils/removeDevTool';
let getDeclarationErrorAddendum;
let staticDebugIdHack;
let ReactComponentTreeHook;
if (process.env.NODE_ENV !== 'production') {
/* eslint-disable global-require */
if (!ReactComponentTreeHook) {
ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
}
/* eslint-enable global-require */
}
if (process.env.NODE_ENV !== 'production') {
staticDebugIdHack = 0;
// prop type helpers
// the warnings for propTypes will not say <anonymous>.
// Some performance is sacrificed for this.
// TODO: could have an env variable to disable this?
if (!THREE._renamed) {
THREE._renamed = true;
THREE.Vector2.displayName = 'THREE.Vector2';
THREE.Vector3.displayName = 'THREE.Vector3';
THREE.Quaternion.displayName = 'THREE.Quaternion';
THREE.Color.displayName = 'THREE.Color';
THREE.Shape.displayName = 'THREE.Shape';
THREE.Euler.displayName = 'THREE.Euler';
THREE.Fog.displayName = 'THREE.Fog';
}
getDeclarationErrorAddendum = (owner) => {
if (owner) {
const name = owner.getName();
if (name) {
return ` Check the render method of \`${name}\`.`;
}
}
return '';
};
}
/**
* Unmounts a component and removes it from the DOM.
*
* @param {ReactComponent} instance React component instance.
* @param {*} container DOM element to unmount from.
* @param {bool} safely
* @final
* @internal
* @see {ReactMount.unmountComponentAtNode}
*/
function unmountComponentFromNode(instance, container, safely) {
if (process.env.NODE_ENV !== 'production') {
ReactInstrumentation.debugTool.onBeginFlush();
}
ReactReconciler.unmountComponent(instance, safely);
if (process.env.NODE_ENV !== 'production') {
ReactInstrumentation.debugTool.onEndFlush();
}
}
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
class TopLevelWrapper extends ReactComponent {
render() {
return this.props.child;
}
static isReactComponent = {};
static isReactTopLevelWrapper = true;
}
if (process.env.NODE_ENV !== 'production') {
TopLevelWrapper.displayName = 'TopLevelWrapper';
}
function internalGetID(markup) {
return (markup && markup[ID_PROPERTY_NAME]) || '';
}
// see ReactMount.js:getReactRootElementInContainer
/**
* @param {THREE.Object3D|HTMLCanvasElement} container That may contain
* a React component
* @return {?*} The markup that may have the reactRoot ID, or null.
*/
function getReactRootMarkupInContainer(container) {
if (!container) {
return null;
}
// in ReactMount this is container.firstChild.
return (container.userData && container.userData.markup
&& container.userData.markup.childrenMarkup[0]) || null;
}
/**
* Check if the type reference is a known internal type. I.e. not a user
* provided composite type.
*
* @param {function} type
* @return {boolean} Returns true if this is a valid internal type.
*/
function isInternalComponentType(type) {
return typeof type === 'function'
&& typeof type.prototype !== 'undefined'
&& typeof type.prototype.mountComponent === 'function'
&& typeof type.prototype.receiveComponent === 'function';
}
class React3Renderer {
// to be used by modules e.g. mouse input ( see examples )
static eventDispatcher = new EventDispatcher();
/**
* Returns the THREE.js object rendered by this element.
*
* @param {React.Component|THREE.Object3D|HTMLCanvasElement} componentOrElement
* @return {?THREE.Object3D} The root node of this element.
*/
static findTHREEObject(componentOrElement) {
if (process.env.NODE_ENV !== 'production') {
const owner = ReactCurrentOwner.current;
if (owner !== null) {
if (process.env.NODE_ENV !== 'production') {
warning(
owner._warnedAboutRefsInRender,
'%s is accessing findDOMNode inside its render(). ' +
'render() should be a pure function of props and state. It should ' +
'never access something that requires stale data from the previous ' +
'render, such as refs. Move this logic to componentDidMount and ' +
'componentDidUpdate instead.',
owner.getName() || 'A component'
);
}
owner._warnedAboutRefsInRender = true;
}
}
if (componentOrElement === null) {
return null;
}
if (componentOrElement instanceof THREE.Object3D ||
componentOrElement instanceof HTMLCanvasElement) {
return componentOrElement;
}
if (ReactInstanceMap.has(componentOrElement)) {
let instance = ReactInstanceMap.get(componentOrElement);
instance = getHostComponentFromComposite(instance);
return instance ? React3ComponentTree.getMarkupFromInstance(instance).threeObject : null;
}
if (!(componentOrElement.render === null || typeof componentOrElement.render !== 'function')) {
if (process.env.NODE_ENV !== 'production') {
invariant(false, 'Component (with keys: %s) contains `render` method ' +
'but is not mounted', Object.keys(componentOrElement));
} else {
invariant(false);
}
}
if (process.env.NODE_ENV !== 'production') {
invariant(false,
'Element appears to be neither ReactComponent, ' +
'a THREE.js object, nor a HTMLCanvasElement (keys: %s)',
Object.keys(componentOrElement));
} else {
invariant(false);
}
return null;
}
/**
* @see ReactChildReconciler.updateChildren
*
* Cloned because it uses
* @see React3Renderer.instantiateReactComponent
*
* Updates the rendered children and returns a new set of children.
*
* @param {?object} prevChildren Previously initialized set of children.
* @param {?object} nextChildren Flat child element maps.
* @param mountImages
* @param {?object} removedMarkups The map for removed nodes.
* @param {ReactReconcileTransaction} transaction
* @param hostParent
* @param hostContainerInfo
* @param {object} context
* @param selfDebugID
* @return {?object} A new set of child instances.
* @internal
*/
updateChildren(prevChildren,
nextChildren,
mountImages,
removedMarkups,
transaction,
hostParent,
hostContainerInfo,
context,
selfDebugID // 0 in production and for roots
) {
// We currently don't have a way to track moves here but if we use iterators
// instead of for..in we can zip the iterators and check if an item has
// moved.
// TODO: If nothing has changed, return the prevChildren object so that we
// can quickly bailout.
if (!nextChildren && !prevChildren) {
return null;
}
if (nextChildren) {
const nextChildrenKeys = Object.keys(nextChildren);
for (let i = 0; i < nextChildrenKeys.length; ++i) {
const childName = nextChildrenKeys[i];
const prevChild = prevChildren && prevChildren[childName];
const prevElement = prevChild && prevChild._currentElement;
const nextElement = nextChildren[childName];
if (prevChild !== null && prevChild !== undefined
&& shouldUpdateReactComponent(prevElement, nextElement)) {
ReactReconciler.receiveComponent(
prevChild, nextElement, transaction, context
);
if (prevChild._forceRemountOfComponent) {
removedMarkups[childName] = prevChild.getHostMarkup();
ReactReconciler.unmountComponent(prevChild, false);
const nextChildInstance = this.instantiateReactComponent(nextElement, true);
nextChildren[childName] = nextChildInstance;
// Creating mount image now ensures refs are resolved in right order
// (see https://github.com/facebook/react/pull/7101 for explanation).
const nextChildMountImage = ReactReconciler.mountComponent(
nextChildInstance,
transaction,
hostParent,
hostContainerInfo,
context,
selfDebugID
);
mountImages.push(nextChildMountImage);
} else {
nextChildren[childName] = prevChild;
}
} else {
if (prevChild) {
removedMarkups[childName] = prevChild.getHostMarkup();
ReactReconciler.unmountComponent(prevChild, false);
}
// The child must be instantiated before it's mounted.
const nextChildInstance = this.instantiateReactComponent(nextElement, true);
nextChildren[childName] = nextChildInstance;
// Creating mount image now ensures refs are resolved in right order
// (see https://github.com/facebook/react/pull/7101 for explanation).
const nextChildMountImage = ReactReconciler.mountComponent(
nextChildInstance,
transaction,
hostParent,
hostContainerInfo,
context,
selfDebugID /* parentDebugID */
);
mountImages.push(nextChildMountImage);
}
}
}
if (prevChildren) {
// Unmount children that are no longer present.
const prevChildrenKeys = Object.keys(prevChildren);
for (let i = 0; i < prevChildrenKeys.length; ++i) {
const childName = prevChildrenKeys[i];
if (!(nextChildren && nextChildren.hasOwnProperty(childName))) {
const prevChild = prevChildren[childName];
removedMarkups[childName] = prevChild.getHostMarkup();
ReactReconciler.unmountComponent(prevChild, false);
}
}
}
return nextChildren;
}
getElementDescriptor(name) {
return this.threeElementDescriptors[name];
}
constructor() {
this._instancesByReactRootID = {};
if (process.env.NODE_ENV !== 'production') {
this.rootMarkupsByReactRootID = {};
}
this.nextMountID = 1;
this.globalIdCounter = 1;
this.nextReactRootIndex = 0;
this.threeElementDescriptors = new ElementDescriptorContainer(this).descriptors;
this._highlightElement = document.createElement('div');
this._highlightCache = null;
if (process.env.NODE_ENV !== 'production') {
this._nextDebugID = 1;
this._debugIdPrefix = staticDebugIdHack++;
}
if (process.env.NODE_ENV !== 'production' || process.env.ENABLE_REACT_ADDON_HOOKS === 'true') {
this._agent = null;
this._onHideHighlightFromInspector = () => {
if (this._highlightCache && this._highlightCache
.threeObject.userData.react3internalComponent) {
const internalComponent = this._highlightCache
.threeObject.userData.react3internalComponent;
internalComponent.hideHighlight();
this._highlightCache = null;
}
};
this._onHighlightFromInspector = (highlightInfo) => {
if (highlightInfo.node === this._highlightElement) {
if (this._highlightCache && this._highlightCache
.threeObject.userData.react3internalComponent) {
const internalComponent = this._highlightCache
.threeObject.userData.react3internalComponent;
internalComponent.highlightComponent();
}
}
};
this._hookAgent = (agent) => {
this._agent = agent;
// agent.on('startInspecting', (...args) => {
// console.log('start inspecting?', args);
// });
// agent.on('setSelection', (...args) => {
// console.log('set selection?', args);
// });
// agent.on('selected', (...args) => {
// console.log('selected?', args);
// });
agent.on('highlight', this._onHighlightFromInspector);
agent.on('hideHighlight', this._onHideHighlightFromInspector);
// agent.on('highlightMany', (...args) => {
// console.log('highlightMany?', args);
// });
};
// Inject the runtime into a devtools global hook regardless of browser.
// Allows for debugging when the hook is injected on the page.
if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined'
&& typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.inject === 'function') {
this._devToolsRendererDefinition = {
ComponentTree: {
getClosestInstanceFromNode(node) {
return React3ComponentTree.getClosestInstanceFromMarkup(node);
},
getNodeFromInstance(instInput) {
let inst = instInput;
// inst is an internal instance (but could be a composite)
while (inst._renderedComponent) {
inst = inst._renderedComponent;
}
if (inst) {
return React3ComponentTree.getMarkupFromInstance(inst);
}
return null;
},
},
Mount: this,
Reconciler: ReactReconciler,
TextComponent: InternalComponent,
};
const rendererListener = (info) => {
this._reactDevtoolsRendererId = info.id;
this._rendererListenerCleanup();
delete this._rendererListenerCleanup;
};
this._rendererListenerCleanup = __REACT_DEVTOOLS_GLOBAL_HOOK__
.sub('renderer', rendererListener);
__REACT_DEVTOOLS_GLOBAL_HOOK__.inject(this._devToolsRendererDefinition);
if (typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.reactDevtoolsAgent !== 'undefined'
&& __REACT_DEVTOOLS_GLOBAL_HOOK__.reactDevtoolsAgent) {
const agent = __REACT_DEVTOOLS_GLOBAL_HOOK__.reactDevtoolsAgent;
this._hookAgent(agent);
} else {
this._devtoolsCallbackCleanup = __REACT_DEVTOOLS_GLOBAL_HOOK__
.sub('react-devtools', (agent) => {
this._devtoolsCallbackCleanup();
this._hookAgent(agent);
});
}
}
}
}
/**
* @see ReactChildReconciler.instantiateChild
* Cloned because it uses
* @see React3Renderer.instantiateReactComponent
*
* @param childInstances
* @param child
* @param name
* @param selfDebugID
*/
instantiateChild = (childInstances, child, name, selfDebugID) => {
// We found a component instance.
const keyUnique = (childInstances[name] === undefined);
if (process.env.NODE_ENV !== 'production') {
if (!keyUnique) {
warning(
false,
'flattenChildren(...): Encountered two children with the same key, ' +
'`%s`. Child keys must be unique; when two children share a key, only ' +
'the first child will be used.%s',
KeyEscapeUtils.unescape(name),
ReactComponentTreeHook.getStackAddendumByID(selfDebugID)
);
}
}
if (child !== null && keyUnique) {
childInstances[name] = this.instantiateReactComponent(child, true);
}
};
/**
* @see ReactChildReconciler.instantiateChildren
* Cloned because it uses
* @see React3Renderer.instantiateChild
*
* Generates a "mount image" for each of the supplied children. In the case
* of `ReactDOMComponent`, a mount image is a string of markup.
*
* @param {?object} nestedChildNodes Nested child maps.
* @param transaction
* @param context
* @param selfDebugID
* @return {?object} A set of child instances.
* @internal
*/
instantiateChildren(nestedChildNodes,
transaction,
context,
selfDebugID // 0 in production and for roots
) {
if (nestedChildNodes === null) {
return null;
}
const childInstances = {};
if (process.env.NODE_ENV !== 'production') {
traverseAllChildren(
nestedChildNodes,
(childInsts, child, name) => this.instantiateChild(
childInsts,
child,
name,
selfDebugID
),
childInstances
);
} else {
traverseAllChildren(nestedChildNodes, this.instantiateChild, childInstances);
}
return childInstances;
}
containsChild(container, markup) {
const childrenMarkup = container.userData.markup.childrenMarkup;
for (let i = 0; i < childrenMarkup.length; i++) {
if (childrenMarkup[i] === markup) {
return true;
}
}
return false;
}
// DO NOT RENAME
// used by react devtools!
findNodeHandle = (instance) => {
const inst = React3ComponentTree.getRenderedHostOrTextFromComponent(instance);
if (!inst || !inst._threeObject) {
return null;
}
const markup = React3ComponentTree.getMarkupFromInstance(inst);
this._highlightCache = markup;
return this._highlightElement;
};
// used by react devtools
nativeTagToRootNodeID = () => 0;
hostTagToRootNodeID = () => 0;
_mountImageIntoNode(markup,
container,
instance,
shouldReuseMarkup,
transaction) { // eslint-disable-line no-unused-vars
// TODO try to do server-side rendering for THREE
if (!container.userData) {
// it has to be a HTMLCanvasElement I guess?
invariant(container instanceof HTMLCanvasElement,
'The root container can only be a THREE.js object ' +
'(with an userData property) or HTMLCanvasElement.');
container.userData = {
_createdByReact3: true,
};
}
const rootImage = markup;
const rootMarkup = {
threeObject: container,
parentMarkup: null,
childrenMarkup: [rootImage],
toJSON: () => '---MARKUP---',
};
Object.assign(container.userData, {
object3D: container,
toJSON: () => '---USERDATA---',
markup: rootMarkup,
});
rootImage.parentMarkup = rootMarkup;
const descriptorForChild = this.threeElementDescriptors[rootImage.elementType];
descriptorForChild.setParent(rootImage.threeObject, rootMarkup.threeObject);
// all objects now added can be marked as added to scene now!
rootImage.threeObject.mountedIntoRoot();
const firstChild = container.userData.markup.childrenMarkup[0];
React3ComponentTree.precacheMarkup(instance, firstChild);
if (process.env.NODE_ENV !== 'production') {
const hostInstance = React3ComponentTree.getInstanceFromMarkup(firstChild);
if (hostInstance._debugID !== 0) {
ReactInstrumentation.debugTool.onHostOperation({
instanceID: hostInstance._debugID,
type: 'mount',
payload: markup.toString(),
});
}
}
}
/**
*
* @param nextElement A react element
* @param container A canvas or a THREE.js object
* @param callback The callback function
* @returns {*}
*/
render(nextElement, container, callback) {
return this._renderSubtreeIntoContainer(null, nextElement, container, callback);
}
getHostRootInstanceInContainer(container) {
const rootMarkup = getReactRootMarkupInContainer(container);
const prevHostInstance = rootMarkup && React3ComponentTree.getInstanceFromMarkup(rootMarkup);
return prevHostInstance && !prevHostInstance._hostParent ? prevHostInstance : null;
}
getTopLevelWrapperInContainer(container) {
const root = this.getHostRootInstanceInContainer(container);
if (root) {
invariant(!!root._hostContainerInfo, 'Root should have native container info %s',
' but it does not');
}
return root ? root._hostContainerInfo._topLevelWrapper : null;
}
_renderSubtreeIntoContainer(parentComponent, nextElement, container, callback) {
if (!reactElementWrapper.isValidElement(nextElement)) {
if (process.env.NODE_ENV !== 'production') {
if (typeof nextElement === 'string') {
invariant(false, 'React3Renderer.render(): Invalid component element.%s',
' Instead of passing an element string, make sure to instantiate ' +
'it by passing it to React.createElement.');
} else if (typeof nextElement === 'function') {
invariant(false, 'React3Renderer.render(): Invalid component element.%s',
' Instead of passing a component class, make sure to instantiate ' +
'it by passing it to React.createElement.');
} else if (nextElement !== null && nextElement.props !== undefined) {
invariant(false, 'React3Renderer.render(): Invalid component element.%s',
' This may be caused by unintentionally loading two independent ' +
'copies of React.');
} else {
invariant(false, 'React3Renderer.render(): Invalid component element.');
}
} else {
invariant(false);
}
}
const nextWrappedElement = reactElementWrapper.createElement(
TopLevelWrapper,
{ child: nextElement }
);
let nextContext;
if (parentComponent) {
const parentInst = ReactInstanceMap.get(parentComponent);
nextContext = parentInst._processChildContext(parentInst._context);
} else {
nextContext = emptyObject;
}
const prevComponent = this.getTopLevelWrapperInContainer(container);
if (prevComponent) {
const prevWrappedElement = prevComponent._currentElement;
const prevElement = prevWrappedElement.props.child;
if (shouldUpdateReactComponent(prevElement, nextElement)) {
const publicInst = prevComponent._renderedComponent.getPublicInstance();
const updatedCallback = callback &&
(() => {
callback.call(publicInst);
});
this._updateRootComponent(prevComponent,
nextWrappedElement,
nextContext,
container,
updatedCallback);
return publicInst;
}
this.unmountComponentAtNode(container);
}
// aka first child
const reactRootMarkup = getReactRootMarkupInContainer(container);
const containerHasReactMarkup = reactRootMarkup && !!internalGetID(reactRootMarkup);
// containerHasNonRootReactChild not implemented
const shouldReuseMarkup = containerHasReactMarkup && !prevComponent;
const component = this._renderNewRootComponent(
nextWrappedElement,
container,
shouldReuseMarkup,
nextContext
)._renderedComponent.getPublicInstance();
if (callback) {
callback.call(component);
}
return component;
}
dispose() {
const rootIds = Object.keys(this._instancesByReactRootID);
for (let i = 0; i < rootIds.length; ++i) {
this.unmountComponentAtNode(this._instancesByReactRootID[rootIds[i]]
.getHostMarkup()
.parentMarkup
.threeObject);
}
delete this._instancesByReactRootID;
if (process.env.NODE_ENV !== 'production') {
delete this.rootMarkupsByReactRootID;
}
delete this._highlightElement;
this.nextMountID = 1;
this.nextReactRootIndex = 0;
if (process.env.NODE_ENV !== 'production' || process.env.ENABLE_REACT_ADDON_HOOKS === 'true') {
if (this._devtoolsCallbackCleanup) {
this._devtoolsCallbackCleanup();
delete this._devtoolsCallbackCleanup;
}
if (this._rendererListenerCleanup) {
this._rendererListenerCleanup();
delete this._rendererListenerCleanup;
}
if (this._devToolsRendererDefinition) {
if (this._agent) {
this._agent.onUnmounted(this._devToolsRendererDefinition);
this._agent.removeListener('highlight', this._onHighlightFromInspector);
this._agent.removeListener('hideHighlight', this._onHideHighlightFromInspector);
}
if (this._reactDevtoolsRendererId) {
delete __REACT_DEVTOOLS_GLOBAL_HOOK__._renderers[this._reactDevtoolsRendererId];
delete this._reactDevtoolsRendererId;
}
delete this._devToolsRendererDefinition;
delete this._agent;
}
delete this._onHighlightFromInspector;
delete this._onHideHighlightFromInspector;
delete this._hookAgent;
}
}
_updateRootComponent(prevComponent, nextElement, nextContext, container, callback) {
ReactUpdateQueue.enqueueElementInternal(prevComponent, nextElement, nextContext);
if (callback) {
ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
}
return prevComponent;
}
/**
* True if the supplied DOM node has a direct React-rendered child that is
* not a React root element. Useful for warning in `render`,
* `unmountComponentAtNode`, etc.
*
* @param {?*} container The container.
* @return {boolean} True if the DOM element contains a direct child that was
* rendered by React but is not a root element.
* @internal
*/
hasNonRootReactChild(container) {
const rootMarkup = getReactRootMarkupInContainer(container);
if (rootMarkup) {
const inst = React3ComponentTree.getInstanceFromMarkup(rootMarkup);
return !!(inst && inst._hostParent);
}
return false;
}
unmountComponentAtNode(container) {
// Various parts of our code (such as ReactCompositeComponent's
// _renderValidatedComponent) assume that calls to render aren't nested;
// verify that that's the case. (Strictly speaking, unmounting won't cause a
// render but we still don't expect to be in a render call here.)
if (process.env.NODE_ENV !== 'production') {
warning(
ReactCurrentOwner.current === null,
'unmountComponentAtNode(): Render methods should be a pure function ' +
'of props and state; triggering nested component updates from render ' +
'is not allowed. If necessary, trigger nested updates in ' +
'componentDidUpdate. Check the render method of %s.',
(ReactCurrentOwner.current && ReactCurrentOwner.current.getName()) ||
'ReactCompositeComponent'
);
}
const prevComponent = this.getTopLevelWrapperInContainer(container);
if (!prevComponent) {
// Check if the node being unmounted was rendered by React, but isn't a
// root node.
const containerHasNonRootReactChild = this.hasNonRootReactChild(container);
// Check if the container itself is a React root node.
const isContainerReactRoot = !!(
container
&& container.userData
&& container.userData.markup
&& container.userData.markup[ID_PROPERTY_NAME]
);
if (process.env.NODE_ENV !== 'production') {
warning(
!containerHasNonRootReactChild,
'unmountComponentAtNode(): The node you\'re attempting to unmount ' +
'was rendered by React and is not a top-level container. %s',
(
isContainerReactRoot ?
'You may have accidentally passed in a React root node instead ' +
'of its container.' :
'Instead, have the parent component update its state and ' +
'rerender in order to remove this component.'
)
);
}
return false;
}
delete this._instancesByReactRootID[prevComponent._instance.rootID];
ReactUpdates.batchedUpdates(
unmountComponentFromNode,
prevComponent,
container,
false
);
if (container && container.userData && container.userData._createdByReact3) {
delete container.userData;
}
return true;
}
/**
* @param {THREE.Object3D|HTMLCanvasElement} container THREE Object
* or HTML Canvas Element that may contain a React component.
* @return {?string} A "reactRoot" ID, if a React component is rendered.
*/
getReactRootID(container) {
const rootMarkup = getReactRootMarkupInContainer(container);
return rootMarkup && this.getID(rootMarkup);
}
// see instantiateReactComponent.js
/**
* @see #instantiateReactComponent
*
* Cloned because it uses
* @see InternalComponent
*
* @param _node ( from createElement )
* @param {boolean} shouldHaveDebugID
* @return {object} A new instance of the element's constructor.
*/
instantiateReactComponent(_node, shouldHaveDebugID) {
let instance;
const node = _node;
const isEmptyNode = (node === null || node === false);
if (isEmptyNode) {
// Create an object3D node so that empty components can be added anywhere
instance = new InternalComponent(
reactElementWrapper.createElement('object3D', {
visible: false,
}), this);
// original: instance = new ReactDOMEmptyComponent(this.instantiateReactComponent);
} else if (typeof node === 'object') {
const element = node;
const type = element.type;
if (
typeof type !== 'function' &&
typeof type !== 'string'
) {
let info = '';
if (process.env.NODE_ENV !== 'production') {
if (
type === undefined ||
(typeof type === 'object' &&
type !== null &&
Object.keys(type).length === 0)
) {
info +=
' You likely forgot to export your component from the file ' +
'it\'s defined in.';
}
}
info += getDeclarationErrorAddendum(element._owner);
invariant(
false,
'Element type is invalid: expected a string (for built-in components) ' +
'or a class/function (for composite components) but got: %s.%s',
(type === null || type === undefined) ? type : typeof type,
info,
);
}
// Special case string values
if (typeof element.type === 'string') {
// original: instance = ReactHostComponent.createInternalComponent(element);
instance = new InternalComponent(element, this);
} else if (isInternalComponentType(element.type)) {
// This is temporarily available for custom components that are not string
// representations. I.e. ART. Once those are updated to use the string
// representation, we can drop this code path.
const Constructor = element.type;
instance = new Constructor(element);
// We renamed this. Allow the old name for compat. :(
if (!instance.getHostNode) {
instance.getHostNode = instance.getNativeNode;
}
} else {
instance = new React3CompositeComponentWrapper(element, this);
}
} else if (typeof node === 'string'
|| typeof node === 'number') {
// TODO create instance for text
if (process.env.NODE_ENV !== 'production') {
invariant(false, 'Encountered invalid React node of type %s : %s',
typeof node, node);
} else {
invariant(false);
}
} else if (process.env.NODE_ENV !== 'production') {
invariant(false, 'Encountered invalid React node of type %s', typeof element);
} else {
invariant(false);
}
if (process.env.NODE_ENV !== 'production') {
warning(
typeof instance.mountComponent === 'function' &&
typeof instance.receiveComponent === 'function' &&
typeof instance.getHostMarkup === 'function' &&
typeof instance.unmountComponent === 'function',
'Only React 3 Components can be mounted.'
);
}
// These two fields are used by the DOM and ART diffing algorithms
// respectively. Instead of using expandos on components, we should be
// storing the state needed by the diffing algorithms elsewhere.
instance._mountIndex = 0;
instance._mountImage = null;
if (process.env.NODE_ENV !== 'production') {
if (shouldHaveDebugID) {
instance._debugID = `r3r${this._debugIdPrefix}-${this._nextDebugID++}`;