@@ -57,6 +57,8 @@ const LOCAL_STORAGE_COLLAPSE_ROOTS_BY_DEFAULT_KEY =
5757const LOCAL_STORAGE_RECORD_CHANGE_DESCRIPTIONS_KEY =
5858 'React::DevTools::recordChangeDescriptions' ;
5959
60+ type ErrorAndWarningTuples = Array < { | id : number , index : number | } > ;
61+
6062type Config = { |
6163 checkBridgeProtocolCompatibility ? : boolean ,
6264 isProfiling ? : boolean ,
@@ -94,7 +96,7 @@ export default class Store extends EventEmitter<{|
9496 // Computed whenever _errorsAndWarnings Map changes.
9597 _cachedErrorCount : number = 0 ;
9698 _cachedWarningCount: number = 0 ;
97- _cachedErrorAndWarningTuples: Array < { | id : number , index : number | } > = [ ] ;
99+ _cachedErrorAndWarningTuples: ErrorAndWarningTuples | null = null ;
98100
99101 // Should new nodes be collapsed by default when added to the tree?
100102 _collapseNodesByDefault: boolean = true ;
@@ -510,7 +512,34 @@ export default class Store extends EventEmitter<{|
510512
511513 // Returns a tuple of [id, index]
512514 getElementsWithErrorsAndWarnings ( ) : Array < { | id : number , index : number | } > {
513- return this . _cachedErrorAndWarningTuples ;
515+ if ( this . _cachedErrorAndWarningTuples !== null ) {
516+ return this . _cachedErrorAndWarningTuples ;
517+ } else {
518+ const errorAndWarningTuples : ErrorAndWarningTuples = [ ] ;
519+
520+ this . _errorsAndWarnings . forEach ( ( _ , id ) => {
521+ const index = this . getIndexOfElementID ( id ) ;
522+ if ( index !== null ) {
523+ let low = 0 ;
524+ let high = errorAndWarningTuples . length ;
525+ while ( low < high ) {
526+ const mid = ( low + high ) >> 1 ;
527+ if ( errorAndWarningTuples [ mid ] . index > index ) {
528+ high = mid ;
529+ } else {
530+ low = mid + 1 ;
531+ }
532+ }
533+
534+ errorAndWarningTuples . splice ( low , 0 , { id, index} ) ;
535+ }
536+ } ) ;
537+
538+ // Cache for later (at least until the tree changes again).
539+ this . _cachedErrorAndWarningTuples = errorAndWarningTuples ;
540+
541+ return errorAndWarningTuples ;
542+ }
514543 }
515544
516545 getErrorAndWarningCountForElementID (
@@ -1124,6 +1153,9 @@ export default class Store extends EventEmitter<{|
11241153
11251154 this . _revision ++ ;
11261155
1156+ // Any time the tree changes (e.g. elements added, removed, or reordered) cached inidices may be invalid.
1157+ this . _cachedErrorAndWarningTuples = null ;
1158+
11271159 if ( haveErrorsOrWarningsChanged ) {
11281160 let errorCount = 0 ;
11291161 let warningCount = 0 ;
@@ -1135,28 +1167,6 @@ export default class Store extends EventEmitter<{|
11351167
11361168 this . _cachedErrorCount = errorCount ;
11371169 this . _cachedWarningCount = warningCount ;
1138-
1139- const errorAndWarningTuples : Array < { | id : number , index : number | } > = [ ] ;
1140-
1141- this . _errorsAndWarnings . forEach ( ( _ , id ) => {
1142- const index = this . getIndexOfElementID ( id ) ;
1143- if ( index !== null ) {
1144- let low = 0 ;
1145- let high = errorAndWarningTuples . length ;
1146- while ( low < high ) {
1147- const mid = ( low + high ) >> 1 ;
1148- if ( errorAndWarningTuples [ mid ] . index > index ) {
1149- high = mid ;
1150- } else {
1151- low = mid + 1 ;
1152- }
1153- }
1154-
1155- errorAndWarningTuples . splice ( low , 0 , { id, index} ) ;
1156- }
1157- } ) ;
1158-
1159- this . _cachedErrorAndWarningTuples = errorAndWarningTuples ;
11601170 }
11611171
11621172 if ( haveRootsChanged ) {
@@ -1187,18 +1197,6 @@ export default class Store extends EventEmitter<{|
11871197 console . groupEnd ( ) ;
11881198 }
11891199
1190- const indicesOfCachedErrorsOrWarningsAreStale =
1191- ! haveErrorsOrWarningsChanged &&
1192- ( addedElementIDs . length > 0 || removedElementIDs . size > 0 ) ;
1193- if ( indicesOfCachedErrorsOrWarningsAreStale ) {
1194- this . _cachedErrorAndWarningTuples . forEach ( entry => {
1195- const index = this . getIndexOfElementID ( entry . id ) ;
1196- if ( index !== null ) {
1197- entry . index = index ;
1198- }
1199- } ) ;
1200- }
1201-
12021200 this . emit ( 'mutated' , [ addedElementIDs , removedElementIDs ] ) ;
12031201 } ;
12041202
0 commit comments