| 1 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 2 | // @ts-nocheck |
| 3 | export function tsort(edges) { |
| 4 | const nodes = new Map(), sorted = [], visited = new Map() |
| 5 | |
| 6 | const Node = function(id) { |
| 7 | this.id = id |
| 8 | this.afters = [] |
| 9 | } |
| 10 | |
| 11 | edges.forEach((v) => { |
| 12 | const from = v[0], to = v[1] |
| 13 | if (!nodes.get(from)) nodes.set(from, new Node(from)) |
| 14 | if (!nodes.get(to)) nodes.set(to, new Node(to)) |
| 15 | nodes.get(from).afters.push(to) |
| 16 | }) |
| 17 | ;[...nodes.keys()].forEach(function visit(idstr, ancestors) { |
| 18 | const node = nodes.get(idstr), id = node.id |
| 19 | |
| 20 | if (visited.get(idstr)) return |
| 21 | if (!Array.isArray(ancestors)) ancestors = [] |
| 22 | |
| 23 | ancestors.push(id) |
| 24 | visited.set(idstr, true) |
| 25 | node.afters.forEach(function(afterID) { |
| 26 | if (ancestors.indexOf(afterID) >= 0) { |
| 27 | throw new Error("closed chain : " + afterID + " is in " + id) |
| 28 | } |
| 29 | visit( |
| 30 | afterID, |
| 31 | ancestors.map(function(v) { |
| 32 | return v |
| 33 | }) |
| 34 | ) |
| 35 | }) |
| 36 | sorted.unshift(id) |
| 37 | }) |
| 38 | |
| 39 | return sorted |
| 40 | } |
| 41 | |
| 42 | export const createEdges = <T extends { dependsOn?: readonly any[] | undefined }>(dep: readonly T[]) => { |
| 43 | const result = [] |