Commit c768fcbac1ba1ca6fc26bec5617c4bf9ad07ee9f

Authored by Igor Kulikov
1 parent 634df646

Dashboard performance improvements by changing change detection strategy to OnPush

@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 /// 15 ///
16 16
17 import { 17 import {
  18 + ChangeDetectorRef,
18 Component, 19 Component,
19 Injector, 20 Injector,
20 Input, 21 Input,
@@ -73,6 +74,7 @@ export class AliasesEntitySelectComponent implements OnInit, OnDestroy { @@ -73,6 +74,7 @@ export class AliasesEntitySelectComponent implements OnInit, OnDestroy {
73 74
74 constructor(private translate: TranslateService, 75 constructor(private translate: TranslateService,
75 private overlay: Overlay, 76 private overlay: Overlay,
  77 + private cd: ChangeDetectorRef,
76 private breakpointObserver: BreakpointObserver, 78 private breakpointObserver: BreakpointObserver,
77 private viewContainerRef: ViewContainerRef) { 79 private viewContainerRef: ViewContainerRef) {
78 } 80 }
@@ -179,6 +181,7 @@ export class AliasesEntitySelectComponent implements OnInit, OnDestroy { @@ -179,6 +181,7 @@ export class AliasesEntitySelectComponent implements OnInit, OnDestroy {
179 displayValue = this.translate.instant('entity.entities'); 181 displayValue = this.translate.instant('entity.entities');
180 } 182 }
181 this.displayValue = displayValue; 183 this.displayValue = displayValue;
  184 + this.cd.detectChanges();
182 } 185 }
183 186
184 private updateEntityAliasesInfo() { 187 private updateEntityAliasesInfo() {
@@ -226,8 +226,6 @@ @@ -226,8 +226,6 @@
226 <mat-drawer class="tb-details-drawer" 226 <mat-drawer class="tb-details-drawer"
227 [opened]="isEditingWidget || isAddingWidget" 227 [opened]="isEditingWidget || isAddingWidget"
228 (openedStart)="detailsDrawerOpenedStart()" 228 (openedStart)="detailsDrawerOpenedStart()"
229 - (opened)="detailsDrawerOpened()"  
230 - (closedStart)="detailsDrawerClosedStart()"  
231 (closed)="detailsDrawerClosed()" 229 (closed)="detailsDrawerClosed()"
232 mode="over" 230 mode="over"
233 position="end"> 231 position="end">
@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 /// 15 ///
16 16
17 import { 17 import {
  18 + ChangeDetectionStrategy,
18 ChangeDetectorRef, 19 ChangeDetectorRef,
19 Component, ElementRef, 20 Component, ElementRef,
20 Inject, 21 Inject,
@@ -134,7 +135,7 @@ import { @@ -134,7 +135,7 @@ import {
134 templateUrl: './dashboard-page.component.html', 135 templateUrl: './dashboard-page.component.html',
135 styleUrls: ['./dashboard-page.component.scss'], 136 styleUrls: ['./dashboard-page.component.scss'],
136 encapsulation: ViewEncapsulation.None, 137 encapsulation: ViewEncapsulation.None,
137 - // changeDetection: ChangeDetectionStrategy.OnPush 138 + changeDetection: ChangeDetectionStrategy.OnPush
138 }) 139 })
139 export class DashboardPageComponent extends PageComponent implements IDashboardController, OnInit, OnDestroy { 140 export class DashboardPageComponent extends PageComponent implements IDashboardController, OnInit, OnDestroy {
140 141
@@ -425,9 +426,9 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC @@ -425,9 +426,9 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC
425 } 426 }
426 427
427 public runChangeDetection() { 428 public runChangeDetection() {
428 - /*setTimeout(() => { 429 + this.ngZone.run(() => {
429 this.cd.detectChanges(); 430 this.cd.detectChanges();
430 - });*/ 431 + });
431 } 432 }
432 433
433 public openToolbar() { 434 public openToolbar() {
@@ -731,6 +732,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC @@ -731,6 +732,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC
731 const widget = importData.widget; 732 const widget = importData.widget;
732 const layoutId = importData.layoutId; 733 const layoutId = importData.layoutId;
733 this.layouts[layoutId].layoutCtx.widgets.addWidgetId(widget.id); 734 this.layouts[layoutId].layoutCtx.widgets.addWidgetId(widget.id);
  735 + this.runChangeDetection();
734 } 736 }
735 } 737 }
736 ); 738 );
@@ -885,28 +887,17 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC @@ -885,28 +887,17 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC
885 } else if (this.isAddingWidget) { 887 } else if (this.isAddingWidget) {
886 this.isAddingWidgetClosed = false; 888 this.isAddingWidgetClosed = false;
887 } 889 }
888 - setTimeout(() => {  
889 - this.cd.detach();  
890 - }, 0);  
891 - }  
892 -  
893 - detailsDrawerOpened() {  
894 - this.cd.reattach();  
895 - }  
896 -  
897 - detailsDrawerClosedStart() {  
898 - this.cd.detach();  
899 } 890 }
900 891
901 detailsDrawerClosed() { 892 detailsDrawerClosed() {
902 this.isEditingWidgetClosed = true; 893 this.isEditingWidgetClosed = true;
903 this.isAddingWidgetClosed = true; 894 this.isAddingWidgetClosed = true;
904 - this.cd.reattach();  
905 } 895 }
906 896
907 private addWidgetToLayout(widget: Widget, layoutId: DashboardLayoutId) { 897 private addWidgetToLayout(widget: Widget, layoutId: DashboardLayoutId) {
908 this.dashboardUtils.addWidgetToLayout(this.dashboard, this.dashboardCtx.state, layoutId, widget); 898 this.dashboardUtils.addWidgetToLayout(this.dashboard, this.dashboardCtx.state, layoutId, widget);
909 this.layouts[layoutId].layoutCtx.widgets.addWidgetId(widget.id); 899 this.layouts[layoutId].layoutCtx.widgets.addWidgetId(widget.id);
  900 + this.runChangeDetection();
910 } 901 }
911 902
912 private selectTargetLayout(): Observable<DashboardLayoutId> { 903 private selectTargetLayout(): Observable<DashboardLayoutId> {
@@ -1060,6 +1051,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC @@ -1060,6 +1051,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC
1060 pos, this.entityAliasesUpdated.bind(this), this.filtersUpdated.bind(this)).subscribe( 1051 pos, this.entityAliasesUpdated.bind(this), this.filtersUpdated.bind(this)).subscribe(
1061 (widget) => { 1052 (widget) => {
1062 layoutCtx.widgets.addWidgetId(widget.id); 1053 layoutCtx.widgets.addWidgetId(widget.id);
  1054 + this.runChangeDetection();
1063 }); 1055 });
1064 } 1056 }
1065 1057
@@ -1068,6 +1060,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC @@ -1068,6 +1060,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC
1068 pos).subscribe( 1060 pos).subscribe(
1069 (widget) => { 1061 (widget) => {
1070 layoutCtx.widgets.addWidgetId(widget.id); 1062 layoutCtx.widgets.addWidgetId(widget.id);
  1063 + this.runChangeDetection();
1071 }); 1064 });
1072 } 1065 }
1073 1066
@@ -1086,6 +1079,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC @@ -1086,6 +1079,7 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC
1086 if (res) { 1079 if (res) {
1087 if (layoutCtx.widgets.removeWidgetId(widget.id)) { 1080 if (layoutCtx.widgets.removeWidgetId(widget.id)) {
1088 this.dashboardUtils.removeWidgetFromLayout(this.dashboard, this.dashboardCtx.state, layoutCtx.id, widget.id); 1081 this.dashboardUtils.removeWidgetFromLayout(this.dashboard, this.dashboardCtx.state, layoutCtx.id, widget.id);
  1082 + this.runChangeDetection();
1089 } 1083 }
1090 } 1084 }
1091 }); 1085 });
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 /// 15 ///
16 16
17 import { 17 import {
18 - AfterViewInit, 18 + AfterViewInit, ChangeDetectionStrategy, ChangeDetectorRef,
19 Component, 19 Component,
20 DoCheck, 20 DoCheck,
21 Input, 21 Input,
@@ -59,7 +59,8 @@ import { UtilsService } from '@core/services/utils.service'; @@ -59,7 +59,8 @@ import { UtilsService } from '@core/services/utils.service';
59 @Component({ 59 @Component({
60 selector: 'tb-dashboard', 60 selector: 'tb-dashboard',
61 templateUrl: './dashboard.component.html', 61 templateUrl: './dashboard.component.html',
62 - styleUrls: ['./dashboard.component.scss'] 62 + styleUrls: ['./dashboard.component.scss'],
  63 + changeDetection: ChangeDetectionStrategy.OnPush
63 }) 64 })
64 export class DashboardComponent extends PageComponent implements IDashboardComponent, DoCheck, OnInit, OnDestroy, AfterViewInit, OnChanges { 65 export class DashboardComponent extends PageComponent implements IDashboardComponent, DoCheck, OnInit, OnDestroy, AfterViewInit, OnChanges {
65 66
@@ -178,6 +179,7 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo @@ -178,6 +179,7 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
178 private breakpointObserver: BreakpointObserver, 179 private breakpointObserver: BreakpointObserver,
179 private differs: IterableDiffers, 180 private differs: IterableDiffers,
180 private kvDiffers: KeyValueDiffers, 181 private kvDiffers: KeyValueDiffers,
  182 + private cd: ChangeDetectorRef,
181 private ngZone: NgZone) { 183 private ngZone: NgZone) {
182 super(store); 184 super(store);
183 this.authUser = getCurrentAuthUser(store); 185 this.authUser = getCurrentAuthUser(store);
@@ -249,6 +251,7 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo @@ -249,6 +251,7 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
249 let updateLayoutOpts = false; 251 let updateLayoutOpts = false;
250 let updateEditingOpts = false; 252 let updateEditingOpts = false;
251 let updateWidgets = false; 253 let updateWidgets = false;
  254 + let updateDashboardTimewindow = false;
252 for (const propName of Object.keys(changes)) { 255 for (const propName of Object.keys(changes)) {
253 const change = changes[propName]; 256 const change = changes[propName];
254 if (!change.firstChange && change.currentValue !== change.previousValue) { 257 if (!change.firstChange && change.currentValue !== change.previousValue) {
@@ -261,13 +264,16 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo @@ -261,13 +264,16 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
261 } else if (['widgets', 'widgetLayouts'].includes(propName)) { 264 } else if (['widgets', 'widgetLayouts'].includes(propName)) {
262 updateWidgets = true; 265 updateWidgets = true;
263 } else if (propName === 'dashboardTimewindow') { 266 } else if (propName === 'dashboardTimewindow') {
264 - this.dashboardTimewindowChangedSubject.next(this.dashboardTimewindow); 267 + updateDashboardTimewindow = true;
265 } 268 }
266 } 269 }
267 } 270 }
268 if (updateWidgets) { 271 if (updateWidgets) {
269 this.updateWidgets(); 272 this.updateWidgets();
  273 + } else if (updateDashboardTimewindow) {
  274 + this.dashboardTimewindowChangedSubject.next(this.dashboardTimewindow);
270 } 275 }
  276 +
271 if (updateMobileOpts) { 277 if (updateMobileOpts) {
272 this.updateMobileOpts(); 278 this.updateMobileOpts();
273 } 279 }
@@ -528,6 +534,10 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo @@ -528,6 +534,10 @@ export class DashboardComponent extends PageComponent implements IDashboardCompo
528 this.updateWidgetLayouts(); 534 this.updateWidgetLayouts();
529 } 535 }
530 536
  537 + public detectChanges() {
  538 + this.cd.detectChanges();
  539 + }
  540 +
531 private detectRowSize(isMobile: boolean, autofillHeight: boolean, parentHeight?: number): number | null { 541 private detectRowSize(isMobile: boolean, autofillHeight: boolean, parentHeight?: number): number | null {
532 let rowHeight = null; 542 let rowHeight = null;
533 if (!autofillHeight) { 543 if (!autofillHeight) {
@@ -468,10 +468,13 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI @@ -468,10 +468,13 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
468 ); 468 );
469 } 469 }
470 470
471 - private detectChanges() { 471 + private detectChanges(detectDashboardChanges = false) {
472 if (!this.destroyed) { 472 if (!this.destroyed) {
473 try { 473 try {
474 this.cd.detectChanges(); 474 this.cd.detectChanges();
  475 + if (detectDashboardChanges) {
  476 + this.widgetContext.dashboard.detectChanges();
  477 + }
475 } catch (e) { 478 } catch (e) {
476 // console.log(e); 479 // console.log(e);
477 } 480 }
@@ -491,6 +494,7 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI @@ -491,6 +494,7 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
491 } 494 }
492 if (!this.widgetContext.inited && this.isReady()) { 495 if (!this.widgetContext.inited && this.isReady()) {
493 this.widgetContext.inited = true; 496 this.widgetContext.inited = true;
  497 + this.widgetContext.dashboard.detectChanges();
494 if (this.cafs.init) { 498 if (this.cafs.init) {
495 this.cafs.init(); 499 this.cafs.init();
496 this.cafs.init = null; 500 this.cafs.init = null;
@@ -878,7 +882,7 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI @@ -878,7 +882,7 @@ export class WidgetComponent extends PageComponent implements OnInit, AfterViewI
878 timeWindowUpdated: (subscription, timeWindowConfig) => { 882 timeWindowUpdated: (subscription, timeWindowConfig) => {
879 this.ngZone.run(() => { 883 this.ngZone.run(() => {
880 this.widget.config.timewindow = timeWindowConfig; 884 this.widget.config.timewindow = timeWindowConfig;
881 - this.detectChanges(); 885 + this.detectChanges(true);
882 }); 886 });
883 } 887 }
884 }; 888 };
@@ -79,6 +79,7 @@ export interface IDashboardComponent { @@ -79,6 +79,7 @@ export interface IDashboardComponent {
79 pauseChangeNotifications(); 79 pauseChangeNotifications();
80 resumeChangeNotifications(); 80 resumeChangeNotifications();
81 notifyLayoutUpdated(); 81 notifyLayoutUpdated();
  82 + detectChanges();
82 } 83 }
83 84
84 declare type DashboardWidgetUpdateOperation = 'add' | 'remove' | 'update'; 85 declare type DashboardWidgetUpdateOperation = 'add' | 'remove' | 'update';
@@ -342,7 +343,7 @@ export class DashboardWidget implements GridsterItem, IDashboardWidget { @@ -342,7 +343,7 @@ export class DashboardWidget implements GridsterItem, IDashboardWidget {
342 widget.id = guid(); 343 widget.id = guid();
343 } 344 }
344 this.widgetId = widget.id; 345 this.widgetId = widget.id;
345 - this.updateWidgetParams(); 346 + this.updateWidgetParams(false);
346 } 347 }
347 348
348 gridsterItemComponent$(): Observable<GridsterItemComponentInterface> { 349 gridsterItemComponent$(): Observable<GridsterItemComponentInterface> {
@@ -353,7 +354,7 @@ export class DashboardWidget implements GridsterItem, IDashboardWidget { @@ -353,7 +354,7 @@ export class DashboardWidget implements GridsterItem, IDashboardWidget {
353 } 354 }
354 } 355 }
355 356
356 - updateWidgetParams() { 357 + updateWidgetParams(detectChanges = true) {
357 this.color = this.widget.config.color || 'rgba(0, 0, 0, 0.87)'; 358 this.color = this.widget.config.color || 'rgba(0, 0, 0, 0.87)';
358 this.backgroundColor = this.widget.config.backgroundColor || '#fff'; 359 this.backgroundColor = this.widget.config.backgroundColor || '#fff';
359 this.padding = this.widget.config.padding || '8px'; 360 this.padding = this.widget.config.padding || '8px';
@@ -405,6 +406,9 @@ export class DashboardWidget implements GridsterItem, IDashboardWidget { @@ -405,6 +406,9 @@ export class DashboardWidget implements GridsterItem, IDashboardWidget {
405 406
406 this.customHeaderActions = this.widgetContext.customHeaderActions ? this.widgetContext.customHeaderActions : []; 407 this.customHeaderActions = this.widgetContext.customHeaderActions ? this.widgetContext.customHeaderActions : [];
407 this.widgetActions = this.widgetContext.widgetActions ? this.widgetContext.widgetActions : []; 408 this.widgetActions = this.widgetContext.widgetActions ? this.widgetContext.widgetActions : [];
  409 + if (detectChanges) {
  410 + this.dashboard.detectChanges();
  411 + }
408 } 412 }
409 413
410 @enumerable(true) 414 @enumerable(true)
@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 /// 15 ///
16 16
17 import { 17 import {
  18 + ChangeDetectorRef,
18 Component, 19 Component,
19 forwardRef, 20 forwardRef,
20 Inject, 21 Inject,
@@ -158,6 +159,7 @@ export class TimewindowComponent implements OnInit, OnDestroy, ControlValueAcces @@ -158,6 +159,7 @@ export class TimewindowComponent implements OnInit, OnDestroy, ControlValueAcces
158 private millisecondsToTimeStringPipe: MillisecondsToTimeStringPipe, 159 private millisecondsToTimeStringPipe: MillisecondsToTimeStringPipe,
159 private datePipe: DatePipe, 160 private datePipe: DatePipe,
160 private overlay: Overlay, 161 private overlay: Overlay,
  162 + private cd: ChangeDetectorRef,
161 public viewContainerRef: ViewContainerRef, 163 public viewContainerRef: ViewContainerRef,
162 public breakpointObserver: BreakpointObserver, 164 public breakpointObserver: BreakpointObserver,
163 @Inject(DOCUMENT) private document: Document, 165 @Inject(DOCUMENT) private document: Document,
@@ -313,6 +315,7 @@ export class TimewindowComponent implements OnInit, OnDestroy, ControlValueAcces @@ -313,6 +315,7 @@ export class TimewindowComponent implements OnInit, OnDestroy, ControlValueAcces
313 } else { 315 } else {
314 this.innerValue.displayTimezoneAbbr = ''; 316 this.innerValue.displayTimezoneAbbr = '';
315 } 317 }
  318 + this.cd.detectChanges();
316 } 319 }
317 320
318 hideLabel() { 321 hideLabel() {