fix: Perps chart viewport breaking when revisiting a candle interval#29344
Conversation
geositta
left a comment
There was a problem hiding this comment.
I tested, looks good, could we add some unit tests for CandleStreamChannel.mergeCandleData ?
|
✅ E2E Fixture Validation — Schema is up to date |
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
Tag selection rationale:
No changes to shared components (TabBar, Navigation, Engine, Controllers, or E2E infrastructure), so no broader test coverage is needed beyond the Perps-related tags. Performance Test Selection: |
|



Description
Fixes a bug where the Perps trading chart's viewport would break, flicker, or "jump between views" when navigating back to a previously visited candle interval (e.g. 4h → 8h → 4h). The first visit to an interval worked correctly, but revisits would render the wrong zoom, drop candles older than the visible range, and reset continuously on every live tick.
Root causes
There were two independent bugs compounding each other:
In CandleStreamChannel, the first visit to an interval fetches OneWeek of candles (~42 for 4h). On revisit, we intentionally do a lighter OneDay refetch (~6 candles) to conserve rate limit budget. However, the subscription callback was doing a full cache.set(cacheKey, incoming) — replacing the cached 42 candles with only 6.
When that shrunken dataset propagated to the WebView, TradingView's stored visible range pointed outside the new data, causing the viewport to collapse and candles older than the visible window to disappear.
In TradingViewChart, every new candleData prop (including live ticks) sent SET_CANDLESTICK_DATA to the WebView, which called candlestickSeries.setData(fullDataset). setData is a destructive operation that resets the chart's internal state. On revisit this collided with the shrunken-cache propagation, producing the flicker and zoom reset the user experienced.
Changes
CandleStreamChannel.ts — merge instead of replace
TradingViewChart.tsx — route live ticks to an incremental path
SET_CANDLESTICK_DATA(full reload) for initial load, symbol/interval change, history prepend, count decrease, or multi-bar jump.UPDATE_LAST_CANDLE(incremental, 1–2 candles) for live ticks and single-bar appends on the same symbol/interval/firstTime.CHART_READYclears the signature so a WebView remount always forces a full reload.CLEAR_DATAclears the signature so the next send is a full reload (empty chart can't be incrementally updated).TradingViewChartTemplate.tsx — handle incremental updates
Changelog
CHANGELOG entry: fixes bug related to chart re-rendering and flickering
Related issues
Fixes: #29232 https://consensyssoftware.atlassian.net/browse/TAT-2973
Manual testing steps
Screenshots/Recordings
Before
Screen.Recording.2026-04-24.at.12.23.06.PM.mov
After
Screen.Recording.2026-04-24.at.12.19.40.PM.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 candle caching/merging and the WebView messaging path used to render the Perps chart; mistakes could cause stale/incorrect candles, memory growth, or regress zoom/viewport behavior across symbols/intervals.
Overview
Fixes Perps chart flicker/viewport jumps when revisiting candle intervals by preserving candle history in cache and avoiding full chart resets on every tick.
CandleStreamChannelnow merges incoming candle updates into the existing cached dataset (timestamp-based, incoming wins) and caps cached history at1000candles, notifying subscribers with the merged result instead of replacing the cache.TradingViewChartintroduces signature-based routing to send either a fullSET_CANDLESTICK_DATAreload (initial load, symbol/interval/history changes, WebView remount, or cleared data) or an incrementalUPDATE_LAST_CANDLEmessage (live ticks / single-bar append, sending only the last 1–2 candles). The WebView template adds anUPDATE_LAST_CANDLEhandler that appliesseries.update()and keeps internal candle/volume state in sync.Adds focused regression tests for both the incremental update routing (
TradingViewChart.incremental.test.tsx) and the candle cache merge invariants (CandleStreamChannel.test.ts).Reviewed by Cursor Bugbot for commit 91eaa5d. Bugbot is set up for automated code reviews on this repo. Configure here.