All files / state/src state-manager.js

70.83% Statements 34/48
80% Branches 12/15
50% Functions 9/18
70.21% Lines 33/47

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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194          3x                                                                                         3x   2x       1x               7x                       3x                     12x 10x     12x     12x     12x 12x                       2x 2x   2x       2x 2x           2x                   3x   2x     2x   2x   1x 1x           1x     1x                                     16x 9x   9x 8x   1x       16x                 14x    
/**
 * 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 E{
                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 E{
            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)
 *
 * useContext() reads from globalState, which is module-level and therefore
 * shared by every render in the process. Clearing only contextStacks left the
 * values themselves in place, so a context provided while rendering one
 * request stayed readable while rendering the next — and became unrecoverable,
 * since restoreContext() bails out once a key's stack is gone.
 *
 * Unwind each tracked key to the value it held before its first
 * provideContext() — the bottom of that key's stack. Only keys that were
 * actually provided as contexts are touched, so unrelated global state
 * survives, which is what the previous implementation was trying to protect.
 */
export function clearAllContexts() {
    for (const [key, stack] of contextStacks) {
        const beforeFirstProvide = stack[0];
 
        if (beforeFirstProvide === undefined) {
            globalState.delete(key);
        } else {
            globalState.set(key, beforeFirstProvide);
        }
    }
 
    contextStacks.clear();
}
 
/**
 * Context consumer to access provided context
 * @param {string} key - Context key
 * @returns {*} Context value
 */
export function useContext(key) {
    return globalState.get(key);
}