All files / src/state state-manager.js

76.47% Statements 117/153
72.72% Branches 8/11
45.45% Functions 5/11
76.47% Lines 117/153

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 1741x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x                                                                     1x 1x 1x 1x 1x   1x   1x   1x   1x   1x   1x   1x   1x 1x     1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x   1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 2x   1x 1x 1x 1x 2x 2x 2x   1x 1x 1x 1x 1x 1x 3x 3x  
/**
 * Simple state management for server-side rendering
 * This is mainly for component state during rendering
 */
 
const globalState = new Map();
 
/**
 * Creates a state container for a request/render cycle
 * @param {Object} initialState - Initial state object
 * @returns {Object} State container
 */
export function createState(initialState = {}) {
    const state = new Map(Object.entries(initialState));
 
    return {
        get(key) {
            return state.get(key);
        },
 
        set(key, value) {
            state.set(key, value);
            return this;
        },
 
        has(key) {
            return state.has(key);
        },
 
        delete(key) {
            return state.delete(key);
        },
 
        clear() {
            state.clear();
            return this;
        },
 
        toObject() {
            return Object.fromEntries(state);
        },
 
        // For debugging
        _internal: state
    };
}
 
/**
 * Global state for sharing data across components during SSR
 */
export const globalStateManager = {
    set(key, value) {
        globalState.set(key, value);
    },
 
    get(key) {
        return globalState.get(key);
    },
 
    has(key) {
        return globalState.has(key);
    },
 
    clear() {
        globalState.clear();
    },
 
    // Create isolated state for each request
    createRequestState() {
        return createState();
    }
};
 
/**
 * Context stack for managing nested context providers
 */
const contextStacks = new Map();
 
/**
 * Context provider for passing data down the component tree
 * @param {string} key - Context key
 * @param {*} value - Context value
 * @param {Object} children - Children to render with context
 * @returns {Object} Children with context available
 */
export function provideContext(key, value) {
    // Initialize context stack if it doesn't exist
    if (!contextStacks.has(key)) {
        contextStacks.set(key, []);
    }
    
    const stack = contextStacks.get(key);
    
    // Store previous value
    const previousValue = globalState.get(key);
    
    // Push previous value to stack and set new value
    stack.push(previousValue);
    globalState.set(key, value);
}
 
/**
 * Create a context provider component that works with the rendering system
 * @param {string} key - Context key
 * @param {*} value - Context value
 * @param {Object} children - Children to render with context
 * @returns {Function} Component function that provides context
 */
export function createContextProvider(key, value, children) {
    // Return a function that will render the children within the context
    return (renderFunction) => {
        try {
            // Provide context
            provideContext(key, value);
            
            // If a render function is provided, use it to render children
            // Otherwise return children to be rendered by the caller
            if (renderFunction && typeof renderFunction === 'function') {
                return renderFunction(children);
            } else {
                return children;
            }
        } finally {
            // Always restore context when done
            restoreContext(key);
        }
    };
}
 
/**
 * Restore context to previous value
 * @param {string} key - Context key
 */
export function restoreContext(key) {
    if (!contextStacks.has(key)) return;
    
    const stack = contextStacks.get(key);
    
    // Restore previous value from stack
    const previousValue = stack.pop();
    
    if (stack.length === 0) {
        // No more providers, delete the key if it was undefined before
        if (previousValue === undefined) {
            globalState.delete(key);
        } else {
            globalState.set(key, previousValue);
        }
        
        // Clean up empty stack
        contextStacks.delete(key);
    } else {
        // Restore previous value
        globalState.set(key, previousValue);
    }
}
 
/**
 * Clear all context stacks (useful for cleanup after rendering)
 */
export function clearAllContexts() {
    contextStacks.clear();
    // Note: This doesn't clear globalState as it might contain other data
}
 
/**
 * Context consumer to access provided context
 * @param {string} key - Context key
 * @returns {*} Context value
 */
export function useContext(key) {
    return globalState.get(key);
}