@@ -42,9 +42,15 @@ export function ChartsCatalogChart({
4242 revision ?: number
4343} ) {
4444 const containerRef = React . useRef < HTMLDivElement > ( null )
45+ const handleRef = React . useRef < ChartMountHandle | undefined > ( undefined )
46+ const inputRef = React . useRef ( { height, interactive, revision } )
47+ const onStatusRef = React . useRef ( onStatus )
4548 const [ visible , setVisible ] = React . useState ( ! defer )
4649 const [ failed , setFailed ] = React . useState ( false )
4750
51+ inputRef . current = { height, interactive, revision }
52+ onStatusRef . current = onStatus
53+
4854 React . useEffect ( ( ) => {
4955 const container = containerRef . current
5056 if ( ! defer || visible || ! container ) return
@@ -71,7 +77,7 @@ export function ChartsCatalogChart({
7177 if ( ! container || ! visible ) return
7278
7379 let cancelled = false
74- let handle : ChartMountHandle | undefined
80+ let mountedHandle : ChartMountHandle | undefined
7581 let width = measureWidth ( container )
7682 const preloadLinks = module . preload . map ( ( assetPath ) => {
7783 const link = document . createElement ( 'link' )
@@ -95,54 +101,60 @@ export function ChartsCatalogChart({
95101
96102 const mounted = loaded . mount ( container , {
97103 width,
98- height,
99- revision,
100- interactive,
104+ ...inputRef . current ,
101105 } )
102106 if ( ! isChartMountHandle ( mounted ) ) {
103107 throw new TypeError ( 'Invalid Charts catalog mount handle' )
104108 }
105- handle = mounted
106- requestAnimationFrame ( ( ) => onStatus ?.( 'ready' ) )
109+ mountedHandle = mounted
110+ handleRef . current = mounted
111+ requestAnimationFrame ( ( ) => {
112+ if ( ! cancelled ) onStatusRef . current ?.( 'ready' )
113+ } )
107114 } )
108115 . catch ( ( error : unknown ) => {
109116 if ( cancelled ) return
110117 console . error ( `Unable to mount Charts catalog case ${ caseId } ` , error )
111118 setFailed ( true )
112- onStatus ?.( 'error' )
119+ onStatusRef . current ?.( 'error' )
113120 } )
114121
115122 const resizeObserver = new ResizeObserver ( ( ) => {
116123 const nextWidth = measureWidth ( container )
117124 if ( nextWidth === width || nextWidth < 1 ) return
118125 width = nextWidth
119- handle ?. update ( {
126+ handleRef . current ?. update ( {
120127 width,
121- height,
122- revision,
123- interactive,
128+ ...inputRef . current ,
124129 } )
125- onStatus ?.( 'resize' )
130+ onStatusRef . current ?.( 'resize' )
126131 } )
127132 resizeObserver . observe ( container )
128133
129134 return ( ) => {
130135 cancelled = true
131136 resizeObserver . disconnect ( )
132- handle ?. destroy ( )
137+ mountedHandle ?. destroy ( )
138+ if ( handleRef . current === mountedHandle ) {
139+ handleRef . current = undefined
140+ }
133141 for ( const link of preloadLinks ) link . remove ( )
134142 container . replaceChildren ( )
135143 }
136- } , [
137- artifactRevision ,
138- caseId ,
139- height ,
140- interactive ,
141- module ,
142- onStatus ,
143- revision ,
144- visible ,
145- ] )
144+ } , [ artifactRevision , caseId , module , visible ] )
145+
146+ React . useEffect ( ( ) => {
147+ const container = containerRef . current
148+ const handle = handleRef . current
149+ if ( ! container || ! handle ) return
150+
151+ handle . update ( {
152+ width : measureWidth ( container ) ,
153+ height,
154+ interactive,
155+ revision,
156+ } )
157+ } , [ height , interactive , revision ] )
146158
147159 return (
148160 < div
0 commit comments