@@ -225,8 +225,22 @@ function getCreateChartString(
225225 if (window._chart) {
226226 try { window._chart.destroy(); } catch (e) {}
227227 }
228+
229+ // get element by id and make sure it exists
230+ var chartEl = document.getElementById('chart');
231+ if (!chartEl) {
232+ console.error('createUPlotChart: chart element not found');
233+ return;
234+ }
235+
236+ // create the chart
237+
238+ requestAnimationFrame(() => {
239+ window._chart = new uPlot(window._opts, window._data, chartEl);
240+ window.__CHART_CREATED__ = true;
241+ console.log('uPlot chart created (after rAF)');
242+ });
228243
229- window._chart = new uPlot(window._opts, window._data, document.getElementById('chart'));
230244
231245 // mark created and flush queued commands
232246 window.__CHART_CREATED__ = true;
@@ -329,6 +343,9 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
329343 const { width, height } = useWindowDimensions ( ) ;
330344 let webref : any = useRef ( null ) ;
331345 const uplotInstance = useRef < any > ( null ) ;
346+ // const [webviewKey, setWebviewKey] = useState<string>(
347+ // `uplot-webview-${Date.now()}`,
348+ // );
332349 // Ensure we keep an independent copy of the incoming `data` so modifications
333350 // to dataRef.current do NOT mutate the original prop. toPlainArrays converts
334351 // typed arrays into plain arrays and returns new arrays.
@@ -349,7 +366,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
349366 const handleLayout = useCallback ( ( event ) => {
350367 const { width, height } = event . nativeEvent . layout ;
351368 // console.log(
352- // `handleLayout | name=${name}, width=${width}, height=${height}`,
369+ // `handleLayout | name=${name}, width=${width}, height=${height}, timeMs=${Date.now()} `,
353370 // );
354371
355372 dimensionsRef . current = {
@@ -374,13 +391,12 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
374391
375392 // memoized onLoadEnd handler for native WebView
376393 const handleLoadEnd = useCallback ( ( ) : void => {
377- // console.log(`handleLoadEnd | name=${name}`);
394+ // console.log(`handleLoadEnd | name=${name}, timeMs=${Date.now()} `);
378395 loadedRef . current = true ;
379396
380397 // Use canonical dataRef when creating the native WebView chart
381398 dataRef . current = toPlainArrays ( data as any [ ] ) as number [ ] [ ] ;
382399 createChart ( options , dataRef . current , bgColor ) ;
383-
384400 if ( onLoad ) {
385401 onLoad ( ) ;
386402 }
@@ -432,7 +448,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
432448
433449 // Native WebView: detect reinitialization and restore variables/data
434450 if ( shouldReinit ) {
435- // console.log('Reinitializing WebView chart');
451+ console . log ( 'Reinitializing WebView chart' ) ;
436452
437453 initialized . current = false ;
438454 destroy ( true ) ;
@@ -522,12 +538,17 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
522538
523539 // eslint-disable-next-line @typescript-eslint/no-explicit-any
524540 const createChart = useCallback (
525- ( opts : any , data : number [ ] [ ] | null = null , bgColor ?: string ) : void => {
541+ (
542+ opts : any ,
543+ data : number [ ] [ ] | null = null ,
544+ bgColor ?: string ,
545+ force : boolean = false ,
546+ ) : void => {
526547 // console.log(
527- // `createChart | name=${name} | initialized=${initialized.current}`,
548+ // `createChart | name=${name} | initialized=${initialized.current}, timeMs=${Date.now()}, w=${dimensionsRef.current.containerWidth}, h=${dimensionsRef.current.containerHeight} `,
528549 // );
529550
530- if ( initialized . current ) {
551+ if ( initialized . current && ! force ) {
531552 return ;
532553 }
533554
@@ -564,6 +585,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
564585 ) ;
565586
566587 webref . current . injectJavaScript ( createChartStr ) ;
588+ // console.log('uPlot createChart injected, timeMs=', Date.now());
567589 }
568590 initialized . current = true ;
569591 } ,
@@ -885,8 +907,6 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
885907 reset,
886908 } ) ) ;
887909
888- // console.log(`render | name=${name}`);
889-
890910 if ( Platform . OS === 'web' ) {
891911 return (
892912 < View
@@ -899,6 +919,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
899919 return (
900920 < WebView
901921 { ...webviewProps }
922+ // key={webviewKey}
902923 originWhitelist = { [ '*' ] }
903924 source = { { html : finalHtml } }
904925 allowingReadAccessToURLs = { true }
@@ -913,6 +934,16 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
913934 injectedJavaScriptWithFunctions
914935 }
915936 onMessage = { handleMessage }
937+ // onContentProcessDidTerminate={(_) => {
938+ // IOS
939+ // console.log('onContentProcessDidTerminate');
940+ // setAutoIncrementingNumber(autoIncrementingNumber + 1);
941+ // }}
942+ // onRenderProcessGone={(_) => {
943+ // Android
944+ // setAutoIncrementingNumber(autoIncrementingNumber + 1);
945+ // console.log('onRenderProcessGone');
946+ // }}
916947 />
917948 ) ;
918949 }
0 commit comments