Repository navigation
fix: bound interval-window load via in-place resolution switch - #11625
sahar-fehri wants to merge 7 commits into
Conversation
🛑 Files requiring codeowner reviewTo merge this PR, you'll need to get approvals from the following owner groups. @MetaMask/metamask-assets or @MetaMask/perps (6 files)
@MetaMask/core-platform (expand for more) |
|
@metamaskbot publish-preview |
|
The "Publish preview" workflow has been updated to a new version that uses OIDC for authentication. Please use the new workflow instead of this one. To trigger the new workflow, go to the Publish a preview build workflow, click "Run workflow", and enter |
Replace the atomic `setTimeFrame` interval-switch mechanism with an in-place resolution switch on the persistent widget. Before calling `setResolution`, the time scale's absolute visible-range pin is released via `setRightOffset`, so TradingView preserves the visible bar count (bounded) instead of inheriting the previous interval's wide time span, which drove the coarse->fine pagination storm. The widget is no longer rebuilt, so studies, MAs, volume and chart type survive a switch. The opt-in `atomicIntervalWindow` flag now clamps the per-interval visible-range pin so its start never predates the oldest loaded bar, keeping framing inside loaded bars. A `countBack` backstop in the datafeed halts any over-large RN-backed pagination request as a final guard. Remove the now-dead `setTimeFrame`/`takeClientScreenshot` widget type declarations and the double-buffer/dimmed-snapshot remnants, and update the CHANGELOG and docs to describe the in-place approach. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
Explanation
How TradingView sizes its initial data request (by default)
The widget is mounted in its container and knows the container's pixel dimensions before it ever calls
getBars. The data request is derived from the width, not the other way around.The actual order:
→ TV knows
containerWidthPx+ defaultbarSpacing(px/bar) immediately.visibleBars ≈ containerWidthPx ÷ barSpacingPxcountBack = visibleBars + indicator lookback + buffer (≈ 300)getBars({ resolution: '1', countBack, to: now })→ datafeed returns ~300 one-minute bars.
visibleBarsof them, right edge = latest bar.So the sequence is layout → countBack → getBars → render.
getBarsis step 3; the width-based sizing is step 2. That is also why the docs say "the library calculates the amount of data necessary to fill the chart space" — "chart space" is the already-known container size.So how does it "know" to show last hour vs last 6 hours?
By default — it doesn't, and it doesn't try to. There is no time-span target. TV targets a bar count (
width ÷ barSpacing), and the time span you end up seeing is just:For a 1-minute chart on a typical container this works out to roughly the last hour; for a 1-day chart the same bar count covers roughly the last year. The span is a side effect of the bar count, not a goal.
The problem on an interval switch
When the user switches intervals, TradingView preserves the previous interval's visible time span and sizes the first
getBarsrequest'scountBackto fill that inherited range at the new bar duration. A coarse→fine switch (e.g.1W → 1m) therefore derives an enormouscountBack— hundreds of thousands of bars — triggering a deep backward-pagination storm that can hang the chart.Why TV's documented range APIs don't help
Our architecture pushes pre-fetched data from the host and uses TradingView as a rendering engine (the chart iframe has
connect-src: none— it cannot fetch data itself). TV'scountBackis computed internally from the inherited viewport span and is not influenced by any range-setting API (setVisibleRange,setTimeFrame,onIntervalChangedtimeframe injection — all tested, none resize the fetch). Those APIs are designed for the lifecycle where TV owns data loading; in our inverted architecture (host pushes data, datafeed serves from memory) they set the display range but do not feed back into the switch-fetch sizing. The fix therefore operates at the datafeed layer where we have full control.The fix
Two-part fix:
Datafeed pagination cap. An RN-backed (Strategy B) backward-pagination request with
countBackaboveMAX_RNBACKED_PAGINATION_COUNTBACK(10,000) is the inherited-range artifact, never a real scroll-back. It is halted withnoData: true. Real scroll-back asks for only a viewport's worth of bars and passes through unaffected.Synchronous visible-range application. On an in-place resolution switch, the
setResolutioncallback applies the per-interval visible range immediately (applyVisibleRangeImmediate) instead of subscribing toonDataLoaded— which never re-fires for in-place switches. This ensures the window lands on the correct per-interval range after every switch.Opt-in (default OFF). An optional
atomicIntervalWindowboolean on theSET_OHLCV_DATApayload gates the synchronous visible-range application and the oldest-bar clamping. Whentrue, the per-interval visible-range pin applied after data loads is clamped so its start never predates the oldest loaded bar, keeping framing inside the bars already in memory. When the flag is absent orfalse, the pin uses the requested start unclamped — byte-for-byte like today's framing — so existing consumers (Social Leaderboard, mobile) are unaffected until they explicitly opt in.Details:
setResolution(newResolution, cb)runs. In the callback the main price scale auto-scale is reset, the data lifecycle is notified, and the per-interval visible range is applied (synchronously for opt-in, viaonDataLoadedfor non-opt-in).CHART_LAYOUT_SETTLEDis emitted.max(visibleFromMs, oldestLoadedBar)) on the post-data visible-range pin is gated by theatomicIntervalWindowflag — non-opt-in consumers keep the unclamped start, preserving today's framing exactly.MAX_RNBACKED_PAGINATION_COUNTBACKcap halts inherited-range pagination storms at the datafeed layer. The cap is unconditional (applies regardless of the opt-in flag) because the storm is always an artifact, never a real user scroll-back.Changelog
@metamask/advanced-chart-coreatomicIntervalWindowflag on theSET_OHLCV_DATApayload that bounds the initial historical-data load on interval switches by clamping the per-interval visible-range pin to the loaded bars and capping backward-pagination storms at the datafeed layer. Off by default; consumers keep today's unclamped framing until they opt in.References
Related to ASSETS-4400
Manual testing steps
With the opt-in enabled (
atomicIntervalWindow: true— e.g. the extension):1D → 1m) and confirm the initial load completes quickly — no burst of deep backward-pagination requests (the datafeed cap halts the inherited-range storm).1m → 1W) and confirm the same: no pagination storm.With the opt-in absent/
false(default — e.g. Social Leaderboard / mobile):Checklist