Skip to content

Commit

Permalink
fix: remove error when recreating node
Browse files Browse the repository at this point in the history
  • Loading branch information
remilry committed Apr 30, 2024
1 parent 00b0c80 commit c0f0e3d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('updateVirtualNodeRegistration', () => {
previousItems,
currentItems,
addVirtualNode: mockAddNode,
removeVirtualNode: jest.fn(),
});

expect(mockAddNode).toHaveBeenCalledTimes(2);
Expand All @@ -24,6 +25,7 @@ describe('updateVirtualNodeRegistration', () => {
previousItems,
currentItems,
addVirtualNode: mockAddNode,
removeVirtualNode: jest.fn(),
});

expect(mockAddNode).not.toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ const registerNewNode = <T>({
});
};

const unregisterOldNode = <T>({
currentItems,
previousItems,
removeVirtualNode,
}: {
currentItems: Array<T>;
previousItems: Array<T>;
removeVirtualNode: (index: number) => void;
}) => {
for (let index = previousItems.length - 1; index > currentItems.length - 1; index--) {
removeVirtualNode(index);
}
};

/**
* This function aims to compare 2 arrays of items : currentItems and previousItems and :
* - addVirtualNode for every item from currentItems that weren't in previousItems
Expand All @@ -26,16 +40,18 @@ export const updateVirtualNodeRegistration = <T>({
currentItems,
previousItems,
addVirtualNode,
removeVirtualNode,
}: {
currentItems: Array<T>;
previousItems: Array<T>;
addVirtualNode: (index: number) => void;
removeVirtualNode: (index: number) => void;
}) => {
// Step 1 : addVirtualNode for every item from currentItems that weren't in previousItems
registerNewNode({ currentItems, previousItems, addVirtualNode });

// Step 2 : removeVirtualNode for every from previousItems that aren't there anymore in currentItems
// TODO
unregisterOldNode({ currentItems, previousItems, removeVirtualNode });

// Step 3 : re-order all the items
// TODO
Expand Down

0 comments on commit c0f0e3d

Please sign in to comment.