169
// Renderer ID is needed to support inspection fiber props, state, and hooks.
170
_rootIDToRendererID: Map<number, number> = new Map();
171
172
- // These options may be initially set by a confiugraiton option when constructing the Store.
172
+ // These options may be initially set by a configuration option when constructing the Store.
173
_supportsNativeInspection: boolean = true;
174
_supportsProfiling: boolean = false;
175
_supportsReloadAndProfile: boolean = false;
486
}
487
488
containsElement(id: number): boolean {
489
- return this._idToElement.get(id) != null;
489
+ return this._idToElement.has(id);
490
}
491
492
getElementAtIndex(index: number): Element | null {
539
}
540
541
getElementIDAtIndex(index: number): number | null {
542
- const element: Element | null = this.getElementAtIndex(index);
542
+ const element = this.getElementAtIndex(index);
543
return element === null ? null : element.id;
544
}
545
546
getElementByID(id: number): Element | null {
547
const element = this._idToElement.get(id);
548
- if (element == null) {
548
+ if (element === undefined) {
549
console.warn(`No element found with id "${id}"`);
550
return null;
551
}
607
let currentID = element.parentID;
608
let index = 0;
609
while (true) {
610
- const current = ((this._idToElement.get(currentID): any): Element);
610
+ const current = this._idToElement.get(currentID);
611
+ if (current === undefined) {
612
+ return null;
613
+ }
614
615
const {children} = current;
616
for (let i = 0; i < children.length; i++) {
618
if (childID === previousID) {
619
break;
620
}
618
- const child = ((this._idToElement.get(childID): any): Element);
621
+
622
+ const child = this._idToElement.get(childID);
623
+ if (child === undefined) {
624
+ return null;
625
+ }
626
+
627
index += child.isCollapsed ? 1 : child.weight;
628
}
629
645
if (rootID === currentID) {
646
break;
647
}
640
- const root = ((this._idToElement.get(rootID): any): Element);
648
+
649
+ const root = this._idToElement.get(rootID);
650
+ if (root === undefined) {
651
+ return null;
652
+ }
653
+
654
index += root.weight;
655
}
656
660
getOwnersListForElement(ownerID: number): Array<Element> {
661
const list: Array<Element> = [];
662
const element = this._idToElement.get(ownerID);
650
- if (element != null) {
663
+ if (element !== undefined) {
664
list.push({
665
...element,
666
depth: 0,
678
// Seems better to defer the cost, since the set of ids is probably pretty small.
679
const sortedIDs = Array.from(unsortedIDs).sort(
680
(idA, idB) =>
668
- ((this.getIndexOfElementID(idA): any): number) -
669
- ((this.getIndexOfElementID(idB): any): number),
681
+ (this.getIndexOfElementID(idA) || 0) -
682
+ (this.getIndexOfElementID(idB) || 0),
683
);
684
685
// Next we need to determine the appropriate depth for each element in the list.
690
// at which point, our depth is just the depth of that node plus one.
691
sortedIDs.forEach(id => {
692
const innerElement = this._idToElement.get(id);
680
- if (innerElement != null) {
693
+ if (innerElement !== undefined) {
694
let parentID = innerElement.parentID;
695
696
let depth = 0;
702
break;
703
}
704
const parent = this._idToElement.get(parentID);
692
- if (parent == null) {
705
+ if (parent === undefined) {
706
break;
707
}
708
parentID = parent.parentID;
723
724
getRendererIDForElement(id: number): number | null {
725
let current = this._idToElement.get(id);
713
- while (current != null) {
726
+ while (current !== undefined) {
727
if (current.parentID === 0) {
728
const rendererID = this._rootIDToRendererID.get(current.id);
729
return rendererID == null ? null : rendererID;
736
737
getRootIDForElement(id: number): number | null {
738
let current = this._idToElement.get(id);
726
- while (current != null) {
739
+ while (current !== undefined) {
740
if (current.parentID === 0) {
741
return current.id;
742
} else {
778
779
const weightDelta = 1 - element.weight;
780
768
- let parentElement: void | Element = ((this._idToElement.get(
769
- element.parentID,
770
- ): any): Element);
771
- while (parentElement != null) {
781
+ let parentElement = this._idToElement.get(element.parentID);
782
+ while (parentElement !== undefined) {
783
// We don't need to break on a collapsed parent in the same way as the expand case below.
784
// That's because collapsing a node doesn't "bubble" and affect its parents.
785
parentElement.weight += weightDelta;
787
}
788
}
789
} else {
779
- let currentElement = element;
790
+ let currentElement: ?Element = element;
791
while (currentElement != null) {
792
const oldWeight = currentElement.isCollapsed
793
? 1
802
: currentElement.weight;
803
const weightDelta = newWeight - oldWeight;
804
794
- let parentElement: void | Element = ((this._idToElement.get(
795
- currentElement.parentID,
796
- ): any): Element);
797
- while (parentElement != null) {
805
+ let parentElement = this._idToElement.get(currentElement.parentID);
806
+ while (parentElement !== undefined) {
807
parentElement.weight += weightDelta;
808
if (parentElement.isCollapsed) {
809
// It's important to break on a collapsed parent when expanding nodes.
817
818
currentElement =
819
currentElement.parentID !== 0
811
- ? // $FlowFixMe[incompatible-type] found when upgrading Flow
812
- this.getElementByID(currentElement.parentID)
813
- : // $FlowFixMe[incompatible-type] found when upgrading Flow
814
- null;
820
+ ? this.getElementByID(currentElement.parentID)
821
+ : null;
822
}
823
}
824
840
}
841
842
_adjustParentTreeWeight: (
836
- parentElement: Element | null,
843
+ parentElement: ?Element,
844
weightDelta: number,
845
) => void = (parentElement, weightDelta) => {
846
let isInsideCollapsedSubTree = false;
855
break;
856
}
857
851
- parentElement = ((this._idToElement.get(
852
- parentElement.parentID,
853
- ): any): Element);
858
+ parentElement = this._idToElement.get(parentElement.parentID);
859
}
860
861
// Additions and deletions within a collapsed subtree should not affect the overall number of elements.
911
const stringTable: Array<string | null> = [
912
null, // ID = 0 corresponds to the null string.
913
];
909
- const stringTableSize = operations[i++];
914
+ const stringTableSize = operations[i];
915
+ i++;
916
+
917
const stringTableEnd = i + stringTableSize;
918
+
919
while (i < stringTableEnd) {
912
- const nextLength = operations[i++];
913
- const nextString = utfDecodeString(
914
- (operations.slice(i, i + nextLength): any),
915
- );
920
+ const nextLength = operations[i];
921
+ i++;
922
+
923
+ const nextString = utfDecodeString(operations.slice(i, i + nextLength));
924
stringTable.push(nextString);
925
i += nextLength;
926
}
929
const operation = operations[i];
930
switch (operation) {
931
case TREE_OPERATION_ADD: {
924
- const id = ((operations[i + 1]: any): number);
932
+ const id = operations[i + 1];
933
const type = ((operations[i + 2]: any): ElementType);
934
935
i += 3;
942
);
943
}
944
937
- let ownerID: number = 0;
938
- let parentID: number = ((null: any): number);
945
if (type === ElementTypeRoot) {
946
if (__DEBUG__) {
947
debug('Add', `new root node ${id}`);
1003
1004
haveRootsChanged = true;
1005
} else {
1000
- parentID = ((operations[i]: any): number);
1006
+ const parentID = operations[i];
1007
i++;
1008
1003
- ownerID = ((operations[i]: any): number);
1009
+ const ownerID = operations[i];
1010
i++;
1011
1012
const displayNameStringID = operations[i];
1024
);
1025
}
1026
1021
- if (!this._idToElement.has(parentID)) {
1027
+ const parentElement = this._idToElement.get(parentID);
1028
+ if (parentElement === undefined) {
1029
this._throwAndEmitError(
1030
Error(
1031
`Cannot add child "${id}" to parent "${parentID}" because parent node was not found in the Store.`,
1032
),
1033
);
1034
+
1035
+ continue;
1036
}
1037
1029
- const parentElement = ((this._idToElement.get(
1030
- parentID,
1031
- ): any): Element);
1038
parentElement.children.push(id);
1039
1040
const [displayNameWithoutHOCs, hocDisplayNames] =
1071
break;
1072
}
1073
case TREE_OPERATION_REMOVE: {
1068
- const removeLength = ((operations[i + 1]: any): number);
1074
+ const removeLength = operations[i + 1];
1075
i += 2;
1076
1077
for (let removeIndex = 0; removeIndex < removeLength; removeIndex++) {
1072
- const id = ((operations[i]: any): number);
1078
+ const id = operations[i];
1079
+ const element = this._idToElement.get(id);
1080
1074
- if (!this._idToElement.has(id)) {
1081
+ if (element === undefined) {
1082
this._throwAndEmitError(
1083
Error(
1084
`Cannot remove node "${id}" because no matching node was found in the Store.`,
1085
),
1086
);
1087
+
1088
+ continue;
1089
}
1090
1091
i += 1;
1092
1084
- const element = ((this._idToElement.get(id): any): Element);
1093
const {children, ownerID, parentID, weight} = element;
1094
if (children.length > 0) {
1095
this._throwAndEmitError(
1099
1100
this._idToElement.delete(id);
1101
1094
- let parentElement = null;
1102
+ let parentElement: ?Element = null;
1103
if (parentID === 0) {
1104
if (__DEBUG__) {
1105
debug('Remove', `node ${id} root`);
1114
if (__DEBUG__) {
1115
debug('Remove', `node ${id} from parent ${parentID}`);
1116
}
1109
- parentElement = ((this._idToElement.get(parentID): any): Element);
1117
+
1118
+ parentElement = this._idToElement.get(parentID);
1119
if (parentElement === undefined) {
1120
this._throwAndEmitError(
1121
Error(
1122
`Cannot remove node "${id}" from parent "${parentID}" because no matching node was found in the Store.`,
1123
),
1124
);
1125
+
1126
+ continue;
1127
}
1128
+
1129
const index = parentElement.children.indexOf(id);
1130
parentElement.children.splice(index, 1);
1131
}
1179
break;
1180
}
1181
case TREE_OPERATION_REORDER_CHILDREN: {
1170
- const id = ((operations[i + 1]: any): number);
1171
- const numChildren = ((operations[i + 2]: any): number);
1182
+ const id = operations[i + 1];
1183
+ const numChildren = operations[i + 2];
1184
i += 3;
1185
1174
- if (!this._idToElement.has(id)) {
1186
+ const element = this._idToElement.get(id);
1187
+ if (element === undefined) {
1188
this._throwAndEmitError(
1189
Error(
1190
`Cannot reorder children for node "${id}" because no matching node was found in the Store.`,
1191
),
1192
);
1193
+
1194
+ continue;
1195
}
1196
1182
- const element = ((this._idToElement.get(id): any): Element);
1197
const children = element.children;
1198
if (children.length !== numChildren) {
1199
this._throwAndEmitError(