|
1 | 1 | import {useEffect, useMemo, useState} from 'react';
|
| 2 | +import {useEffectEvent} from '@react-aria/utils'; |
2 | 3 | import * as Sentry from '@sentry/react';
|
3 | 4 | import isEmpty from 'lodash/isEmpty';
|
4 | 5 | import isEqualWith from 'lodash/isEqualWith';
|
@@ -186,26 +187,32 @@ export const useSortedTimeSeries = <
|
186 | 187 | : {};
|
187 | 188 | }, [timeSeriesResult.data]);
|
188 | 189 |
|
189 |
| - useEffect(() => { |
190 |
| - if ( |
191 |
| - isTimeSeriesEndpointComparisonEnabled && |
192 |
| - !result.isFetching && |
193 |
| - !isEmpty(data) && |
194 |
| - !timeSeriesResult.isFetching && |
195 |
| - !isEmpty(otherData) |
196 |
| - ) { |
| 190 | + // We don't want to re-run the comparison every time the `data` changes, since |
| 191 | + // `data` might be changed or mutated by the live reload functionality. |
| 192 | + // Instead, extract that into an effect event so it doesn't have a dependency |
| 193 | + // on `data`. |
| 194 | + const compareResponses = useEffectEvent(() => { |
| 195 | + if (!isEmpty(data) && !isEmpty(otherData)) { |
197 | 196 | if (!isEqualWith(data, otherData, comparator)) {
|
198 | 197 | warn(`\`useDiscoverSeries\` found a data difference in responses`, {
|
199 | 198 | statsData: JSON.stringify(data),
|
200 | 199 | timeSeriesData: JSON.stringify(otherData),
|
201 | 200 | });
|
202 | 201 | }
|
203 | 202 | }
|
| 203 | + }); |
| 204 | + |
| 205 | + useEffect(() => { |
| 206 | + if ( |
| 207 | + isTimeSeriesEndpointComparisonEnabled && |
| 208 | + !result.isFetching && |
| 209 | + !timeSeriesResult.isFetching |
| 210 | + ) { |
| 211 | + compareResponses(); |
| 212 | + } |
204 | 213 | }, [
|
205 | 214 | isTimeSeriesEndpointComparisonEnabled,
|
206 |
| - data, |
207 | 215 | result.isFetching,
|
208 |
| - otherData, |
209 | 216 | timeSeriesResult.isFetching,
|
210 | 217 | ]);
|
211 | 218 |
|
@@ -370,14 +377,18 @@ const NUMERIC_KEYS: Array<symbol | string | number> = [
|
370 | 377 | function comparator(
|
371 | 378 | valueA: unknown,
|
372 | 379 | valueB: unknown,
|
373 |
| - key: symbol | string | number | undefined |
| 380 | + key: symbol | string | number | undefined, |
| 381 | + objA: Record<PropertyKey, unknown>, |
| 382 | + objB: Record<PropertyKey, unknown> |
374 | 383 | ) {
|
375 | 384 | // Compare numbers by near equality, which makes the comparison less sensitive to small natural variations in value caused by request sequencing
|
376 | 385 | if (
|
377 | 386 | key &&
|
378 | 387 | NUMERIC_KEYS.includes(key) &&
|
379 | 388 | typeof valueA === 'number' &&
|
380 |
| - typeof valueB === 'number' |
| 389 | + typeof valueB === 'number' && |
| 390 | + !objA?.incomplete && |
| 391 | + !objB?.incomplete |
381 | 392 | ) {
|
382 | 393 | return areNumbersAlmostEqual(valueA, valueB, 5);
|
383 | 394 | }
|
|
0 commit comments