Skip to content

Commit 430bb54

Browse files
vik-13mgechev
authored andcommitted
refactor: use onPush strategy
1 parent 2b66c9d commit 430bb54

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

src/app/app.component.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { Component, NgZone, ViewChild, AfterViewInit, OnDestroy, ChangeDetectorRef } from '@angular/core';
1+
import {
2+
ChangeDetectionStrategy,
3+
ChangeDetectorRef,
4+
Component,
5+
NgZone,
6+
OnDestroy,
7+
ViewChild
8+
} from '@angular/core';
29
import { ProjectProxy } from './model/project-proxy';
310
import { Config, IdentifiedStaticSymbol, VisualizationConfig } from '../shared/data-format';
411
import { formatError } from './shared/utils';
5-
import { StateManager, Memento } from './model/state-manager';
12+
import { Memento, StateManager } from './model/state-manager';
613
import { Theme } from '../shared/themes/color-map';
714
import { IPCBus } from './model/ipc-bus';
815
import { Message } from '../shared/ipc-constants';
@@ -17,9 +24,10 @@ import { KeyValue } from '@angular/common';
1724
@Component({
1825
selector: 'ngrev-app',
1926
templateUrl: './app.component.html',
20-
styleUrls: ['./app.component.scss']
27+
styleUrls: ['./app.component.scss'],
28+
changeDetection: ChangeDetectionStrategy.OnPush
2129
})
22-
export class AppComponent implements AfterViewInit, OnDestroy {
30+
export class AppComponent implements OnDestroy {
2331
projectSet = false;
2432
loading = false;
2533
queryList: KeyValue<string, IdentifiedStaticSymbol>[] = [];
@@ -35,9 +43,11 @@ export class AppComponent implements AfterViewInit, OnDestroy {
3543

3644
private _stopLoading = () => {
3745
this.loading = false;
46+
this._cd.markForCheck();
3847
};
3948
private _startLoading = () => {
4049
this.loading = true;
50+
this._cd.markForCheck();
4151
};
4252

4353
private _keyDownSubscription: Subscription;
@@ -55,6 +65,30 @@ export class AppComponent implements AfterViewInit, OnDestroy {
5565
this.theme = config.themes[config.theme];
5666
this.showLibs = config.showLibs;
5767
this.showModules = config.showModules;
68+
this._cd.markForCheck();
69+
});
70+
71+
this._ipcBus.on(Message.ChangeTheme, (_: any, theme: string) => {
72+
this._ngZone.run(() => {
73+
this.theme = this.themes[theme];
74+
this._cd.markForCheck();
75+
});
76+
});
77+
this._ipcBus.on(Message.ToggleLibsMenuAction, () => {
78+
this._ngZone.run(() => {
79+
this.manager.toggleLibs().then(() => {
80+
this.manager.reloadAppState();
81+
this._cd.markForCheck();
82+
});
83+
});
84+
});
85+
this._ipcBus.on(Message.ToggleModulesMenuAction, () => {
86+
this._ngZone.run(() => {
87+
this.manager.toggleModules().then(() => {
88+
this.manager.reloadAppState();
89+
this._cd.markForCheck();
90+
});
91+
});
5892
});
5993

6094
this.maxWidth$ = fromEvent(window, 'resize').pipe(
@@ -73,25 +107,6 @@ export class AppComponent implements AfterViewInit, OnDestroy {
73107
).subscribe();
74108
}
75109

76-
ngAfterViewInit(): void {
77-
this._ipcBus.on(Message.ChangeTheme, (_: any, theme: string) => {
78-
this.theme = this.themes[theme];
79-
this._cd.detectChanges();
80-
});
81-
this._ipcBus.on(Message.ToggleLibsMenuAction, () => {
82-
this.manager.toggleLibs().then(() => {
83-
this.manager.reloadAppState();
84-
this._cd.detectChanges();
85-
});
86-
});
87-
this._ipcBus.on(Message.ToggleModulesMenuAction, () => {
88-
this.manager.toggleModules().then(() => {
89-
this.manager.reloadAppState();
90-
this._cd.detectChanges();
91-
});
92-
});
93-
}
94-
95110
ngOnDestroy(): void {
96111
this._keyDownSubscription.unsubscribe();
97112
}
@@ -137,15 +152,17 @@ the Angular's AoT compiler. Error during parsing:\n\n${formatError(error)}`
137152
}
138153

139154
restoreMemento(memento: Memento): void {
140-
this.manager
141-
.restoreMemento(memento)
142-
.then(this._stopLoading)
143-
.catch(this._stopLoading);
155+
this._ngZone.run(() => {
156+
this.manager
157+
.restoreMemento(memento)
158+
.then(this._stopLoading)
159+
.catch(this._stopLoading);
160+
});
144161
}
145162

146163
get initialized(): VisualizationConfig<any> | null {
147164
return this.manager.getCurrentState(() => {
148-
this._cd.detectChanges();
165+
this._cd.markForCheck();
149166
});
150167
}
151168

src/app/components/state-navigation/state-navigation.component.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Component, Input, Output, EventEmitter, ChangeDetectorRef } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
22
import { VisualizationConfig } from '../../../shared/data-format';
3-
import { Theme, DefaultColor, BoxTheme } from '../../../shared/themes/color-map';
3+
import { BoxTheme, DefaultColor, Theme } from '../../../shared/themes/color-map';
44
import { Memento } from '../../model/state-manager';
55
import { coerceNumberProperty } from '@angular/cdk/coercion';
66

@@ -19,7 +19,8 @@ const MetaMemento = new Memento(dummyConfig);
1919
@Component({
2020
selector: 'ngrev-state-navigation',
2121
templateUrl: './state-navigation.component.html',
22-
styleUrls: ['./state-navigation.component.scss']
22+
styleUrls: ['./state-navigation.component.scss'],
23+
changeDetection: ChangeDetectionStrategy.OnPush
2324
})
2425
export class StateNavigationComponent {
2526
@Input() states: Memento[] = [];
@@ -37,16 +38,12 @@ export class StateNavigationComponent {
3738

3839
visibleTooltip = -1;
3940

40-
constructor(private _changeDetectorRef: ChangeDetectorRef) {}
41-
4241
showTooltip(idx: number) {
4342
this.visibleTooltip = idx;
44-
this._changeDetectorRef.detectChanges();
4543
}
4644

4745
hideTooltip(idx: number) {
4846
this.visibleTooltip = -1;
49-
this._changeDetectorRef.detectChanges();
5047
}
5148

5249
changeState(state: Memento) {

src/app/model/state-manager.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ export class StateManager {
2828
loadProject(tsconfig: string, showLibs: boolean, showModules: boolean) {
2929
return this.project
3030
.load(tsconfig, showLibs, showModules)
31-
// TODO: StateProxy is gonna be created everytime the same as IPCBus. Maybe it would be great to use only one,
32-
// provided by angular service, just reset the state after loading new project.
3331
.then(() => (this.state = new StateProxy()))
3432
.then((proxy: StateProxy) => proxy.getData())
35-
.then(data => this.history.push(new Memento(data)));
33+
.then((data: VisualizationConfig<any>) => (this.history = [...this.history, new Memento(data)]));
3634
}
3735

3836
toggleLibs() {
@@ -91,7 +89,8 @@ export class StateManager {
9189
if (last.dirty) {
9290
last.dirty = false;
9391
this.state!.reload().then(state => {
94-
this.history[this.history.length - 1] = new Memento(state);
92+
this.history.pop();
93+
this.history = [...this.history, new Memento(state)];
9594
refreshOnReady && refreshOnReady();
9695
return state;
9796
});
@@ -112,7 +111,7 @@ export class StateManager {
112111
// eslint-disable-next-line @typescript-eslint/no-misused-promises
113112
return new Promise(async (resolve, reject) => {
114113
try {
115-
while (true && this.history.length) {
114+
while (this.history.length) {
116115
const last = this.history[this.history.length - 1];
117116
if (last === memento) {
118117
break;
@@ -128,10 +127,13 @@ export class StateManager {
128127
}
129128

130129
private pushState(data: VisualizationConfig<any>) {
131-
this.history.push(new Memento(data));
130+
this.history = [...this.history, new Memento(data)];
132131
}
133132

134133
private popState() {
135-
return this.state!.prevState().then(() => this.history.pop());
134+
return this.state!.prevState().then(() => {
135+
this.history.pop();
136+
this.history = [...this.history];
137+
});
136138
}
137139
}

src/electron/menu/application_menu_template.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const applicationMenuTemplate = (
2626
submenu: [
2727
{
2828
label: "Themes",
29-
submenu: Object.keys(getConfig().themes || []).map((label) => {
29+
submenu: Object.keys(getConfig().themes || []).map((label: string) => {
3030
return {
3131
label,
3232
type: "radio",

0 commit comments

Comments
 (0)