* Visit every project in the graph that can be reached by the given entry project exactly once. * The entry project defaults to the root project. * In case a cycle is detected, an error is thrown * * @public * @param {string} [startName] Name of the project to start the traversal at. Defau
(startName, callback)
| 388 | * @param {@ui5/project/graph/ProjectGraph~traversalCallback} callback Will be called |
| 389 | */ |
| 390 | async traverseBreadthFirst(startName, callback) { |
| 391 | if (!callback) { |
| 392 | // Default optional first parameter |
| 393 | callback = startName; |
| 394 | startName = this._rootProjectName; |
| 395 | } |
| 396 | |
| 397 | if (!this.getProject(startName)) { |
| 398 | throw new Error(`Failed to start graph traversal: Could not find project ${startName} in project graph`); |
| 399 | } |
| 400 | |
| 401 | const queue = [{ |
| 402 | projectNames: [startName], |
| 403 | ancestors: [] |
| 404 | }]; |
| 405 | |
| 406 | const visited = Object.create(null); |
| 407 | |
| 408 | while (queue.length) { |
| 409 | const {projectNames, ancestors} = queue.shift(); // Get and remove first entry from queue |
| 410 | |
| 411 | await Promise.all(projectNames.map(async (projectName) => { |
| 412 | this._checkCycle(ancestors, projectName); |
| 413 | |
| 414 | if (visited[projectName]) { |
| 415 | return visited[projectName]; |
| 416 | } |
| 417 | |
| 418 | return visited[projectName] = (async () => { |
| 419 | const newAncestors = [...ancestors, projectName]; |
| 420 | const dependencies = this.getDependencies(projectName); |
| 421 | |
| 422 | queue.push({ |
| 423 | projectNames: dependencies, |
| 424 | ancestors: newAncestors |
| 425 | }); |
| 426 | |
| 427 | await callback({ |
| 428 | project: this.getProject(projectName), |
| 429 | dependencies |
| 430 | }); |
| 431 | })(); |
| 432 | })); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Visit every project in the graph that can be reached by the given entry project exactly once. |
no test coverage detected