@@ -55,7 +55,7 @@ const cascadingRedraw = _.throttle(function internalRedraw() {
5555 x . _maybeRenderedInBadState = false ;
5656 }
5757 window . cancelAnimationFrame ( redrawRaf ) ;
58- window . requestAnimationFrame ( internalRedraw ) ;
58+ redrawRaf = window . requestAnimationFrame ( internalRedraw ) ;
5959} , 100 ) ;
6060
6161// A component that fetches data from the TensorBoard server and renders it into
@@ -140,6 +140,38 @@ class _TfLineChartDataLoader<ScalarMetadata>
140140 ` ;
141141
142142 private _redrawRaf : number | null = null ;
143+ private _resizeObserver : ResizeObserver | null = null ;
144+ private _lastObservedWidth = - 1 ;
145+ private _lastObservedHeight = - 1 ;
146+
147+ override connectedCallback ( ) {
148+ super . connectedCallback ( ) ;
149+ this . _resizeObserver = new ResizeObserver ( ( entries ) => {
150+ if ( entries . length === 0 ) return ;
151+ const { width, height} = entries [ 0 ] . contentRect ;
152+ const roundedWidth = Math . round ( width ) ;
153+ const roundedHeight = Math . round ( height ) ;
154+ if (
155+ roundedWidth === this . _lastObservedWidth &&
156+ roundedHeight === this . _lastObservedHeight
157+ ) {
158+ return ;
159+ }
160+ this . _lastObservedWidth = roundedWidth ;
161+ this . _lastObservedHeight = roundedHeight ;
162+
163+ if ( roundedWidth <= 0 || roundedHeight <= 0 || ! this . active ) {
164+ this . _maybeRenderedInBadState = true ;
165+ return ;
166+ }
167+
168+ if ( ! redrawQueue . includes ( this ) ) {
169+ redrawQueue . push ( this ) ;
170+ }
171+ cascadingRedraw ( ) ;
172+ } ) ;
173+ this . _resizeObserver . observe ( this ) ;
174+ }
143175
144176 @property ( {
145177 type : Boolean ,
@@ -231,6 +263,10 @@ class _TfLineChartDataLoader<ScalarMetadata>
231263 }
232264
233265 override disconnectedCallback ( ) {
266+ if ( this . _resizeObserver !== null ) {
267+ this . _resizeObserver . disconnect ( ) ;
268+ this . _resizeObserver = null ;
269+ }
234270 super . disconnectedCallback ( ) ;
235271 if ( this . _redrawRaf !== null ) cancelAnimationFrame ( this . _redrawRaf ) ;
236272 }
0 commit comments