Revert useApplyGridTransaction changes
This commit is contained in:
parent
1db7919875
commit
afd82a8e45
@ -1,48 +1,10 @@
|
|||||||
import { GridApi } from 'ag-grid-community';
|
import { GridApi } from 'ag-grid-community';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import isEqual from 'lodash/isEqual';
|
import isEqual from 'lodash/isEqual';
|
||||||
import { produce } from 'immer';
|
|
||||||
|
|
||||||
export const updateCallback =
|
export const useApplyGridTransaction = <T extends { id: string }>(
|
||||||
<T>(
|
|
||||||
gridApiRef: { current: GridApi | null },
|
|
||||||
getRowNodeId: (row: T) => string
|
|
||||||
) =>
|
|
||||||
(data: T[]) => {
|
|
||||||
if (!gridApiRef.current) return;
|
|
||||||
|
|
||||||
const update: T[] = [];
|
|
||||||
const add: T[] = [];
|
|
||||||
|
|
||||||
// split into updates and adds
|
|
||||||
data.forEach((d) => {
|
|
||||||
if (!gridApiRef.current) return;
|
|
||||||
|
|
||||||
const rowNode = gridApiRef.current.getRowNode(getRowNodeId(d));
|
|
||||||
|
|
||||||
if (rowNode) {
|
|
||||||
if (
|
|
||||||
produce(rowNode.data, (draft: T) => Object.assign(draft, d)) !==
|
|
||||||
rowNode.data
|
|
||||||
) {
|
|
||||||
update.push(d);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
add.push(d);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// async transaction for optimal handling of high grequency updates
|
|
||||||
gridApiRef.current.applyTransactionAsync({
|
|
||||||
update,
|
|
||||||
add,
|
|
||||||
addIndex: 0,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useApplyGridTransaction = <T>(
|
|
||||||
data: T[],
|
data: T[],
|
||||||
gridApi: GridApi | null,
|
gridApi: GridApi | null
|
||||||
getRowNodeId: (row: T) => string
|
|
||||||
) => {
|
) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!gridApi) return;
|
if (!gridApi) return;
|
||||||
@ -54,7 +16,7 @@ export const useApplyGridTransaction = <T>(
|
|||||||
data.forEach((d) => {
|
data.forEach((d) => {
|
||||||
if (!gridApi) return;
|
if (!gridApi) return;
|
||||||
|
|
||||||
const rowNode = gridApi.getRowNode(getRowNodeId(d));
|
const rowNode = gridApi.getRowNode(d.id);
|
||||||
|
|
||||||
if (rowNode) {
|
if (rowNode) {
|
||||||
if (!isEqual(rowNode.data, d)) {
|
if (!isEqual(rowNode.data, d)) {
|
||||||
@ -64,11 +26,11 @@ export const useApplyGridTransaction = <T>(
|
|||||||
add.push(d);
|
add.push(d);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// async transaction for optimal handling of high grequency updates
|
|
||||||
gridApi.applyTransaction({
|
gridApi.applyTransaction({
|
||||||
update,
|
update,
|
||||||
add,
|
add,
|
||||||
addIndex: 0,
|
addIndex: 0,
|
||||||
});
|
});
|
||||||
}, [data, gridApi, getRowNodeId]);
|
}, [data, gridApi]);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user