Mutate the children array
Dan Abramov committed
Apr 25, 2019 at 14:39 UTC
2993b168e55cd83d4bcf9265cad32bbe37e8a87e
1 file changed
+23
-28
src/devtools/store.js
+23
-28
@@ -720,7 +720,7 @@ export default class Store extends EventEmitter {
720
const parentElement = ((this._idToElement.get(
721
parentID
722
): any): Element);
723
- parentElement.children = parentElement.children.concat(id);
723
+ parentElement.children.push(id);
724
725
const element: Element = {
726
children: [],
@@ -785,9 +785,8 @@ export default class Store extends EventEmitter {
785
`Cannot remove node ${id} from parent ${parentID} because no matching node was found in the Store.`
786
);
787
}
788
- parentElement.children = parentElement.children.filter(
789
- childID => childID !== id
790
- );
788
+ const index = parentElement.children.indexOf(id);
789
+ parentElement.children.splice(index, 1);
790
}
791
792
this._adjustParentTreeWeight(parentElement, -element.weight);
@@ -798,16 +797,7 @@ export default class Store extends EventEmitter {
797
case TREE_OPERATION_REORDER_CHILDREN: {
798
const id = ((operations[i + 1]: any): number);
799
const numChildren = ((operations[i + 2]: any): number);
801
- const nextChildren = ((operations.slice(
802
- i + 3,
803
- i + 3 + numChildren
804
- ): any): Array<number>);
805
-
806
- i = i + 3 + numChildren;
807
-
808
- if (__DEBUG__) {
809
- debug('Re-order', `Node ${id} children ${nextChildren.join(',')}`);
810
- }
800
+ i = i + 3;
801
802
if (!this._idToElement.has(id)) {
803
throw Error(
@@ -816,26 +806,31 @@ export default class Store extends EventEmitter {
806
}
807
808
const element = ((this._idToElement.get(id): any): Element);
819
- const prevChildren = element.children;
820
- if (nextChildren.length !== prevChildren.length) {
809
+ const children = element.children;
810
+ if (children.length !== numChildren) {
811
throw Error(
812
`Children cannot be added or removed during a reorder operation.`
813
);
814
}
825
- // This check is more expensive so it's gated
826
- if (__DEV__) {
827
- if (
828
- nextChildren.find(childID => {
829
- const childElement = this._idToElement.get(childID);
830
- return childElement == null || childElement.parentID !== id;
831
- }) != null
832
- ) {
833
- console.error(
834
- `Children cannot be added or removed during a reorder operation.`
835
- );
815
+
816
+ for (let j = 0; j < numChildren; j++) {
817
+ const childID = operations[i + j];
818
+ children[j] = childID;
819
+ if (__DEV__) {
820
+ // This check is more expensive so it's gated by __DEV__.
821
+ const childElement = this._idToElement.get(childID);
822
+ if (childElement == null || childElement.parentID !== id) {
823
+ console.error(
824
+ `Children cannot be added or removed during a reorder operation.`
825
+ );
826
+ }
827
}
828
}
838
- element.children = Array.from(nextChildren);
829
+ i = i + numChildren;
830
+
831
+ if (__DEBUG__) {
832
+ debug('Re-order', `Node ${id} children ${children.join(',')}`);
833
+ }
834
break;
835
}
836
case TREE_OPERATION_UPDATE_TREE_BASE_DURATION: