Skip to content

Commit 86cd290

Browse files
author
Alexander Vakrilov
authored
feat(router): detach change detection on navigation (#1507)
* feat(router): detach change detection on navigation * test: add CD test when navigation * chore: update deps
1 parent 8284f94 commit 86cd290

File tree

11 files changed

+77
-17
lines changed

11 files changed

+77
-17
lines changed

Diff for: e2e/modal-navigation-ng/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"mocha-junit-reporter": "^1.18.0",
4545
"mocha-multi": "^1.0.1",
4646
"nativescript-dev-appium": "next",
47-
"nativescript-dev-typescript": "~0.7.8",
47+
"nativescript-dev-typescript": "next",
4848
"nativescript-dev-webpack": "next",
4949
"typescript": "~3.1.1"
5050
},

Diff for: e2e/nested-router-tab-view/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"mocha-junit-reporter": "^1.18.0",
4545
"mocha-multi": "^1.0.1",
4646
"nativescript-dev-appium": "next",
47-
"nativescript-dev-typescript": "~0.7.4",
47+
"nativescript-dev-typescript": "next",
4848
"nativescript-dev-webpack": "next",
4949
"typescript": "~3.1.1"
5050
},

Diff for: e2e/renderer/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"mocha-junit-reporter": "~1.17.0",
4040
"mocha-multi": "~1.0.0",
4141
"nativescript-dev-appium": "next",
42-
"nativescript-dev-typescript": "~0.7.1",
42+
"nativescript-dev-typescript": "next",
4343
"nativescript-dev-webpack": "next",
4444
"tslib": "^1.7.1",
4545
"typescript": "~3.1.1"

Diff for: e2e/router-tab-view/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"mocha-junit-reporter": "~1.17.0",
4242
"mocha-multi": "~1.0.0",
4343
"nativescript-dev-appium": "next",
44-
"nativescript-dev-typescript": "~0.6.0",
44+
"nativescript-dev-typescript": "next",
4545
"typescript": "~3.1.1"
4646
},
4747
"scripts": {

Diff for: e2e/router/app/first/first.component.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit, OnDestroy, OnChanges } from "@angular/core";
1+
import { Component, OnInit, OnDestroy, OnChanges, DoCheck } from "@angular/core";
22
import { ActivatedRoute, Router, Route } from "@angular/router";
33
import { Location } from "@angular/common";
44
import { RouterExtensions } from "nativescript-angular/router";
@@ -18,11 +18,14 @@ import { Page } from "tns-core-modules/ui/page";
1818
<Button text="GO TO C-LESS LAZY" automationText="GO TO C-LESS LAZY" [nsRouterLink]="['/lazy','nest','more']"></Button>
1919
2020
<Button text="BACK" automationText="BACK" (tap)="goBack()"></Button>
21+
<Button text="RESET" automationText="RESET" (tap)="reset()"></Button>
2122
<Label [text]="message"></Label>
23+
<Label [text]="'CHECK: ' + doCheckCount"></Label>
2224
</StackLayout>`
2325
})
24-
export class FirstComponent implements OnInit, OnDestroy {
26+
export class FirstComponent implements OnInit, OnDestroy, DoCheck {
2527
public message: string = "";
28+
public doCheckCount: number = 0;
2629
constructor(private routerExt: RouterExtensions, page: Page) {
2730
console.log("FirstComponent - constructor() page: " + page);
2831
}
@@ -35,6 +38,15 @@ export class FirstComponent implements OnInit, OnDestroy {
3538
console.log("FirstComponent - ngOnDestroy()");
3639
}
3740

41+
ngDoCheck() {
42+
this.doCheckCount++;
43+
console.log("FirstComponent - ngDoCheck(): " + this.doCheckCount);
44+
}
45+
46+
reset() {
47+
this.doCheckCount = 0;
48+
}
49+
3850
goBack() {
3951
this.message = "";
4052
if (this.routerExt.canGoBack()) {

Diff for: e2e/router/e2e/router.e2e-spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,37 @@ describe("Navigate to componentless lazy module route", () => {
406406
});
407407
});
408408

409+
describe("Simple navigate and back should trigger only one CD on FirstComponent", () => {
410+
let driver: AppiumDriver;
411+
412+
before(async () => {
413+
driver = await createDriver();
414+
await driver.resetApp();
415+
});
416+
417+
it("should find First", async () => {
418+
await assureFirstComponent(driver);
419+
});
420+
421+
it("should reset counter", async () => {
422+
await findAndClick(driver, "RESET");
423+
await driver.waitForElement("CHECK: 1");
424+
});
425+
426+
it("should navigate to Second(1)/master", async () => {
427+
await findAndClick(driver, "GO TO SECOND");
428+
429+
await assureSecondComponent(driver, 1);
430+
await assureNestedMasterComponent(driver);
431+
});
432+
433+
it("should navigate back to First", async () => {
434+
await goBack(driver);
435+
await assureFirstComponent(driver);
436+
await driver.waitForElement("CHECK: 2");
437+
});
438+
});
439+
409440
async function assureFirstComponent(driver: AppiumDriver) {
410441
await driver.findElementByAutomationText("FirstComponent");
411442
}

Diff for: e2e/router/package.json

+13-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"readme": "NativeScript Application",
55
"repository": "<fill-your-repository-here>",
66
"nativescript": {
7-
"id": "org.nativescript.router"
7+
"id": "org.nativescript.router",
8+
"tns-android": {
9+
"version": "5.1.0"
10+
}
811
},
912
"dependencies": {
1013
"@angular/animations": "~7.2.0",
@@ -24,10 +27,10 @@
2427
"zone.js": "^0.8.4"
2528
},
2629
"devDependencies": {
27-
"@ngtools/webpack": "~7.2.0",
2830
"@angular/compiler-cli": "~7.2.0",
29-
"@types/chai": "^4.0.2",
30-
"@types/mocha": "^5.2.5",
31+
"@ngtools/webpack": "~7.2.0",
32+
"@types/chai": "~4.1.7",
33+
"@types/mocha": "~5.2.5",
3134
"@types/node": "^10.12.12",
3235
"babel-traverse": "6.25.0",
3336
"babel-types": "6.25.0",
@@ -36,14 +39,14 @@
3639
"chai-as-promised": "~7.1.1",
3740
"colors": "^1.1.2",
3841
"lazy": "1.0.11",
39-
"mocha": "~3.5.0",
40-
"mocha-junit-reporter": "^1.13.0",
41-
"mocha-multi": "^0.11.0",
42-
"nativescript-dev-appium": "next",
43-
"nativescript-dev-typescript": "~0.4.0",
42+
"mocha": "~5.2.0",
43+
"mocha-junit-reporter": "~1.18.0",
44+
"mocha-multi": "~1.0.1",
45+
"nativescript-dev-appium": "^4.0.10-2019-01-16-01",
46+
"nativescript-dev-typescript": "^0.7.10-next-2019-01-07-183215-03",
4447
"nativescript-dev-webpack": "next",
4548
"tslib": "^1.7.1",
46-
"typescript": "~3.1.1"
49+
"typescript": "^3.1.6"
4750
},
4851
"scripts": {
4952
"e2e": "tsc -p e2e && mocha --opts ../config/mocha.opts --recursive e2e --appiumCapsLocation ../config/appium.capabilities.json",

Diff for: e2e/router/references.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference path="./node_modules/tns-core-modules/tns-core-modules.d.ts" /> Needed for autocompletion and compilation.

Diff for: e2e/router/tsconfig.tns.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"module": "es2015",
5+
"moduleResolution": "node"
6+
}
7+
}

Diff for: nativescript-angular/router/page-router-outlet.ts

+6
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
242242
log(`PageRouterOutlet.detach() - ${routeToString(this._activatedRoute)}`);
243243
}
244244

245+
// Detach from ChangeDetection
246+
this.activated.hostView.detach();
247+
245248
const component = this.activated;
246249
this.activated = null;
247250
this._activatedRoute = null;
@@ -257,6 +260,9 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
257260
}
258261

259262
this.activated = ref;
263+
264+
// reattach to ChangeDetection
265+
this.activated.hostView.reattach();
260266
this._activatedRoute = activatedRoute;
261267
this.markActivatedRoute(activatedRoute);
262268

Diff for: tests/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"karma-mocha-reporter": "2.2.5",
5252
"karma-nativescript-launcher": "0.4.0",
5353
"mocha": "5.2.0",
54-
"nativescript-dev-typescript": "~0.7.8",
54+
"nativescript-dev-typescript": "next",
5555
"babel-traverse": "6.8.0",
5656
"babel-types": "6.8.1",
5757
"babylon": "6.8.0",

0 commit comments

Comments
 (0)