Topological sort by dependsOn.
(operations: PendingOperation[])
| 494 | |
| 495 | /** Topological sort by dependsOn. */ |
| 496 | private sortByDependencies(operations: PendingOperation[]): PendingOperation[] { |
| 497 | const result: PendingOperation[] = [] |
| 498 | const visited = new Set<string>() |
| 499 | |
| 500 | const visit = (op: PendingOperation) => { |
| 501 | if (visited.has(op.id)) return |
| 502 | visited.add(op.id) |
| 503 | |
| 504 | if (op.dependsOn) { |
| 505 | const dep = operations.find(d => d.id === op.dependsOn) |
| 506 | if (dep) visit(dep) |
| 507 | } |
| 508 | |
| 509 | result.push(op) |
| 510 | } |
| 511 | |
| 512 | for (const op of operations) { |
| 513 | visit(op) |
| 514 | } |
| 515 | |
| 516 | return result |
| 517 | } |
| 518 | |
| 519 | reset(): void { |
| 520 | this.state.connected = false |