UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

34 lines 921 B
/** * @packageDocumentation * This is a simplified topologic sort that does not use a graph library like graphlib which causes bloat in the bundle size */ /** * Note that this will fail if there are cycles in the graph * @param graph * @returns */ export function topologicalSort(graph) { const visited = new Set(); const stack = []; function dfs(node) { visited.add(node); const neighbors = graph.successors(node); for (const neighbor of neighbors) { if (!visited.has(neighbor.id)) { dfs(neighbor.id); } } stack.unshift(node); // Add node to the front of the stack } //Sort the ids to ensure stability graph .getNodeIds() .sort() .forEach(node => { if (!visited.has(node)) { dfs(node); } }); return stack; } //# sourceMappingURL=topologicSort.js.map