From b6d336ea0bf4f63e69cbbcb9fdacb27bb6802be2 Mon Sep 17 00:00:00 2001 From: Doug Martin Date: Thu, 18 Jul 2024 07:52:19 -0400 Subject: [PATCH] fix: Stop series data not saving when max values reached [PT-187949831] Previously there was a useCallback() wrapper on the stop time series function that depended on the formData but this caused issues with the formData being captured as an empty array at the start of multiple runs where the runs hit their max number of values. This fix adds a ref to the formData so that the closure is not created. --- src/shared/components/data-table-field.tsx | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/shared/components/data-table-field.tsx b/src/shared/components/data-table-field.tsx index 6ee3883..d546649 100644 --- a/src/shared/components/data-table-field.tsx +++ b/src/shared/components/data-table-field.tsx @@ -193,6 +193,7 @@ export const DataTableField: React.FC = props => { }, [isTimeSeries, formData]); const [selectableSensorId, setSelectableSensorId] = useState(); const {log} = formContext; + const timeSeriesFormDataRef = useRef(undefined); // So this is a bit of a hack - if we use the setInputDisabled function passed in the formContext // the timeseries data saving fails, probably because of a re-render which updates the formData. @@ -389,11 +390,15 @@ export const DataTableField: React.FC = props => { const timeSeriesMetadata = getTimeSeriesMetadata(timeSeriesCapabilities); + // save a ref to the data so that when we stop the stop handler doesn't keep a closure over the data + timeSeriesFormDataRef.current = []; + stopTimeSeriesFnRef.current = sensor.collectTimeSeries(timeSeriesCapabilities.measurementPeriod, selectableSensorId, (values) => { const newData = formData.slice(); newData[rowIdx] = {...newData[rowIdx], timeSeries: values, timeSeriesMetadata}; if (values.length <= MaxNumberOfTimeSeriesValues) { setFormData(newData); + timeSeriesFormDataRef.current = newData; } if (values.length === MaxNumberOfTimeSeriesValues) { onSensorStopTimeSeries(); @@ -417,17 +422,18 @@ export const DataTableField: React.FC = props => { } }; - const onSensorStopTimeSeries = useCallback(() => { + const onSensorStopTimeSeries = () => { // no check of input disabled here as this handler updates it - if (stopTimeSeriesFnRef.current) { + if (stopTimeSeriesFnRef.current && timeSeriesFormDataRef.current) { log?.("sensorStopTimeSeries"); stopTimeSeriesFnRef.current?.(); stopTimeSeriesFnRef.current = undefined; timeSeriesRecordingRowRef.current = undefined; - saveData(formData); + saveData(timeSeriesFormDataRef.current); + timeSeriesFormDataRef.current = undefined; setInputDisabled(false); } - }, [formData]); + }; // make sure the time series recording stops if the tab is changed useEffect(() => {