Skip to content

Commit 82747df

Browse files
NathanWalkerAlexander Vakrilov
authored and
Alexander Vakrilov
committed
fix(router): avoiding throw for app stability improvements (NativeScript#1344)
1 parent 26ad2d6 commit 82747df

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

nativescript-angular/router/ns-location-strategy.ts

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Injectable } from "@angular/core";
22
import { LocationStrategy } from "@angular/common";
33
import { DefaultUrlSerializer, UrlSegmentGroup, UrlTree } from "@angular/router";
4-
import { routerLog } from "../trace";
4+
import { routerLog, routerError } from "../trace";
55
import { NavigationTransition, Frame } from "tns-core-modules/ui/frame";
66
import { isPresent } from "../lang-facade";
77
import { FrameService } from "../platform-providers";
@@ -286,10 +286,11 @@ export class NSLocationStrategy extends LocationStrategy {
286286

287287
// Methods for syncing with page navigation in PageRouterOutlet
288288
public _beginBackPageNavigation(name: string, frame: Frame) {
289-
routerLog("NSLocationStrategy.startGoBack()");
290289
if (this._isPageNavigationBack) {
291-
throw new Error("Calling startGoBack while going back.");
290+
routerError("Attempted to call startGoBack while going back.");
291+
return;
292292
}
293+
routerLog("NSLocationStrategy.startGoBack()");
293294
this._isPageNavigationBack = true;
294295

295296
let { cachedFrame } = this.frameService.findFrame(frame);
@@ -302,10 +303,11 @@ export class NSLocationStrategy extends LocationStrategy {
302303
}
303304

304305
public _finishBackPageNavigation() {
305-
routerLog("NSLocationStrategy.finishBackPageNavigation()");
306306
if (!this._isPageNavigationBack) {
307-
throw new Error("Calling endGoBack while not going back.");
307+
routerError("Attempted to call endGoBack while not going back.");
308+
return;
308309
}
310+
routerLog("NSLocationStrategy.finishBackPageNavigation()");
309311
this._isPageNavigationBack = false;
310312
}
311313

@@ -314,33 +316,35 @@ export class NSLocationStrategy extends LocationStrategy {
314316
}
315317

316318
public _beginModalNavigation(frame: Frame): void {
317-
routerLog("NSLocationStrategy._beginModalNavigation()");
319+
routerLog("NSLocationStrategy._beginModalNavigation()");
318320

319-
let { cachedFrameRootOutlet } = this.frameService.findFrame(frame);
321+
let { cachedFrameRootOutlet } = this.frameService.findFrame(frame);
320322

321-
const lastState = this.peekState(cachedFrameRootOutlet || this.currentOutlet);
323+
const lastState = this.peekState(cachedFrameRootOutlet || this.currentOutlet);
322324

323-
if (lastState) {
324-
lastState.isModalNavigation = true;
325-
}
325+
if (lastState) {
326+
lastState.isModalNavigation = true;
327+
}
326328

327-
this._isModalNavigation = true;
328-
}
329+
this._isModalNavigation = true;
330+
}
329331

330332
public _beginCloseModalNavigation(): void {
331-
routerLog("NSLocationStrategy.startCloseModal()");
332333
if (this._isModalClosing) {
333-
throw new Error("Calling startCloseModal while closing modal.");
334+
routerError("Attempted to call startCloseModal while closing modal.");
335+
return;
334336
}
337+
routerLog("NSLocationStrategy.startCloseModal()");
335338
this._isModalClosing = true;
336339
}
337340

338341
public _finishCloseModalNavigation() {
339-
routerLog("NSLocationStrategy.finishCloseModalNavigation()");
340342
if (!this._isModalClosing) {
341-
throw new Error("Calling startCloseModal while not closing modal.");
343+
routerError("Attempted to call startCloseModal while not closing modal.");
344+
return;
342345
}
343346

347+
routerLog("NSLocationStrategy.finishCloseModalNavigation()");
344348
this._isModalNavigation = false;
345349
this._isModalClosing = false;
346350
}

nativescript-angular/router/page-router-outlet.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,16 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
126126

127127
get component(): Object {
128128
if (!this.activated) {
129-
throw new Error("Outlet is not activated");
129+
log("Outlet is not activated");
130+
return;
130131
}
131132

132133
return this.activated.instance;
133134
}
134135
get activatedRoute(): ActivatedRoute {
135136
if (!this.activated) {
136-
throw new Error("Outlet is not activated");
137+
log("Outlet is not activated");
138+
return;
137139
}
138140

139141
return this._activatedRoute;
@@ -173,8 +175,8 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
173175

174176
deactivate(): void {
175177
if (!this.locationStrategy._isPageNavigatingBack()) {
176-
throw new Error("Currently not in page back navigation" +
177-
" - component should be detached instead of deactivated.");
178+
log("Currently not in page back navigation - component should be detached instead of deactivated.");
179+
return;
178180
}
179181

180182
log("PageRouterOutlet.deactivate() while going back - should destroy");
@@ -197,7 +199,8 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
197199
*/
198200
detach(): ComponentRef<any> {
199201
if (!this.isActivated) {
200-
throw new Error("Outlet is not activated");
202+
log("Outlet is not activated");
203+
return;
201204
}
202205

203206
log("PageRouterOutlet.detach() - " + routeToString(this._activatedRoute));
@@ -232,7 +235,8 @@ export class PageRouterOutlet implements OnDestroy { // tslint:disable-line:dire
232235
resolver: ComponentFactoryResolver | null): void {
233236

234237
if (this.locationStrategy._isPageNavigatingBack()) {
235-
throw new Error("Currently in page back navigation - component should be reattached instead of activated.");
238+
log("Currently in page back navigation - component should be reattached instead of activated.");
239+
this.locationStrategy._finishBackPageNavigation();
236240
}
237241

238242
log("PageRouterOutlet.activateWith() - " + routeToString(activatedRoute));

nativescript-angular/trace.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export function routerLog(message: string): void {
2828
write(message, routerTraceCategory);
2929
}
3030

31+
export function routerError(message: string): void {
32+
write(message, routerTraceCategory, messageType.error);
33+
}
34+
3135
export function routeReuseStrategyLog(message: string): void {
3236
write(message, routeReuseStrategyTraceCategory);
3337
}

0 commit comments

Comments
 (0)