-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-dom.js
2622 lines (2097 loc) · 91 KB
/
react-dom.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
const ReactDOM = {}
var workInProgress = null; //工作单元代理变量
var nextEffect = null;
var MAX_SIGNED_31_BIT_INT = 1073741823;
var NoWork = 0;
var Never = 1;
var Idle = 2;
var ContinuousHydration = 3;
var Sync = MAX_SIGNED_31_BIT_INT; //1073741823;
var Batched = Sync - 1; //1073741822;
var UNIT_SIZE = 10;
var MAGIC_NUMBER_OFFSET = Batched - 1; // 1 unit of expiration time represents 10ms. //1073741821
var FunctionComponent = 0;
var ClassComponent = 1;
var IndeterminateComponent = 2; // Before we know whether it is function or class
var HostRoot = 3; // Root of a host tree. Could be nested inside another node.
var HostPortal = 4; // A subtree. Could be an entry point to a different renderer.
var HostComponent = 5;
var HostText = 6;
// Don't change these two values. They're used by React Dev Tools.
var NoEffect =
/* */
0;
var PerformedWork =
/* */
1; // You can change the rest (and add more).
var Placement =
/* */
2;
var Update =
/* */
4;
var PlacementAndUpdate =
/* */
6;
var Deletion =
/* */
8;
var ContentReset =
/* */
16;
var Callback =
/* */
32;
var DidCapture =
/* */
64;
var Ref =
/* */
128;
var Snapshot =
/* */
256;
var Passive =
/* */
512;
var Hydrating =
/* */
1024;
var HydratingAndUpdate =
/* */
1028; // Passive & Update & Callback & Ref & Snapshot
var LifecycleEffectMask =
/* */
932; // Union of all host effects
var HostEffectMask =
/* */
2047;
var Incomplete =
/* */
2048;
var ShouldCapture =
/* */
4096;
var ELEMENT_NODE = 1;
var TEXT_NODE = 3;
var COMMENT_NODE = 8;
var DOCUMENT_NODE = 9;
var DOCUMENT_FRAGMENT_NODE = 11;
var REACT_ELEMENT_TYPE = Symbol.for('react.element')
var isArray$1 = Array.isArray;
function get(key) {
return key._reactInternalFiber;
}
function has(key) {
return key._reactInternalFiber !== undefined;
}
function set(key, value) {
key._reactInternalFiber = value;
}
function markUpdate(workInProgress) {
// Tag the fiber with an update effect. This turns a Placement into
// a PlacementAndUpdate.
workInProgress.effectTag |= Update;
}
function shouldSetTextContent(type, props) {
return type === 'textarea' || type === 'option' || type === 'noscript' || typeof props.children === 'string' || typeof props.children === 'number' || typeof props.dangerouslySetInnerHTML === 'object' && props.dangerouslySetInnerHTML !== null && props.dangerouslySetInnerHTML.__html != null;
}
function createFiber(tag, pendingProps, key, mode) {
return new FiberNode(tag, pendingProps, key, mode)
}
function FiberNode(tag, pendingProps, key, mode) {
// Instance
//标记不同的组件类型
//有原生的DOM节点,有React自己的节点
this.tag = tag;
//ReactElement里面的key
this.key = key;
//ReactElement.type,也就是我们调用createElement的第一个参数
this.elementType = null;
//异步组件resolve之后返回的内容,一般是function或class
//比如懒加载
this.type = null;
//当前Fiber的状态(比如浏览器环境就是DOM节点)
//不同类型的实例都会记录在stateNode上
//比如DOM组件对应DOM节点实例
//ClassComponent对应Class实例
//FunctionComponent没有实例,所以stateNode值为null
//state更新了或props更新了均会更新到stateNode上
this.stateNode = null; // Fiber
//指向该对象在Fiber节点树中的`parent`,用来在处理完该节点后返回
//即流程图上的红线
this.return = null;
//指向自己的第一个子节点
this.child = null;
//指向自己的兄弟结构
//兄弟节点的return指向同一个父节点
this.sibling = null;
this.index = 0;
//ref属性
this.ref = null;
//新的变动带来的新的props,即nextProps
this.pendingProps = pendingProps;
//上一次渲染完成后的props,即 props
this.memoizedProps = null;
//该Fiber对应的组件,所产生的update,都会放在该队列中
this.updateQueue = null;
//上次渲染的state,即 state
//新的state由updateQueue计算得出,并覆盖memoizedState
this.memoizedState = null;
//一个列表,存在该Fiber依赖的contexts,events
this.dependencies = null;
//mode有conCurrentMode和strictMode
//用来描述当前Fiber和其他子树的Bitfield
//共存的模式表示这个子树是否默认是 异步渲染的
//Fiber刚被创建时,会继承父Fiber
//this.mode = mode;
// Effects
//以下属性是副作用
//副作用是 标记组件哪些需要更新的工具、标记组件需要执行哪些生命周期的工具
this.effectTag = NoEffect;
//16进制的数字,可以理解为通过一个字段标识n个动作,如Placement、Update、Deletion、Callback……所以源码中看到很多 &=
this.nextEffect = null;
//表示下一个将要处理的副作用FiberNode的引用
this.firstEffect = null;
//与副作用操作遍历流程相关 当前节点下,第一个需要处理的副作用FiberNode的引用
this.lastEffect = null;
//表示最后一个将要处理的副作用FiberNode的引用
//代表任务在未来的哪个时间点 应该被完成
//不包括该Fiber的子树产生的任务
this.expirationTime = NoWork; //ExpirationTime
//快速确定子树中是否有 update
//如果子节点有update的话,就记录应该更新的时间
this.childExpirationTime = NoWork; //ExpirationTime
// 在FIber树更新的过程中,每个Fiber都有与其对应的Fiber
//我们称之为 current <==> workInProgress
//在渲染完成后,会交换位置
//doubleBuffer Fiber在更新后,不用再重新创建对象,
// 而是复制自身,并且两者相互复用,用来提高性能
this.alternate = null;
}
function FiberRootNode(containerInfo, tag = 0) {
this.tag = tag;
this.current = null;
this.containerInfo = containerInfo;
this.pendingChildren = null;
this.pingCache = null;
//this.finishedExpirationTime = NoWork;
this.finishedWork = null;
// this.timeoutHandle = noTimeout;
this.context = null;
this.pendingContext = null;
this.hydrate = false;
this.callbackNode = null;
// this.callbackPriority = NoPriority;
// this.firstPendingTime = NoWork;
// this.firstSuspendedTime = NoWork;
// this.lastSuspendedTime = NoWork;
// this.nextKnownPendingLevel = NoWork;
// this.lastPingedTime = NoWork;
// this.lastExpiredTime = NoWork;
}
//创建更新队列
function initializeUpdateQueue(fiber) {
var queue = {
// 应用更新后的state
// 每次的更新都是在这个baseState基础上进行更新
baseState: fiber.memoizedState,
baseQueue: null,
shared: {
pending: null
},
effects: null
};
fiber.updateQueue = queue;
}
function legacyCreateRootFromDOMContainer(container) {
var rootSibling;
while (rootSibling = container.lastChild) {
container.removeChild(rootSibling);
}
var root = new FiberRootNode(container)
var uninitializedFiber = createFiber(HostRoot = 3, null, null);
uninitializedFiber.stateNode = root;
//root.current = uninitializedFiber;
root.current = uninitializedFiber;
initializeUpdateQueue(uninitializedFiber);
return {
_internalRoot: root
}
}
function createUpdate(expirationTime, suspenseConfig = null) {
var update = {
// 过期时间
expirationTime: expirationTime,
suspenseConfig: suspenseConfig,
// export const UpdateState = 0;
// export const ReplaceState = 1;
// export const ForceUpdate = 2;
// export const CaptureUpdate = 3;
// 指定更新的类型,值为以上几种
// 提下CaptureUpdate,在React16后有一个ErrorBoundaries功能
// 即在渲染过程中报错了,可以选择新的渲染状态(提示有错误的状态),来更新页面
// 0更新 1替换 2强制更新 3捕获性的更新
//tag: UpdateState,
tag: 0,
// 更新内容,比如`setState`接收的第一个参数
// 第一次渲染ReactDOM.render接收的是payload = {element};
payload: null,
// 更新完成后对应的回调,`setState`,`render`都有
callback: null,
// 指向下一个更新
next: null
};
update.next = update;
return update;
}
function requestCurrentTimeForUpdate() {
return 1073741821 - (100 / 10 | 0)
}
function computeExpirationForFiber() {
return Sync
}
function enqueueUpdate(fiber, update) {
//更新后面
var updateQueue = fiber.updateQueue;
var sharedQueue = updateQueue.shared;
var pending = sharedQueue.pending;
if (pending === null) {
// This is the first update. Create a circular list.
update.next = update;
} else {
update.next = pending.next;
pending.next = update;
}
sharedQueue.pending = update;
}
function createWorkInProgress(current, pendingProps) {
var workInProgress = current.alternate;
if (workInProgress === null) {
// We use a double buffering pooling technique because we know that we'll
// only ever need at most two versions of a tree. We pool the "other" unused
// node that we're free to reuse. This is lazily created to avoid allocating
// extra objects for things that are never updated. It also allow us to
// reclaim the extra memory if needed.
workInProgress = createFiber(current.tag, pendingProps, current.key, current.mode);
workInProgress.elementType = current.elementType;
workInProgress.type = current.type;
workInProgress.stateNode = current.stateNode;
workInProgress.alternate = current;
current.alternate = workInProgress;
} else {
workInProgress.pendingProps = pendingProps; // We already have an alternate.
// Reset the effect tag.
workInProgress.effectTag = NoEffect; // The effect list is no longer valid.
workInProgress.nextEffect = null;
workInProgress.firstEffect = null;
workInProgress.lastEffect = null;
}
workInProgress.childExpirationTime = current.childExpirationTime;
workInProgress.expirationTime = current.expirationTime;
workInProgress.child = current.child;
workInProgress.memoizedProps = current.memoizedProps;
workInProgress.memoizedState = current.memoizedState;
workInProgress.updateQueue = current.updateQueue; // Clone the dependencies object.
//This is mutated during the render phase, so
// it cannot be shared with the current fiber.
// var currentDependencies = current.dependencies;
workInProgress.sibling = current.sibling;
workInProgress.index = current.index;
workInProgress.ref = current.ref;
return workInProgress;
}
function prepareFreshStack(root, expirationTime) {
root.finishedWork = null;
root.finishedExpirationTime = NoWork;
//workInProgressRoot = root;
//workInProgress是全局变量
workInProgress = createWorkInProgress(root.current, null);
}
function cloneUpdateQueue(current, workInProgress) {
//不懂这个函数的作用
// Clone the update queue from current. Unless it's already a clone.
var queue = workInProgress.updateQueue;
var currentQueue = current.updateQueue;
if (queue === currentQueue) {
var clone = {
baseState: currentQueue.baseState,
baseQueue: currentQueue.baseQueue,
shared: currentQueue.shared,
effects: currentQueue.effects
};
workInProgress.updateQueue = clone;
}
}
function getStateFromUpdate(workInProgress, queue, update, prevState, nextProps, instance) {
switch (update.tag) {
case 0: {
//UpdateState=0
var _payload = update.payload;
var partialState;
if (typeof _payload === 'function') {
// Updater function
} else {
// Partial state object
partialState = _payload;
}
if (partialState === null || partialState === undefined) {
// Null and undefined are treated as no-ops.
return prevState;
} // Merge the partial state and the previous state.
return {
...prevState,
...partialState
};
}
}
return prevState;
}
function processUpdateQueue(workInProgress, props, instance, renderExpirationTime) {
//这个方法要做的事情,就是遍历这个 UpdateQueue ,
//然后计算出最后的新 State,然后存到workInProgress.memoizedState和 instance 中
var queue = workInProgress.updateQueue;
var baseQueue = queue.baseQueue; // The last pending update that hasn't been processed yet.
var pendingQueue = queue.shared.pending;
if (pendingQueue !== null) {
//将workInProgress.updateQueue中的pendingQueue赋值给 workInProgress.alternate.updateQueue.baseQueue
// We have new updates that haven't been processed yet.
// We'll add them to the base queue.
if (baseQueue !== null) {
// Merge the pending queue and the base queue.
var baseFirst = baseQueue.next;
var pendingFirst = pendingQueue.next;
baseQueue.next = pendingFirst;
pendingQueue.next = baseFirst;
}
baseQueue = pendingQueue;
queue.shared.pending = null;
var current = workInProgress.alternate;
if (current !== null) {
var currentQueue = current.updateQueue;
if (currentQueue !== null) {
currentQueue.baseQueue = pendingQueue;
}
}
}
if (baseQueue !== null) {
var first = baseQueue.next; // Iterate through the list of updates to compute the result.
var newState = queue.baseState;
var newExpirationTime = NoWork;
var newBaseState = null;
var newBaseQueueFirst = null;
var newBaseQueueLast = null;
if (first !== null) {
var update = first;
do {
newState = getStateFromUpdate(workInProgress, queue, update, newState, props, instance);
update = update.next;
if (update === null || update === first) {
pendingQueue = queue.shared.pending;
if (pendingQueue === null) {
break;
} else {
// An update was scheduled from inside a reducer. Add the new
// pending updates to the end of the list and keep processing.
update = baseQueue.next = pendingQueue.next;
pendingQueue.next = first;
queue.baseQueue = baseQueue = pendingQueue;
queue.shared.pending = null;
}
}
} while (true);
}
if (newBaseQueueLast === null) {
newBaseState = newState;
} else {
newBaseQueueLast.next = newBaseQueueFirst;
}
queue.baseState = newBaseState;
queue.baseQueue = newBaseQueueLast;
workInProgress.expirationTime = newExpirationTime;
workInProgress.memoizedState = newState;
}
}
function createFiberFromElement(element, mode, expirationTime) {
var owner = null;
var type = element.type;
var key = element.key;
var pendingProps = element.props;
var fiber = createFiberFromTypeAndProps(type, key, pendingProps, owner, mode, expirationTime);
return fiber;
}
function createFiberFromFragment(elements, mode, expirationTime, key) {
var fiber = createFiber(Fragment, elements, key, mode);
fiber.expirationTime = expirationTime;
return fiber;
}
function createFiberFromTypeAndProps(type, // React$ElementType
key, pendingProps, owner, mode, expirationTime) {
var fiber;
var fiberTag; // The resolved type is set if we know what the final type will be. I.e. it's not lazy.
var resolvedType = type;
if (typeof type === 'function') {
fiberTag = ClassComponent;
} else if (typeof type === 'string') {
fiberTag = HostComponent;
} else {
getTag: switch (type) {
default: {
}
}
}
fiber = createFiber(fiberTag, pendingProps, key, mode);
fiber.elementType = type;
fiber.type = resolvedType;
fiber.expirationTime = expirationTime;
return fiber;
}
function createFiberFromText(content, mode, expirationTime) {
var fiber = createFiber(HostText, content, null, mode);
fiber.expirationTime = expirationTime;
return fiber;
}
function reconcileChildren(current, workInProgress, nextChildren, renderExpirationTime) {
if (current === null) {
workInProgress.child = mountChildFibers(workInProgress, null, nextChildren, renderExpirationTime);
} else {
workInProgress.child = reconcileChildFibers(workInProgress, current.child, nextChildren, renderExpirationTime);
}
}
var reconcileChildFibers = ChildReconciler(true);
var mountChildFibers = ChildReconciler(false);
function ChildReconciler(shouldTrackSideEffects) {
function deleteChild(returnFiber, childToDelete) {
if (!shouldTrackSideEffects) {
// Noop.
return;
} // Deletions are added in reversed order so we add it to the front.
// At this point, the return fiber's effect list is empty except for
// deletions, so we can just append the deletion to the list. The remaining
// effects aren't added until the complete phase. Once we implement
// resuming, this may not be true.
var last = returnFiber.lastEffect;
if (last !== null) {
last.nextEffect = childToDelete;
returnFiber.lastEffect = childToDelete;
} else {
returnFiber.firstEffect = returnFiber.lastEffect = childToDelete;
}
childToDelete.nextEffect = null;
childToDelete.effectTag = Deletion;
}
function deleteRemainingChildren(returnFiber, currentFirstChild) {
if (!shouldTrackSideEffects) {
// Noop.
return null;
} // TODO: For the shouldClone case, this could be micro-optimized a bit by
// assuming that after the first child we've already added everything.
var childToDelete = currentFirstChild;
while (childToDelete !== null) {
deleteChild(returnFiber, childToDelete);
childToDelete = childToDelete.sibling;
}
return null;
}
function placeSingleChild(newFiber) {
// This is simpler for the single child case. We only need to do a
// placement for inserting new children.
if (shouldTrackSideEffects && newFiber.alternate === null) {
newFiber.effectTag = Placement;
}
return newFiber;
}
function useFiber(fiber, pendingProps) {
// We currently set sibling to null and index to 0 here because it is easy
// to forget to do before returning it. E.g. for the single child case.
var clone = createWorkInProgress(fiber, pendingProps);
clone.index = 0;
clone.sibling = null;
return clone;
}
function placeChild(newFiber, lastPlacedIndex, newIndex) {
newFiber.index = newIndex;
if (!shouldTrackSideEffects) {
// Noop.
return lastPlacedIndex;
}
var current = newFiber.alternate;
if (current !== null) {
var oldIndex = current.index;
if (oldIndex < lastPlacedIndex) {
// This is a move.
newFiber.effectTag = Placement;
return lastPlacedIndex;
} else {
// This item can stay in place.
return oldIndex;
}
} else {
// This is an insertion.
newFiber.effectTag = Placement;
return lastPlacedIndex;
}
}
function reconcileSingleElement(returnFiber, currentFirstChild, element, expirationTime) {
var key = element.key;
var child = currentFirstChild;
var _created4 = createFiberFromElement(element, returnFiber.mode, expirationTime);
_created4.return = returnFiber;
return _created4;
}
function reconcileSingleTextNode(returnFiber, currentFirstChild, textContent, expirationTime) {
var created = createFiberFromText(textContent, returnFiber.mode, expirationTime);
created.return = returnFiber;
return created;
}
function updateTextNode(returnFiber, current, textContent, expirationTime) {
if (current === null || current.tag !== HostText) {
// Insert
var created = createFiberFromText(textContent, returnFiber.mode, expirationTime);
created.return = returnFiber;
return created;
} else {
// Update
var existing = useFiber(current, textContent);
existing.return = returnFiber;
return existing;
}
}
function updateElement(returnFiber, current, element, expirationTime) {
if (current !== null) {
if (current.elementType === element.type || ( // Keep this check inline so it only runs on the false path:
isCompatibleFamilyForHotReloading(current, element))) {
// Move based on index
var existing = useFiber(current, element.props);
//existing.ref = coerceRef(returnFiber, current, element);
existing.return = returnFiber;
return existing;
}
} // Insert
var created = createFiberFromElement(element, returnFiber.mode, expirationTime);
// created.ref = coerceRef(returnFiber, current, element);
created.return = returnFiber;
return created;
}
function updatePortal(returnFiber, current, portal, expirationTime) {
if (current === null || current.tag !== HostPortal || current.stateNode.containerInfo !== portal.containerInfo || current.stateNode.implementation !== portal.implementation) {
// Insert
var created = createFiberFromPortal(portal, returnFiber.mode, expirationTime);
created.return = returnFiber;
return created;
} else {
// Update
var existing = useFiber(current, portal.children || []);
existing.return = returnFiber;
return existing;
}
}
function updateFragment(returnFiber, current, fragment, expirationTime, key) {
if (current === null || current.tag !== Fragment) {
// Insert
var created = createFiberFromFragment(fragment, returnFiber.mode, expirationTime, key);
created.return = returnFiber;
return created;
} else {
// Update
var existing = useFiber(current, fragment);
existing.return = returnFiber;
return existing;
}
}
function createChild(returnFiber, newChild, expirationTime) {
if (typeof newChild === 'string' || typeof newChild === 'number') {
// Text nodes don't have keys. If the previous node is implicitly keyed
// we can continue to replace it without aborting even if it is not a text
// node.
var created = createFiberFromText('' + newChild, returnFiber.mode, expirationTime);
created.return = returnFiber;
return created;
}
if (typeof newChild === 'object' && newChild !== null) {
switch (newChild.$$typeof) {
case REACT_ELEMENT_TYPE: {
var _created = createFiberFromElement(newChild, returnFiber.mode, expirationTime);
//_created.ref = coerceRef(returnFiber, null, newChild);
_created.return = returnFiber;
return _created;
}
/* case REACT_PORTAL_TYPE: {
var _created2 = createFiberFromPortal(newChild, returnFiber.mode, expirationTime);
_created2.return = returnFiber;
return _created2;
} */
}
if (isArray$1(newChild)) {
var _created3 = createFiberFromFragment(newChild, returnFiber.mode, expirationTime, null);
_created3.return = returnFiber;
return _created3;
}
}
/* {
if (typeof newChild === 'function') {
warnOnFunctionType();
}
} */
return null;
}
function updateFromMap(existingChildren, returnFiber, newIdx, newChild, expirationTime) {
if (typeof newChild === 'string' || typeof newChild === 'number') {
// Text nodes don't have keys, so we neither have to check the old nor
// new node for the key. If both are text nodes, they match.
var matchedFiber = existingChildren.get(newIdx) || null;
return updateTextNode(returnFiber, matchedFiber, '' + newChild, expirationTime);
}
if (typeof newChild === 'object' && newChild !== null) {
switch (newChild.$$typeof) {
case REACT_ELEMENT_TYPE: {
var _matchedFiber = existingChildren.get(newChild.key === null ? newIdx : newChild.key) || null;
if (newChild.type === REACT_FRAGMENT_TYPE) {
return updateFragment(returnFiber, _matchedFiber, newChild.props.children, expirationTime, newChild.key);
}
return updateElement(returnFiber, _matchedFiber, newChild, expirationTime);
}
/* case REACT_PORTAL_TYPE: {
var _matchedFiber2 = existingChildren.get(newChild.key === null ? newIdx : newChild.key) || null;
return updatePortal(returnFiber, _matchedFiber2, newChild, expirationTime);
} */
}
if (isArray$1(newChild)) {
var _matchedFiber3 = existingChildren.get(newIdx) || null;
return updateFragment(returnFiber, _matchedFiber3, newChild, expirationTime, null);
}
}
/* {
if (typeof newChild === 'function') {
warnOnFunctionType();
}
}
*/
return null;
}
function updateSlot(returnFiber, oldFiber, newChild, expirationTime) {
// Update the fiber if the keys match, otherwise return null.
var key = oldFiber !== null ? oldFiber.key : null;
if (typeof newChild === 'string' || typeof newChild === 'number') {
// Text nodes don't have keys. If the previous node is implicitly keyed
// we can continue to replace it without aborting even if it is not a text
// node.
if (key !== null) {
return null;
}
return updateTextNode(returnFiber, oldFiber, '' + newChild, expirationTime);
}
if (typeof newChild === 'object' && newChild !== null) {
switch (newChild.$$typeof) {
case REACT_ELEMENT_TYPE: {
if (newChild.key === key) {
/* if (newChild.type === REACT_FRAGMENT_TYPE) {
return updateFragment(returnFiber, oldFiber, newChild.props.children, expirationTime, key);
} */
return updateElement(returnFiber, oldFiber, newChild, expirationTime);
} else {
return null;
}
}
/* case REACT_PORTAL_TYPE: {
if (newChild.key === key) {
return updatePortal(returnFiber, oldFiber, newChild, expirationTime);
} else {
return null;
}
} */
}
// if (isArray$1(newChild) || getIteratorFn(newChild)) {
if (isArray$1(newChild)) {
if (key !== null) {
return null;
}
return updateFragment(returnFiber, oldFiber, newChild, expirationTime, null);
}
}
return null;
}
function reconcileChildrenArray(returnFiber, currentFirstChild, newChildren, expirationTime) {
var resultingFirstChild = null;
var previousNewFiber = null;
var oldFiber = currentFirstChild;
var lastPlacedIndex = 0;
var newIdx = 0;
var nextOldFiber = null;
for (; oldFiber !== null && newIdx < newChildren.length; newIdx++) {
if (oldFiber.index > newIdx) {
nextOldFiber = oldFiber;
oldFiber = null;
} else {
nextOldFiber = oldFiber.sibling;
}
var newFiber = updateSlot(returnFiber, oldFiber, newChildren[newIdx], expirationTime);
if (newFiber === null) {
// TODO: This breaks on empty slots like null children. That's
// unfortunate because it triggers the slow path all the time. We need
// a better way to communicate whether this was a miss or null,
// boolean, undefined, etc.
if (oldFiber === null) {
oldFiber = nextOldFiber;
}
break;
}
if (shouldTrackSideEffects) {
if (oldFiber && newFiber.alternate === null) {
// We matched the slot, but we didn't reuse the existing fiber, so we
// need to delete the existing child.
deleteChild(returnFiber, oldFiber);
}
}
lastPlacedIndex = placeChild(newFiber, lastPlacedIndex, newIdx);
if (previousNewFiber === null) {
// TODO: Move out of the loop. This only happens for the first run.
resultingFirstChild = newFiber;
} else {
// TODO: Defer siblings if we're not at the right index for this slot.
// I.e. if we had null values before, then we want to defer this
// for each null value. However, we also don't want to call updateSlot
// with the previous one.
previousNewFiber.sibling = newFiber;
}
previousNewFiber = newFiber;
oldFiber = nextOldFiber;
}
if (newIdx === newChildren.length) {
// We've reached the end of the new children. We can delete the rest.
deleteRemainingChildren(returnFiber, oldFiber);
return resultingFirstChild;
}
if (oldFiber === null) {
// If we don't have any more existing children we can choose a fast path
// since the rest will all be insertions.
for (; newIdx < newChildren.length; newIdx++) {
var _newFiber = createChild(returnFiber, newChildren[newIdx], expirationTime);
if (_newFiber === null) {
continue;
}
lastPlacedIndex = placeChild(_newFiber, lastPlacedIndex, newIdx);
if (previousNewFiber === null) {
// TODO: Move out of the loop. This only happens for the first run.
resultingFirstChild = _newFiber;
} else {
previousNewFiber.sibling = _newFiber;
}
previousNewFiber = _newFiber;
}
return resultingFirstChild;
} // Add all children to a key map for quick lookups.
var existingChildren = mapRemainingChildren(returnFiber, oldFiber); // Keep scanning and use the map to restore deleted items as moves.
for (; newIdx < newChildren.length; newIdx++) {
var _newFiber2 = updateFromMap(existingChildren, returnFiber, newIdx, newChildren[newIdx], expirationTime);
if (_newFiber2 !== null) {
if (shouldTrackSideEffects) {
if (_newFiber2.alternate !== null) {
// The new fiber is a work in progress, but if there exists a
// current, that means that we reused the fiber. We need to delete
// it from the child list so that we don't add it to the deletion
// list.
existingChildren.delete(_newFiber2.key === null ? newIdx : _newFiber2.key);
}
}
lastPlacedIndex = placeChild(_newFiber2, lastPlacedIndex, newIdx);
if (previousNewFiber === null) {
resultingFirstChild = _newFiber2;
} else {
previousNewFiber.sibling = _newFiber2;
}
previousNewFiber = _newFiber2;
}
}
if (shouldTrackSideEffects) {
// Any existing children that weren't consumed above were deleted. We need