Commit c768fcbac1ba1ca6fc26bec5617c4bf9ad07ee9f

Authored by Igor Kulikov
1 parent 634df646

Dashboard performance improvements by changing change detection strategy to OnPush

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