Skip to content

Commit 39da255

Browse files
committed
avoid issue from functions calling too early
1 parent 1c1960d commit 39da255

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uplot-react-native",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "React Native wrapper for uPlot on web, iOS, and Android",
55
"homepage": "https://github.com/murphycj/uplot-react-native",
66
"bugs": {

src/components/ChartUPlot.tsx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
336336
const variablesRef = useRef<{ [key: string]: any }>({});
337337
const initialized = useRef<boolean>(false);
338338
const containerRef = useRef<any>(null);
339+
const loadedRef = useRef<boolean>(false);
339340
const dimensionsRef = useRef({
340341
containerWidth: Math.round(options?.width || style?.width || width),
341342
containerHeight: Math.round(options?.height || style?.height || height),
@@ -372,6 +373,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
372373
// memoized onLoadEnd handler for native WebView
373374
const handleLoadEnd = useCallback((): void => {
374375
// console.log(`handleLoadEnd | name=${name}`);
376+
loadedRef.current = true;
375377

376378
// Use canonical dataRef when creating the native WebView chart
377379
dataRef.current = toPlainArrays(data as any[]) as number[][];
@@ -428,12 +430,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
428430

429431
// Native WebView: detect reinitialization and restore variables/data
430432
if (shouldReinit) {
433+
// console.log('Reinitializing WebView chart');
434+
431435
initialized.current = false;
432436
destroy(true);
433-
}
434-
435-
if (shouldReinit) {
436-
// console.log('Reinitializing WebView chart');
437437

438438
// re-add any variables that were set
439439
let injectedVars = '';
@@ -575,6 +575,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
575575
* @param {Object} newOptions - The new options to set for the chart.
576576
*/
577577
const updateOptions = useCallback((newOptions: any): void => {
578+
if (!isWeb && !loadedRef.current) return;
578579
// console.log(`updateOptions | name=${name}`);
579580

580581
destroy(true); // keep data
@@ -588,6 +589,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
588589
* @param {number[][]} newData - The new data to set for the chart.
589590
*/
590591
const setData = useCallback((newData: number[][]): void => {
592+
if (!isWeb && !loadedRef.current) return;
591593
// console.log(`setData | name=${name}, newData length=${newData?.length}`);
592594

593595
// Keep canonical copy (plain arrays)
@@ -619,6 +621,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
619621
* Append a new data point across all series: [x, y1, y2, ...]
620622
*/
621623
const pushData = useCallback((item: number[]): void => {
624+
if (!isWeb && !loadedRef.current) return;
622625
// console.log(`pushData | name=${name}, item length=${item?.length}`);
623626

624627
// Update canonical copy locally first
@@ -668,6 +671,7 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
668671
*/
669672
const sliceSeries = useCallback(
670673
(axis: number, min: number, max: number): void => {
674+
if (!isWeb && !loadedRef.current) return;
671675
// console.log(
672676
// `sliceSeries | name=${name}, axis=${axis}, min=${min}, max=${max}`,
673677
// );
@@ -705,9 +709,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
705709

706710
// function to call setScale
707711
const setScale = useCallback((axis: string, options: any): void => {
708-
// console.log(
709-
// `setScale | name=${name}, axis=${axis}, options=${JSON.stringify(options)}`,
710-
// );
712+
if (!isWeb && !loadedRef.current) return;
713+
console.log(
714+
`setScale | name=${name}, axis=${axis}, options=${JSON.stringify(options)}`,
715+
);
711716

712717
if (isWeb) {
713718
uplotInstance.current?.setScale(axis, options);
@@ -731,9 +736,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
731736
// if web, sets the variable to window.[name]
732737
// if native, sets the variable to window.[name] via webref.current.injectJavaScript
733738
const setVariable = useCallback((name: string, value: any): void => {
734-
// console.log(
735-
// `setVariable | name=${name}, name=${name}, value=${JSON.stringify(value)}`,
736-
// );
739+
if (!isWeb && !loadedRef.current) return;
740+
console.log(
741+
`setVariable | name=${name}, name=${name}, value=${JSON.stringify(value)}`,
742+
);
737743

738744
variablesRef.current[name] = value;
739745

@@ -759,7 +765,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
759765

760766
// function to call setSize
761767
const setSize = useCallback((width: number, height: number): void => {
762-
// console.log(`setSize | name=${name}, width=${width}, height=${height}`);
768+
if (!isWeb && !loadedRef.current) return;
769+
console.log(`setSize | name=${name}, width=${width}, height=${height}`);
763770

764771
if (isWeb) {
765772
uplotInstance.current?.setSize(width, height);
@@ -782,9 +789,10 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
782789

783790
// function to call destroy, also clears the data
784791
const destroy = useCallback((keepData: boolean = false): void => {
785-
// console.log(
786-
// `destroy | name=${name}, keepData=${keepData}, data=${data?.length}`,
787-
// );
792+
if (!isWeb && !loadedRef.current) return;
793+
console.log(
794+
`destroy | name=${name}, keepData=${keepData}, data=${data?.length}`,
795+
);
788796

789797
if (!keepData) {
790798
dataRef.current = [];
@@ -828,7 +836,8 @@ const ChartUPlot = forwardRef<any, UPlotProps>(
828836
// destroy, clear data, and reinitialize the chart when the component unmounts
829837
const reset = useCallback(
830838
(opts: any, data: number[][], bgColor?: string): void => {
831-
// console.log(`reset | name=${name}`);
839+
if (!isWeb && !loadedRef.current) return;
840+
console.log(`reset | name=${name}`);
832841
destroy();
833842
createChart(opts, data, bgColor);
834843
},

0 commit comments

Comments
 (0)