@@ -373,29 +373,31 @@ export class SortablePrecedenceGraph<
373373 if ( pg == null || type == null ) return ;
374374
375375 const visited = new Set ( ) ;
376- const search = ( parentNode : T , nodes : Set < unknown > ) : void => {
377- for ( const node of nodes ) {
378- if ( node instanceof type ) {
379- this . addEdge ( node , parentNode ) ;
380- if ( ! visited . has ( node ) ) {
381- visited . add ( node ) ;
382- search ( node , pg . getUpstreamNeighbors ( node ) ) ;
376+ const startNodes = pg . getSourceNodes ( ) ;
377+
378+ const search = ( upstreamNode : T , downstreamNodes : Set < unknown > ) : void => {
379+ for ( const downstreamNode of downstreamNodes ) {
380+ if ( downstreamNode instanceof type ) {
381+ this . addEdge ( upstreamNode , downstreamNode ) ;
382+ if ( ! visited . has ( downstreamNode ) ) {
383+ visited . add ( downstreamNode ) ;
384+ search ( downstreamNode , pg . getDownstreamNeighbors ( downstreamNode ) ) ;
383385 }
384386 } else {
385- search ( parentNode , pg . getUpstreamNeighbors ( node ) ) ;
387+ // Look further downstream for neighbors that match the type.
388+ search ( upstreamNode , pg . getDownstreamNeighbors ( downstreamNode ) ) ;
386389 }
387390 }
388391 } ;
389- const leafs = pg . getSinkNodes ( ) ;
390- for ( const leaf of leafs ) {
391- if ( leaf instanceof type ) {
392- this . addNode ( leaf ) ;
393- search ( leaf , pg . getUpstreamNeighbors ( leaf ) ) ;
394- visited . clear ( ) ;
392+
393+ for ( const node of startNodes ) {
394+ if ( node instanceof type ) {
395+ this . addNode ( node ) ;
396+ search ( node , pg . getDownstreamNeighbors ( node ) ) ;
395397 } else {
396- // leaf is not a type. It's upstream neighbors become a new leafs .
397- for ( const newLeaf of pg . getUpstreamNeighbors ( leaf ) ) {
398- leafs . add ( newLeaf ) ;
398+ // Look further upstream for start nodes that match the type .
399+ for ( const newStartNode of pg . getDownstreamNeighbors ( node ) ) {
400+ startNodes . add ( newStartNode ) ;
399401 }
400402 }
401403 }
0 commit comments