chore: fix time range change race condition cp-7.74.0#28939
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
| <View style={styles.container}> | ||
| <View style={styles.chartSurface}> | ||
| <WebView | ||
| key={`advanced-chart-${ohlcvSeriesKey ?? ''}`} |
There was a problem hiding this comment.
When ohlcvSeriesKey changes, the key string changes and react destroys the old WebView and creates a new one.
|
✅ E2E Fixture Validation — Schema is up to date |
| return; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
check to make sure we do not send stale data when ohlcvSeriesKey changes, but should wait for fresh data. If it is stale, it returns and skips sendOHLCVData
| const prevOhlcvDataRef = useRef<OHLCVBar[]>([]); | ||
| const prevOhlcvSeriesKeyRef = useRef<string | undefined>(undefined); | ||
| /** When non-null, `ohlcvData` is still the previous series' array; skip sync until the hook replaces it. */ | ||
| const ohlcvSeriesStaleSnapshotRef = useRef<OHLCVBar[] | null>(null); |
There was a problem hiding this comment.
Holds the previous series' ohlcvData array reference after ohlcvSeriesKey changes (see
guard below). useOHLCVChart does not clear data until the new fetch resolves.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f7a52c7. Configure here.
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
Since no E2E test tags cover this charting functionality and the changes are well-contained internal bug fixes, no E2E tags need to be run. Performance Test Selection: |
|




Description
Fixes the token advanced chart staying scrolled back in time when users pan and then change the time range quickly. Also avoids sending previous range OHLCV into the WebView while the new fetch is still in flight
Problem
Viewport / scroll — TradingView keeps pan/scroll state inside the WebView. Changing range without a full new surface could reuse that state, so the chart looked wrong after a fast time-range change.
Stale data — useOHLCVChart keeps the last ohlcvData until the new request completes. Right after a range change, props can show the new ohlcvSeriesKey but the old candle array (same reference). Sending that as SET_OHLCV_DATA for the new range causes bad data / races.
Solution
WebView key — Tie key to ohlcvSeriesKey (with ?? '' when the prop is omitted) so each series change remounts the WebView and drops inherited TradingView scroll state.
ohlcvSeriesStaleSnapshotRef + guard — When the series key changes but ohlcvData is still the previous array reference, don’t sync full OHLCV; wait until the hook returns a new array reference, then send.
useEffect on ohlcvSeriesKey — Reset loading / ready state (chartReadyCount, webViewLoaded, layout settle timers) and clear ohlcvSeriesStaleSnapshotRef. Reset activeIndicatorsRef, prevPositionLinesRef, and prevChartTypeRef so the new WebView gets indicators, position lines, and chart type (line vs candles) applied correctly without a default-candles flash.
Changelog
CHANGELOG entry: Fixes race condition on timeRange switch
Related issues
Fixes:
Manual testing steps
Screenshots/Recordings
Before
Uploading Screen Recording 2026-04-16 at 23.39.54.mov…
After
Uploading Screen Recording 2026-04-17 at 00.10.19.mov…
Pre-merge author checklist
Performance checks (if applicable)
trace()for usage andaddTokenfor an exampleFor performance guidelines and tooling, see the Performance Guide.
Pre-merge reviewer checklist
Note
Medium Risk
Touches chart lifecycle/sync logic and WebView remount behavior, which could regress loading/skeleton timing or data updates across range switches if edge cases weren’t covered.
Overview
Fixes a race on time-range changes in
AdvancedChartby remounting the WebView whenohlcvSeriesKeychanges (via akey) and resetting “ready/loading/layout settle” state so the new instance re-applies indicators, position lines, and chart type deterministically.Adds a stale-series guard (
ohlcvSeriesStaleSnapshotRef) that suppressesSET_OHLCV_DATAsyncing when the series key updates butohlcvDatais still the previous array reference, resuming only once fresh data arrives; updates tests to reflect the WebView remount/load sequence and stale-data wait behavior.Reviewed by Cursor Bugbot for commit f7a52c7. Bugbot is set up for automated code reviews on this repo. Configure here.