All files / src/lib/graph2d graphInstanceHelper.js

77.5% Statements 31/40
62.5% Branches 15/24
100% Functions 4/4
76.92% Lines 30/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98            19x 38x     38x         38x       38x 38x 38x         3x 3x     3x                           38x 5x 5x           33x 99x   33x   6x   4x 4x 4x 12x   4x   2x         4x   4x       27x                       19x 11x 4x       7x    
import { Graph2D } from "graph2d-react";
 
/**
 * Safely creates a Graph2D instance with error handling
 * @returns {object|null} Graph2D instance or null if creation fails
 */
export const createGraphInstance = () => {
    try {
        // Check Graph2D type
 
        Iif (!Graph2D) {
            console.error("[GraphInstanceHelper] Graph2D is not available");
            return null;
        }
 
        let instance = null;
 
        // Try different initialization approaches
        // Approach 1: Direct function call (most common for factory patterns)
        if (typeof Graph2D === "function") {
            try {
                instance = Graph2D();
                // Created instance via Graph2D()
            } catch (e) {
                // Direct call failed, trying constructor
                // Approach 2: Constructor with new keyword
                try {
                    instance = new Graph2D();
                    // Created instance via new Graph2D()
                } catch (e2) {
                    console.error("[GraphInstanceHelper] Both direct call and constructor failed:", e2);
                }
            }
        } else Eif (typeof Graph2D === "object") {
            // Approach 3: Object with factory methods
            if (typeof Graph2D.create === "function") {
                instance = Graph2D.create();
                // Created instance via Graph2D.create()
            } else if (typeof Graph2D.getInstance === "function") {
                instance = Graph2D.getInstance();
                // Created instance via Graph2D.getInstance()
            }
        }
 
        if (!instance) {
            console.error("[GraphInstanceHelper] Failed to create instance - all approaches failed");
            return null;
        }
 
        // Do not call instance.init() here — DrawingGraph (graph2d-react) registers canvases and runs init after layout.
 
        // Verify the instance has the required methods
        const requiredMethods = ["addCanvas", "setDrawingMode", "importAnswer"];
        const missingMethods = requiredMethods.filter((method) => typeof instance[method] !== "function");
 
        if (missingMethods.length > 0) {
            // Try to get the actual instance through getInstance
            if (typeof instance.getInstance === "function") {
                // Instance missing methods, trying getInstance()
                const actualInstance = instance.getInstance();
                Eif (actualInstance) {
                    const stillMissing = requiredMethods.filter(
                        (method) => typeof actualInstance[method] !== "function"
                    );
                    if (stillMissing.length === 0) {
                        // getInstance() provided complete instance
                        return actualInstance;
                    }
                }
            }
 
            console.warn("[GraphInstanceHelper] Instance missing required methods:", missingMethods);
            // Return the instance anyway - it might still be partially functional
            return instance;
        }
 
        // Instance created successfully with all required methods
        return instance;
    } catch (error) {
        console.error("[GraphInstanceHelper] Error creating Graph2D instance:", error);
        return null;
    }
};
 
/**
 * Checks if a graph instance is valid
 * @param {object} instance - The graph instance to check
 * @returns {boolean} True if instance is valid
 */
export const isValidGraphInstance = (instance) => {
    if (!instance || typeof instance !== "object") {
        return false;
    }
 
    // Check if it has the required addCanvas method
    return typeof instance.addCanvas === "function";
};