UNPKG

@memberjunction/react-runtime

Version:

Platform-agnostic React component runtime for MemberJunction. Provides core compilation, registry, and execution capabilities for React components in any JavaScript environment.

113 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentResolver = void 0; class ComponentResolver { constructor(registry) { this.registry = registry; } resolveComponents(spec, namespace = 'Global') { const resolved = {}; this.resolveComponentHierarchy(spec, resolved, namespace); return resolved; } resolveComponentHierarchy(spec, resolved, namespace, visited = new Set()) { if (visited.has(spec.name)) { console.warn(`Circular dependency detected for component: ${spec.name}`); return; } visited.add(spec.name); const component = this.registry.get(spec.name, namespace); if (component) { resolved[spec.name] = component; } const children = spec.dependencies || []; for (const child of children) { this.resolveComponentHierarchy(child, resolved, namespace, visited); } } validateDependencies(spec, namespace = 'Global') { const missing = []; const checked = new Set(); this.checkDependencies(spec, namespace, missing, checked); return missing; } checkDependencies(spec, namespace, missing, checked) { if (checked.has(spec.name)) return; checked.add(spec.name); if (!this.registry.has(spec.name, namespace)) { missing.push(spec.name); } const children = spec.dependencies || []; for (const child of children) { this.checkDependencies(child, namespace, missing, checked); } } getDependencyGraph(spec) { const graph = new Map(); const visited = new Set(); this.buildDependencyGraph(spec, graph, visited); return graph; } buildDependencyGraph(spec, graph, visited) { if (visited.has(spec.name)) return; visited.add(spec.name); const children = spec.dependencies || []; const dependencies = children.map(child => child.name); graph.set(spec.name, dependencies); for (const child of children) { this.buildDependencyGraph(child, graph, visited); } } getLoadOrder(spec) { const graph = this.getDependencyGraph(spec); const visited = new Set(); const stack = []; for (const node of graph.keys()) { if (!visited.has(node)) { this.topologicalSortDFS(node, graph, visited, stack); } } return stack.reverse(); } topologicalSortDFS(node, graph, visited, stack) { visited.add(node); const dependencies = graph.get(node) || []; for (const dep of dependencies) { if (!visited.has(dep)) { this.topologicalSortDFS(dep, graph, visited, stack); } } stack.push(node); } resolveInOrder(spec, namespace = 'Global') { const loadOrder = this.getLoadOrder(spec); const resolved = []; for (const name of loadOrder) { const component = this.registry.get(name, namespace); if (component) { resolved.push({ name, component }); } } return resolved; } flattenComponentSpecs(spec) { const flattened = []; const visited = new Set(); this.collectComponentSpecs(spec, flattened, visited); return flattened; } collectComponentSpecs(spec, collected, visited) { if (visited.has(spec.name)) return; visited.add(spec.name); collected.push(spec); const children = spec.dependencies || []; for (const child of children) { this.collectComponentSpecs(child, collected, visited); } } } exports.ComponentResolver = ComponentResolver; //# sourceMappingURL=component-resolver.js.map