@@ -80,23 +80,41 @@ export default class Dashboard extends React.PureComponent<Props, State> {
80
80
}
81
81
}
82
82
83
- /** We have received an update from `this.props.initWatcher` */
83
+ /** Helps with debouncing updates of WorkersUpdate */
84
+ private updateDebounce1 : ( null | ReturnType < typeof setTimeout > ) [ ] = [ ]
85
+
86
+ /** Helps with debouncing updates of LogLineUpdate */
87
+ private updateDebounce2 : ( null | ReturnType < typeof setTimeout > ) [ ] = [ ]
88
+
89
+ /** We have received an update from a `GridSpec.initWatcher` */
84
90
private readonly onUpdate = ( gridIdx : number , model : UpdatePayload ) => {
85
- this . setState ( ( curState ) => ( {
86
- firstUpdate : ( curState && curState . firstUpdate ) || Date . now ( ) , // TODO pull from the events
87
- lastUpdate : Date . now ( ) , // TODO pull from the events
88
- events : ! isWorkersUpdate ( model )
89
- ? curState ?. events
90
- : ! model . events || model . events . length === 0
91
- ? curState ?. events
92
- : model . events ,
93
- logLine : ! isWorkersUpdate ( model ) ? model . logLine : curState ?. logLine ,
94
- workers : ! isWorkersUpdate ( model )
95
- ? curState ?. workers
96
- : ! curState ?. workers
97
- ? [ model . workers ]
98
- : [ ...curState . workers . slice ( 0 , gridIdx ) , model . workers , ...curState . workers . slice ( gridIdx + 1 ) ] ,
99
- } ) )
91
+ // to avoid a flurry of renders, we do some debouncing here
92
+ const bouncies = isWorkersUpdate ( model ) ? this . updateDebounce1 : this . updateDebounce2
93
+ const bouncey = bouncies [ gridIdx ]
94
+ if ( bouncey !== null ) clearTimeout ( bouncey )
95
+
96
+ bouncies [ gridIdx ] = setTimeout (
97
+ ( ) =>
98
+ this . setState ( ( curState ) => ( {
99
+ lastUpdate : Date . now ( ) , // TODO pull from the events
100
+ firstUpdate : ( curState && curState . firstUpdate ) || Date . now ( ) , // TODO pull from the events
101
+
102
+ logLine : ! isWorkersUpdate ( model ) ? model . logLine : curState ?. logLine ,
103
+
104
+ events : ! isWorkersUpdate ( model )
105
+ ? curState ?. events
106
+ : ! model . events || model . events . length === 0
107
+ ? curState ?. events
108
+ : model . events ,
109
+
110
+ workers : ! isWorkersUpdate ( model )
111
+ ? curState ?. workers
112
+ : ! curState ?. workers
113
+ ? [ model . workers ]
114
+ : [ ...curState . workers . slice ( 0 , gridIdx ) , model . workers , ...curState . workers . slice ( gridIdx + 1 ) ] ,
115
+ } ) ) ,
116
+ 0
117
+ )
100
118
}
101
119
102
120
/** @return the grid models, excluding the `null` linebreak indicators */
0 commit comments