Skip to content

Commit

Permalink
Mitigate the incorrect layout of Discover due to a race condition bet…
Browse files Browse the repository at this point in the history
…ween loading column definition and data

Signed-off-by: Miki <[email protected]>
  • Loading branch information
AMoo-Miki committed Nov 26, 2024
1 parent 539675e commit 1400482
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ const DefaultDiscoverTableUI = ({
// Allow auto column-sizing using the initially rendered rows and then convert to fixed
const tableLayoutRequestFrameRef = useRef<number>(0);

/* In asynchronous data loading, column metadata may arrive before the corresponding data, resulting in
layout being calculated for the new column definitions using the old data. To mitigate this issue, we
additionally trigger a recalculation when a change is observed in the index that the data attributes
itself to. This ensures a re-layout is performed when new data is loaded or the column definitions
change, effectively addressing the symptoms of the race condition.
*/
const indexOfRenderedData = rows?.[0]?._index;
const timeFromFirstRow =
indexPattern?.timeFieldName && rows?.[0]?._source?.[indexPattern.timeFieldName];

useEffect(() => {
if (tableElement) {
// Load the first batch of rows and adjust the columns to the contents
Expand Down Expand Up @@ -214,7 +224,7 @@ const DefaultDiscoverTableUI = ({
}

return () => cancelAnimationFrame(tableLayoutRequestFrameRef.current);
}, [columns, tableElement]);
}, [columns, tableElement, indexOfRenderedData, timeFromFirstRow]);

return (
indexPattern && (
Expand Down

0 comments on commit 1400482

Please sign in to comment.