mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
EventsCanvas: Clean up action to avoid memory leak (#37592)
This commit is contained in:
parent
16b1922cdc
commit
b028dbc537
@ -1,5 +1,5 @@
|
|||||||
import { DataFrame, DataFrameFieldIndex } from '@grafana/data';
|
import { DataFrame, DataFrameFieldIndex } from '@grafana/data';
|
||||||
import React, { useLayoutEffect, useMemo, useState } from 'react';
|
import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { usePlotContext } from '../context';
|
import { usePlotContext } from '../context';
|
||||||
import { Marker } from './Marker';
|
import { Marker } from './Marker';
|
||||||
import { XYCanvas } from './XYCanvas';
|
import { XYCanvas } from './XYCanvas';
|
||||||
@ -21,13 +21,25 @@ export function EventsCanvas({ id, events, renderEventMarker, mapEventToXYCoords
|
|||||||
// render token required to re-render annotation markers. Rendering lines happens in uPlot and the props do not change
|
// render token required to re-render annotation markers. Rendering lines happens in uPlot and the props do not change
|
||||||
// so we need to force the re-render when the draw hook was performed by uPlot
|
// so we need to force the re-render when the draw hook was performed by uPlot
|
||||||
const [renderToken, setRenderToken] = useState(0);
|
const [renderToken, setRenderToken] = useState(0);
|
||||||
|
const isMounted = useRef(false);
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
|
if (isMounted.current === false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
config.addHook('draw', () => {
|
config.addHook('draw', () => {
|
||||||
setRenderToken((s) => s + 1);
|
setRenderToken((s) => s + 1);
|
||||||
});
|
});
|
||||||
}, [config, setRenderToken]);
|
}, [config, setRenderToken]);
|
||||||
|
|
||||||
|
// clean up action to avoid memory leak
|
||||||
|
useEffect(() => {
|
||||||
|
isMounted.current = true;
|
||||||
|
return () => {
|
||||||
|
isMounted.current = false;
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
const eventMarkers = useMemo(() => {
|
const eventMarkers = useMemo(() => {
|
||||||
const markers: React.ReactNode[] = [];
|
const markers: React.ReactNode[] = [];
|
||||||
const plotInstance = plotCtx.plot;
|
const plotInstance = plotCtx.plot;
|
||||||
|
Loading…
Reference in New Issue
Block a user