Skip to content

Commit f613b3b

Browse files
committed
perf(angular): detach fromn change detection
1 parent 2c41823 commit f613b3b

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

angular/src/directives/navigation/stack-controller.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ export class StackController {
4343

4444
getExistingView(activatedRoute: ActivatedRoute): RouteView | undefined {
4545
const activatedUrlKey = getUrl(this.router, activatedRoute);
46-
return this.views.find(vw => vw.url === activatedUrlKey);
46+
const view = this.views.find(vw => vw.url === activatedUrlKey);
47+
if (view) {
48+
view.ref.changeDetectorRef.reattach();
49+
}
50+
return view;
4751
}
4852

4953
async setActive(enteringView: RouteView) {
@@ -55,30 +59,35 @@ export class StackController {
5559
}
5660
this.insertView(enteringView, direction);
5761
await this.transition(enteringView, leavingView, animation, this.canGoBack(1), false);
58-
this.cleanup();
62+
requestAnimationFrame(() => this.cleanup());
5963
}
6064

6165
canGoBack(deep: number, stackId = this.getActiveStackId()): boolean {
6266
return this.getStack(stackId).length > deep;
6367
}
6468

6569
pop(deep: number, stackId = this.getActiveStackId()) {
66-
this.zone.run(() => {
70+
return this.zone.run(() => {
6771
const views = this.getStack(stackId);
6872
const view = views[views.length - deep - 1];
69-
this.navCtrl.navigateBack(view.url);
73+
return this.navCtrl.navigateBack(view.url);
7074
});
7175
}
7276

73-
startBackTransition(stackId = this.getActiveStackId()) {
74-
const views = this.getStack(stackId);
75-
this.transition(
76-
views[views.length - 2], // entering view
77-
views[views.length - 1], // leaving view
78-
'back',
79-
true,
80-
true
81-
);
77+
startBackTransition() {
78+
const leavingView = this.activeView;
79+
if (leavingView) {
80+
const views = this.getStack(leavingView.stackId);
81+
const enteringView = views[views.length - 2];
82+
enteringView.ref.changeDetectorRef.reattach();
83+
this.transition(
84+
enteringView, // entering view
85+
leavingView, // leaving view
86+
'back',
87+
true,
88+
true
89+
);
90+
}
8291
}
8392

8493
endBackTransition(shouldComplete: boolean) {
@@ -125,6 +134,7 @@ export class StackController {
125134
const element = view.element;
126135
element.setAttribute('aria-hidden', 'true');
127136
element.classList.add('ion-page-hidden');
137+
view.ref.changeDetectorRef.detach();
128138
}
129139
});
130140
this.viewsSnapshot = views.slice();
@@ -145,11 +155,6 @@ export class StackController {
145155
this.skipTransition = false;
146156
return;
147157
}
148-
// TODO
149-
// if (enteringView) {
150-
// enteringView.ref.changeDetectorRef.reattach();
151-
// enteringView.ref.changeDetectorRef.markForCheck();
152-
// }
153158
const enteringEl = enteringView ? enteringView.element : undefined;
154159
const leavingEl = leavingView ? leavingView.element : undefined;
155160
const containerEl = this.containerEl;

angular/src/providers/angular-delegate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const LIFECYCLES = [
110110
LIFECYCLE_DID_ENTER,
111111
LIFECYCLE_WILL_LEAVE,
112112
LIFECYCLE_DID_LEAVE,
113+
LIFECYCLE_WILL_UNLOAD
113114
];
114115

115116
export function bindLifecycleEvents(instance: any, element: HTMLElement) {

angular/test/test-app/e2e/src/router-link.e2e-spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ describe('router-link', () => {
2525
it('should go forward with ion-button[routerLink]', async () => {
2626
await element(by.css('#routerLink')).click();
2727
await testForward();
28+
29+
// test go back
30+
await element(by.css('ion-back-button')).click();
31+
await waitTime(500);
32+
33+
await testStack('ion-router-outlet', ['app-router-link']);
34+
await testLifeCycle('app-router-link', {
35+
ionViewWillEnter: 2,
36+
ionViewDidEnter: 2,
37+
ionViewWillLeave: 1,
38+
ionViewDidLeave: 1,
39+
});
2840
});
2941

3042
it('should go forward with a[routerLink]', async () => {
@@ -95,6 +107,7 @@ async function testForward() {
95107
ionViewWillLeave: 0,
96108
ionViewDidLeave: 0,
97109
});
110+
98111
}
99112

100113
async function testRoot() {

0 commit comments

Comments
 (0)