-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
chore: fix time range change race condition cp-7.74.0 #28939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,8 @@ const AdvancedChart = forwardRef<AdvancedChartRef, AdvancedChartProps>( | |
| const prevChartTypeRef = useRef(chartType); | ||
| 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); | ||
| const tradingViewOpenInterceptRef = useRef(0); | ||
|
|
||
| const htmlContent = useMemo( | ||
|
|
@@ -141,6 +143,7 @@ const AdvancedChart = forwardRef<AdvancedChartRef, AdvancedChartProps>( | |
| prevChartTypeRef.current = undefined; | ||
| prevOhlcvDataRef.current = []; | ||
| prevOhlcvSeriesKeyRef.current = undefined; | ||
| ohlcvSeriesStaleSnapshotRef.current = null; | ||
| }, [htmlContent]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
|
||
| // ---- Helpers ---- | ||
|
|
@@ -171,6 +174,22 @@ const AdvancedChart = forwardRef<AdvancedChartRef, AdvancedChartProps>( | |
| }, LAYOUT_SETTLE_FALLBACK_MS); | ||
| }, [isChartReady, clearLayoutSettleTimeout]); | ||
|
|
||
| // WebView remounts when `key` changes; parent state would otherwise still look "ready". | ||
| // `CHART_READY` clears indicator/position/chart-type refs once the new instance loads. | ||
| useEffect(() => { | ||
| if (ohlcvSeriesKey === undefined) { | ||
| return; | ||
| } | ||
| setChartReadyCount(0); | ||
| setWebViewLoaded(false); | ||
| setLayoutSettling(false); | ||
| clearLayoutSettleTimeout(); | ||
| ohlcvSeriesStaleSnapshotRef.current = null; | ||
| activeIndicatorsRef.current.clear(); | ||
| prevPositionLinesRef.current = undefined; | ||
| prevChartTypeRef.current = undefined; | ||
| }, [ohlcvSeriesKey, clearLayoutSettleTimeout]); | ||
|
|
||
| useEffect( | ||
| () => () => { | ||
| clearLayoutSettleTimeout(); | ||
|
|
@@ -381,6 +400,7 @@ const AdvancedChart = forwardRef<AdvancedChartRef, AdvancedChartProps>( | |
| prevChartTypeRef.current = undefined; | ||
| prevOhlcvDataRef.current = []; | ||
| prevOhlcvSeriesKeyRef.current = undefined; | ||
| ohlcvSeriesStaleSnapshotRef.current = null; | ||
| webViewRef.current?.reload(); | ||
| }, | ||
| }), | ||
|
|
@@ -397,19 +417,23 @@ const AdvancedChart = forwardRef<AdvancedChartRef, AdvancedChartProps>( | |
| useEffect(() => { | ||
| if (ohlcvData.length === 0 || !webViewLoaded) return; | ||
|
|
||
| if (ohlcvSeriesStaleSnapshotRef.current !== null) { | ||
| if (ohlcvData !== ohlcvSeriesStaleSnapshotRef.current) { | ||
| ohlcvSeriesStaleSnapshotRef.current = null; | ||
| } else { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 prevData = prevOhlcvDataRef.current; | ||
|
|
||
| if ( | ||
| ohlcvSeriesKey !== undefined && | ||
| ohlcvSeriesKey !== prevOhlcvSeriesKeyRef.current | ||
| ) { | ||
| if (prevOhlcvSeriesKeyRef.current !== undefined) { | ||
| // Time range switch: ohlcvData is still stale from the previous | ||
|
sahar-fehri marked this conversation as resolved.
|
||
| // period (fetch is in progress). Show skeleton, mark the key, and | ||
| // clear prevData so the fresh data triggers the length-diff branch | ||
| // on arrival — avoiding sending stale data to the WebView which | ||
| // causes a resolution race condition in TradingView. | ||
| beginFullOhlcvLayoutSettle(); | ||
| ohlcvSeriesStaleSnapshotRef.current = ohlcvData; | ||
| prevOhlcvSeriesKeyRef.current = ohlcvSeriesKey; | ||
| prevOhlcvDataRef.current = []; | ||
| return; | ||
|
|
@@ -561,6 +585,7 @@ const AdvancedChart = forwardRef<AdvancedChartRef, AdvancedChartProps>( | |
| <View style={styles.container}> | ||
| <View style={styles.chartSurface}> | ||
| <WebView | ||
| key={`advanced-chart-${ohlcvSeriesKey ?? ''}`} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When ohlcvSeriesKey changes, the key string changes and react destroys the old WebView and creates a new one. |
||
| ref={webViewRef} | ||
| source={{ html: htmlContent, baseUrl: CHARTING_LIBRARY_BASE_URL }} | ||
| style={styles.webview} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Holds the previous series'
ohlcvDataarray reference afterohlcvSeriesKeychanges (seeguard below).
useOHLCVChartdoes not clear data until the new fetch resolves.