refactor(reactflow): optimize applyChanges handler
This commit is contained in:
@@ -55,14 +55,17 @@ function applyChanges(changes: any[], elements: any[]): any[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let remainingChanges = changes;
|
let remainingChanges = changes;
|
||||||
const initElements: any[] = changes.filter((c) => c.type === 'add').map((c) => c.item);
|
const updatedElements: any[] = [];
|
||||||
|
|
||||||
return elements.reduce((res: any[], item: any) => {
|
for (let i = 0; i < elements.length; i++) {
|
||||||
const nextChanges: any[] = [];
|
const nextChanges: any[] = [];
|
||||||
const _remainingChanges: any[] = [];
|
const _remainingChanges: any[] = [];
|
||||||
|
const item = elements[i];
|
||||||
|
|
||||||
remainingChanges.forEach((c) => {
|
remainingChanges.forEach((c) => {
|
||||||
if (c.id === item.id) {
|
if (c.type === 'add') {
|
||||||
|
updatedElements.push(c.item);
|
||||||
|
} else if (c.id === item.id) {
|
||||||
nextChanges.push(c);
|
nextChanges.push(c);
|
||||||
} else {
|
} else {
|
||||||
_remainingChanges.push(c);
|
_remainingChanges.push(c);
|
||||||
@@ -71,8 +74,8 @@ function applyChanges(changes: any[], elements: any[]): any[] {
|
|||||||
remainingChanges = _remainingChanges;
|
remainingChanges = _remainingChanges;
|
||||||
|
|
||||||
if (nextChanges.length === 0) {
|
if (nextChanges.length === 0) {
|
||||||
res.push(item);
|
updatedElements.push(item);
|
||||||
return res;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateItem = { ...item };
|
const updateItem = { ...item };
|
||||||
@@ -101,7 +104,7 @@ function applyChanges(changes: any[], elements: any[]): any[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (updateItem.expandParent) {
|
if (updateItem.expandParent) {
|
||||||
handleParentExpand(res, updateItem);
|
handleParentExpand(updatedElements, updateItem);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -123,20 +126,20 @@ function applyChanges(changes: any[], elements: any[]): any[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (updateItem.expandParent) {
|
if (updateItem.expandParent) {
|
||||||
handleParentExpand(res, updateItem);
|
handleParentExpand(updatedElements, updateItem);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'remove': {
|
case 'remove': {
|
||||||
return res;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updatedElements.push(updateItem);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
res.push(updateItem);
|
return updatedElements;
|
||||||
return res;
|
|
||||||
}, initElements);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyNodeChanges<NodeData = any>(changes: NodeChange[], nodes: Node<NodeData>[]): Node<NodeData>[] {
|
export function applyNodeChanges<NodeData = any>(changes: NodeChange[], nodes: Node<NodeData>[]): Node<NodeData>[] {
|
||||||
|
|||||||
Reference in New Issue
Block a user