UNPKG

state-synchronizers

Version:

Deterministically update state based on other state

32 lines (31 loc) 984 B
"use strict"; exports.__esModule = true; exports.getTopologicalSorting = void 0; function getTopologicalSorting(edges) { var vertices = Object.keys(edges); var visited = new Set(); var topologicallySortedVertices = []; var currentPath = []; var dfs = function (vertex) { if (currentPath.includes(vertex)) { var cycle = currentPath.join('->') + "->" + vertex; throw new Error("Cycle detected: " + cycle); } if (visited.has(vertex)) { return; } currentPath.push(vertex); visited.add(vertex); if (edges[vertex]) { edges[vertex].forEach(dfs); } topologicallySortedVertices.unshift(vertex); currentPath.pop(); }; vertices.forEach(function (vertex) { currentPath = []; dfs(vertex); }); return topologicallySortedVertices; } exports.getTopologicalSorting = getTopologicalSorting;