@@ -716,10 +716,7 @@ export class CdkTree<T, K = T>
716716 if ( this . treeControl ) {
717717 this . treeControl . expandAll ( ) ;
718718 } else if ( this . _expansionModel ) {
719- const expansionModel = this . _expansionModel ;
720- expansionModel . select (
721- ...this . _flattenedNodes . value . map ( child => this . _getExpansionKey ( child ) ) ,
722- ) ;
719+ this . _forEachExpansionKey ( keys => this . _expansionModel ?. select ( ...keys ) ) ;
723720 }
724721 }
725722
@@ -728,10 +725,7 @@ export class CdkTree<T, K = T>
728725 if ( this . treeControl ) {
729726 this . treeControl . collapseAll ( ) ;
730727 } else if ( this . _expansionModel ) {
731- const expansionModel = this . _expansionModel ;
732- expansionModel . deselect (
733- ...this . _flattenedNodes . value . map ( child => this . _getExpansionKey ( child ) ) ,
734- ) ;
728+ this . _forEachExpansionKey ( keys => this . _expansionModel ?. deselect ( ...keys ) ) ;
735729 }
736730 }
737731
@@ -776,13 +770,7 @@ export class CdkTree<T, K = T>
776770 if ( ! expanded ) {
777771 return [ ] ;
778772 }
779- return this . _findChildrenByLevel (
780- levelAccessor ,
781- flattenedNodes ,
782-
783- dataNode ,
784- 1 ,
785- ) ;
773+ return this . _findChildrenByLevel ( levelAccessor , flattenedNodes , dataNode , 1 ) ;
786774 } ) ,
787775 ) ;
788776 }
@@ -1153,6 +1141,28 @@ export class CdkTree<T, K = T>
11531141 this . _ariaSets . set ( parentKey , group ) ;
11541142 }
11551143 }
1144+
1145+ /** Invokes a callback with all node expansion keys. */
1146+ private _forEachExpansionKey ( callback : ( keys : K [ ] ) => void ) {
1147+ const toToggle : K [ ] = [ ] ;
1148+ const observables : Observable < T [ ] > [ ] = [ ] ;
1149+
1150+ this . _nodes . value . forEach ( node => {
1151+ toToggle . push ( this . _getExpansionKey ( node . data ) ) ;
1152+ observables . push ( this . _getDescendants ( node . data ) ) ;
1153+ } ) ;
1154+
1155+ if ( observables . length > 0 ) {
1156+ combineLatest ( observables )
1157+ . pipe ( take ( 1 ) , takeUntil ( this . _onDestroy ) )
1158+ . subscribe ( results => {
1159+ results . forEach ( inner => inner . forEach ( r => toToggle . push ( this . _getExpansionKey ( r ) ) ) ) ;
1160+ callback ( toToggle ) ;
1161+ } ) ;
1162+ } else {
1163+ callback ( toToggle ) ;
1164+ }
1165+ }
11561166}
11571167
11581168/**
0 commit comments