From 3928845895ed6d14d3511055ee29bd0535f3d3d5 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Mon, 27 Aug 2018 19:48:28 +0300 Subject: [PATCH] refactor: call tracer only after isEnabled check original PR: https://github.com/NativeScript/nativescript-angular/pull/958 --- .../animations/animation-player.ts | 26 +++-- .../directives/list-view-comp.ts | 42 +++++-- nativescript-angular/directives/tab-view.ts | 6 +- nativescript-angular/dom-adapter.ts | 12 +- nativescript-angular/platform-common.ts | 62 +++++++--- nativescript-angular/renderer.ts | 106 ++++++++++++----- .../router/ns-location-strategy.ts | 108 +++++++++++++----- .../router/ns-platform-location.ts | 7 +- .../router/ns-route-reuse-strategy.ts | 30 +++-- nativescript-angular/router/ns-router-link.ts | 16 ++- .../router/page-router-outlet.ts | 55 ++++++--- nativescript-angular/trace.ts | 1 + nativescript-angular/view-util.ts | 40 +++++-- 13 files changed, 368 insertions(+), 143 deletions(-) diff --git a/nativescript-angular/animations/animation-player.ts b/nativescript-angular/animations/animation-player.ts index 3e0472cdd..fa38f6782 100644 --- a/nativescript-angular/animations/animation-player.ts +++ b/nativescript-angular/animations/animation-player.ts @@ -4,7 +4,7 @@ import { KeyframeAnimation } import { Keyframe, createKeyframeAnimation } from "./utils"; import { NgView } from "../element-registry"; -import { animationsLog as traceLog } from "../trace"; +import { animationsLog as traceLog, isLogEnabled } from "../trace"; export class NativeScriptAnimationPlayer implements AnimationPlayer { public parentPlayer: AnimationPlayer = null; @@ -41,7 +41,9 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer { onDestroy(fn: Function): void { this._doneSubscriptions.push(fn); } play(): void { - traceLog(`NativeScriptAnimationPlayer.play`); + if (isLogEnabled()) { + traceLog(`NativeScriptAnimationPlayer.play`); + } if (!this.animation) { return; @@ -66,7 +68,9 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer { } reset(): void { - traceLog(`NativeScriptAnimationPlayer.reset`); + if (isLogEnabled()) { + traceLog(`NativeScriptAnimationPlayer.reset`); + } if (this.animation && this.animation.isPlaying) { this.animation.cancel(); @@ -74,14 +78,18 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer { } restart(): void { - traceLog(`NativeScriptAnimationPlayer.restart`); + if (isLogEnabled()) { + traceLog(`NativeScriptAnimationPlayer.restart`); + } this.reset(); this.play(); } destroy(): void { - traceLog(`NativeScriptAnimationPlayer.destroy`); + if (isLogEnabled()) { + traceLog(`NativeScriptAnimationPlayer.destroy`); + } this.onFinish(); } @@ -94,13 +102,17 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer { } private initKeyframeAnimation(keyframes: Keyframe[], duration: number, delay: number, easing: string) { - traceLog(`NativeScriptAnimationPlayer.initKeyframeAnimation`); + if (isLogEnabled()) { + traceLog(`NativeScriptAnimationPlayer.initKeyframeAnimation`); + } this.animation = createKeyframeAnimation(keyframes, duration, delay, easing); } private onFinish() { - traceLog(`NativeScriptAnimationPlayer.onFinish`); + if (isLogEnabled()) { + traceLog(`NativeScriptAnimationPlayer.onFinish`); + } if (this._finished) { return; diff --git a/nativescript-angular/directives/list-view-comp.ts b/nativescript-angular/directives/list-view-comp.ts index a04f244b4..eb0b951ff 100644 --- a/nativescript-angular/directives/list-view-comp.ts +++ b/nativescript-angular/directives/list-view-comp.ts @@ -26,7 +26,7 @@ import { ObservableArray } from "tns-core-modules/data/observable-array"; import { profile } from "tns-core-modules/profiling"; import { getSingleViewRecursive } from "../element-registry"; -import { listViewLog, listViewError } from "../trace"; +import { listViewLog, listViewError, isLogEnabled } from "../trace"; const NG_VIEW = "_ngViewRef"; @@ -101,7 +101,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit { } ngAfterContentInit() { - listViewLog("ListView.ngAfterContentInit()"); + if (isLogEnabled()) { + listViewLog("ListView.ngAfterContentInit()"); + } this.setItemTemplates(); } @@ -115,7 +117,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit { this.itemTemplate = this.itemTemplateQuery; if (this._templateMap) { - listViewLog("Setting templates"); + if (isLogEnabled()) { + listViewLog("Setting templates"); + } const templates: KeyedTemplate[] = []; this._templateMap.forEach(value => { @@ -126,7 +130,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit { } public registerTemplate(key: string, template: TemplateRef) { - listViewLog("registerTemplate for key: " + key); + if (isLogEnabled()) { + listViewLog(`registerTemplate for key: ${key}`); + } if (!this._templateMap) { this._templateMap = new Map(); } @@ -134,7 +140,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit { const keyedTemplate = { key, createView: () => { - listViewLog("registerTemplate for key: " + key); + if (isLogEnabled()) { + listViewLog(`registerTemplate for key: ${key}`); + } const viewRef = this.loader.createEmbeddedView(template, new ListItemContext(), 0); const resultView = getItemViewRoot(viewRef); @@ -159,7 +167,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit { let viewRef: EmbeddedViewRef; if (args.view) { - listViewLog("onItemLoading: " + index + " - Reusing existing view"); + if (isLogEnabled()) { + listViewLog(`onItemLoading: ${index} - Reusing existing view`); + } viewRef = args.view[NG_VIEW]; // Getting angular view from original element (in cases when ProxyViewContainer // is used NativeScript internally wraps it in a StackLayout) @@ -168,12 +178,16 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit { } if (!viewRef) { - listViewError("ViewReference not found for item " + index + ". View recycling is not working"); + if (isLogEnabled()) { + listViewError(`ViewReference not found for item ${index}. View recycling is not working`); + } } } if (!viewRef) { - listViewLog("onItemLoading: " + index + " - Creating view from template"); + if (isLogEnabled()) { + listViewLog(`onItemLoading: ${index} - Creating view from template`); + } viewRef = this.loader.createEmbeddedView(this.itemTemplate, new ListItemContext(), 0); args.view = getItemViewRoot(viewRef); args.view[NG_VIEW] = viewRef; @@ -197,17 +211,23 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit { @profile private detectChangesOnChild(viewRef: EmbeddedViewRef, index: number) { - listViewLog("Manually detect changes in child: " + index); + if (isLogEnabled()) { + listViewLog(`Manually detect changes in child: ${index}`); + } viewRef.markForCheck(); viewRef.detectChanges(); } ngDoCheck() { if (this._differ) { - listViewLog("ngDoCheck() - execute differ"); + if (isLogEnabled()) { + listViewLog("ngDoCheck() - execute differ"); + } const changes = this._differ.diff(this._items); if (changes) { - listViewLog("ngDoCheck() - refresh"); + if (isLogEnabled()) { + listViewLog("ngDoCheck() - refresh"); + } this.listView.refresh(); } } diff --git a/nativescript-angular/directives/tab-view.ts b/nativescript-angular/directives/tab-view.ts index c569f9fc8..787918658 100644 --- a/nativescript-angular/directives/tab-view.ts +++ b/nativescript-angular/directives/tab-view.ts @@ -11,7 +11,7 @@ import { TabView, TabViewItem } from "tns-core-modules/ui/tab-view"; import { TextTransform } from "tns-core-modules/ui/text-base"; import { InvisibleNode } from "../element-registry"; -import { rendererLog } from "../trace"; +import { rendererLog, isLogEnabled } from "../trace"; import { isBlank } from "../lang-facade"; export interface TabViewItemDef { @@ -46,7 +46,9 @@ export class TabViewDirective implements AfterViewInit { ngAfterViewInit() { this.viewInitialized = true; - rendererLog("this._selectedIndex: " + this._selectedIndex); + if (isLogEnabled()) { + rendererLog("this._selectedIndex: " + this._selectedIndex); + } if (!isBlank(this._selectedIndex)) { this.tabView.selectedIndex = this._selectedIndex; } diff --git a/nativescript-angular/dom-adapter.ts b/nativescript-angular/dom-adapter.ts index 2a112782f..7ddffb681 100644 --- a/nativescript-angular/dom-adapter.ts +++ b/nativescript-angular/dom-adapter.ts @@ -1,7 +1,7 @@ /* tslint:disable */ import { Type } from "@angular/core"; import { ɵDomAdapter } from "@angular/platform-browser"; -import { rendererLog } from "./trace"; +import { rendererLog, isLogEnabled } from "./trace"; export class NativeScriptDomAdapter implements ɵDomAdapter { static makeCurrent() { @@ -12,12 +12,16 @@ export class NativeScriptDomAdapter implements ɵDomAdapter { const privateAPI = global.require("@angular/platform-browser"); const setRootDomAdapter = privateAPI.ɵsetRootDomAdapter; - rendererLog("Setting root DOM adapter..."); + if (isLogEnabled()) { + rendererLog("Setting root DOM adapter..."); + } setRootDomAdapter(new NativeScriptDomAdapter()); } catch (e) { - rendererLog("@angular/platform-browser package not present. NOT setting root DOM adapter..."); + if (isLogEnabled()) { + rendererLog("@angular/platform-browser package not present. NOT setting root DOM adapter..."); + } } - } + } } hasProperty(_element: any, _name: string) { diff --git a/nativescript-angular/platform-common.ts b/nativescript-angular/platform-common.ts index b74fcf983..6e61289aa 100644 --- a/nativescript-angular/platform-common.ts +++ b/nativescript-angular/platform-common.ts @@ -22,7 +22,7 @@ import { } from "@angular/core"; import { DOCUMENT } from "@angular/common"; -import { bootstrapLog, bootstrapLogError } from "./trace"; +import { bootstrapLog, bootstrapLogError, isLogEnabled } from "./trace"; import { defaultPageFactoryProvider, setRootPage, PageFactory, PAGE_FACTORY } from "./platform-providers"; import { AppHostView } from "./app-host-view"; @@ -156,18 +156,24 @@ export class NativeScriptPlatformRef extends PlatformRef { setRootPage(tempAppHostView); } - bootstrapLog("NativeScriptPlatform bootstrap started."); + if (isLogEnabled()) { + bootstrapLog("NativeScriptPlatform bootstrap started."); + } const launchCallback = profile( "nativescript-angular/platform-common.launchCallback", (args: LaunchEventData) => { - bootstrapLog("Application launch event fired"); + if (isLogEnabled()) { + bootstrapLog("Application launch event fired"); + } let bootstrapPromiseCompleted = false; this._bootstrapper().then( moduleRef => { bootstrapPromiseCompleted = true; - bootstrapLog(`Angular bootstrap bootstrap done. uptime: ${uptime()}`); + if (isLogEnabled()) { + bootstrapLog(`Angular bootstrap bootstrap done. uptime: ${uptime()}`); + } if (!autoCreateFrame) { rootContent = tempAppHostView.content; @@ -179,20 +185,30 @@ export class NativeScriptPlatformRef extends PlatformRef { bootstrapPromiseCompleted = true; const errorMessage = err.message + "\n\n" + err.stack; - bootstrapLogError("ERROR BOOTSTRAPPING ANGULAR"); - bootstrapLogError(errorMessage); + if (isLogEnabled()) { + bootstrapLogError("ERROR BOOTSTRAPPING ANGULAR"); + } + if (isLogEnabled()) { + bootstrapLogError(errorMessage); + } rootContent = this.createErrorUI(errorMessage); } ); - bootstrapLog("bootstrapAction called, draining micro tasks queue. Root: " + rootContent); + if (isLogEnabled()) { + bootstrapLog("bootstrapAction called, draining micro tasks queue. Root: " + rootContent); + } (global).Zone.drainMicroTaskQueue(); - bootstrapLog("bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent); + if (isLogEnabled()) { + bootstrapLog("bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent); + } if (!bootstrapPromiseCompleted) { const errorMessage = "Bootstrap promise didn't resolve"; - bootstrapLogError(errorMessage); + if (isLogEnabled()) { + bootstrapLogError(errorMessage); + } rootContent = this.createErrorUI(errorMessage); } @@ -206,7 +222,9 @@ export class NativeScriptPlatformRef extends PlatformRef { @profile public _livesync() { - bootstrapLog("Angular livesync started."); + if (isLogEnabled()) { + bootstrapLog("Angular livesync started."); + } onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null); const autoCreateFrame = !!this.appOptions.createFrameOnBootstrap; @@ -227,7 +245,9 @@ export class NativeScriptPlatformRef extends PlatformRef { this._bootstrapper().then( moduleRef => { bootstrapPromiseCompleted = true; - bootstrapLog("Angular livesync done."); + if (isLogEnabled()) { + bootstrapLog("Angular livesync done."); + } onAfterLivesync.next({ moduleRef }); if (!autoCreateFrame) { @@ -238,9 +258,13 @@ export class NativeScriptPlatformRef extends PlatformRef { }, error => { bootstrapPromiseCompleted = true; - bootstrapLogError("ERROR LIVESYNC BOOTSTRAPPING ANGULAR"); + if (isLogEnabled()) { + bootstrapLogError("ERROR LIVESYNC BOOTSTRAPPING ANGULAR"); + } const errorMessage = error.message + "\n\n" + error.stack; - bootstrapLogError(errorMessage); + if (isLogEnabled()) { + bootstrapLogError(errorMessage); + } rootContent = this.createErrorUI(errorMessage); @@ -248,13 +272,19 @@ export class NativeScriptPlatformRef extends PlatformRef { } ); - bootstrapLog("livesync bootstrapAction called, draining micro tasks queue. Root: " + rootContent); + if (isLogEnabled()) { + bootstrapLog("livesync bootstrapAction called, draining micro tasks queue. Root: " + rootContent); + } (global).Zone.drainMicroTaskQueue(); - bootstrapLog("livesync bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent); + if (isLogEnabled()) { + bootstrapLog("livesync bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent); + } if (!bootstrapPromiseCompleted) { const result = "Livesync bootstrap promise didn't resolve"; - bootstrapLogError(result); + if (isLogEnabled()) { + bootstrapLogError(result); + } rootContent = this.createErrorUI(result); onAfterLivesync.next({ error: new Error(result) }); diff --git a/nativescript-angular/renderer.ts b/nativescript-angular/renderer.ts index cad922ac2..ea6998cde 100644 --- a/nativescript-angular/renderer.ts +++ b/nativescript-angular/renderer.ts @@ -12,7 +12,7 @@ import { profile } from "tns-core-modules/profiling"; import { APP_ROOT_VIEW, DEVICE, getRootPage } from "./platform-providers"; import { ViewUtil } from "./view-util"; import { NgView, InvisibleNode } from "./element-registry"; -import { rendererLog as traceLog } from "./trace"; +import { rendererLog as traceLog, isLogEnabled } from "./trace"; // CONTENT_ATTR not exported from NativeScript_renderer - we need it for styles application. const COMPONENT_REGEX = /%COMP%/g; @@ -84,31 +84,41 @@ export class NativeScriptRenderer extends Renderer2 { private viewUtil: ViewUtil ) { super(); - traceLog("NativeScriptRenderer created"); + if (isLogEnabled()) { + traceLog("NativeScriptRenderer created"); + } } @profile appendChild(parent: NgView, newChild: NgView): void { - traceLog(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`); + } this.viewUtil.insertChild(parent, newChild); } @profile insertBefore(parent: NgView, newChild: NgView, { previous, next }: ElementReference): void { - traceLog(`NativeScriptRenderer.insertBefore child: ${newChild} ` + + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.insertBefore child: ${newChild} ` + `parent: ${parent} previous: ${previous} next: ${next}`); + } this.viewUtil.insertChild(parent, newChild, previous, next); } @profile removeChild(parent: any, oldChild: NgView): void { - traceLog(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`); + } this.viewUtil.removeChild(parent, oldChild); } @profile selectRootElement(selector: string): NgView { - traceLog("NativeScriptRenderer.selectRootElement: " + selector); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.selectRootElement: ${selector}`); + } if (selector && selector[0] === "#") { const result = getViewById(this.rootView, selector.slice(1)); return (result || this.rootView) as NgView; @@ -118,13 +128,17 @@ export class NativeScriptRenderer extends Renderer2 { @profile parentNode(node: NgView): any { - traceLog(`NativeScriptRenderer.parentNode for node: ${node}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.parentNode for node: ${node}`); + } return node.parentNode; } @profile nextSibling(node: NgView): ElementReference { - traceLog(`NativeScriptRenderer.nextSibling of ${node} is ${node.nextSibling}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.nextSibling of ${node} is ${node.nextSibling}`); + } return { previous: node, @@ -134,79 +148,105 @@ export class NativeScriptRenderer extends Renderer2 { @profile createComment(_value: any): InvisibleNode { - traceLog(`NativeScriptRenderer.createComment ${_value}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.createComment ${_value}`); + } return this.viewUtil.createComment(); } @profile createElement(name: any, _namespace: string): NgView { - traceLog(`NativeScriptRenderer.createElement: ${name}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.createElement: ${name}`); + } return this.viewUtil.createView(name); } @profile createText(_value: string): InvisibleNode { - traceLog(`NativeScriptRenderer.createText ${_value}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.createText ${_value}`); + } return this.viewUtil.createText(); } @profile createViewRoot(hostElement: NgView): NgView { - traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`); + } return hostElement; } @profile projectNodes(parentElement: NgView, nodes: NgView[]): void { - traceLog("NativeScriptRenderer.projectNodes"); + if (isLogEnabled()) { + traceLog("NativeScriptRenderer.projectNodes"); + } nodes.forEach((node) => this.viewUtil.insertChild(parentElement, node)); } @profile destroy() { - traceLog("NativeScriptRenderer.destroy"); + if (isLogEnabled()) { + traceLog("NativeScriptRenderer.destroy"); + } // Seems to be called on component dispose only (router outlet) // TODO: handle this when we resolve routing and navigation. } @profile setAttribute(view: NgView, name: string, value: string, namespace?: string) { - traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}, namespace: ${namespace}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}, namespace: ${namespace}`); + } return this.viewUtil.setProperty(view, name, value, namespace); } @profile removeAttribute(_el: NgView, _name: string): void { - traceLog(`NativeScriptRenderer.removeAttribute ${_el}: ${_name}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.removeAttribute ${_el}: ${_name}`); + } } @profile setProperty(view: any, name: string, value: any) { - traceLog(`NativeScriptRenderer.setProperty ${view} : ${name} = ${value}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.setProperty ${view} : ${name} = ${value}`); + } return this.viewUtil.setProperty(view, name, value); } @profile addClass(view: NgView, name: string): void { - traceLog(`NativeScriptRenderer.addClass ${name}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.addClass ${name}`); + } this.viewUtil.addClass(view, name); } @profile removeClass(view: NgView, name: string): void { - traceLog(`NativeScriptRenderer.removeClass ${name}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.removeClass ${name}`); + } this.viewUtil.removeClass(view, name); } @profile setStyle(view: NgView, styleName: string, value: any, _flags?: RendererStyleFlags2): void { - traceLog(`NativeScriptRenderer.setStyle: ${styleName} = ${value}`); + if (isLogEnabled()) { + traceLog(`NativeScriptRenderer.setStyle: ${styleName} = ${value}`); + } this.viewUtil.setStyle(view, styleName, value); } @profile removeStyle(view: NgView, styleName: string, _flags?: RendererStyleFlags2): void { - traceLog("NativeScriptRenderer.removeStyle: ${styleName}"); + if (isLogEnabled()) { + traceLog("NativeScriptRenderer.removeStyle: ${styleName}"); + } this.viewUtil.removeStyle(view, styleName); } @@ -214,32 +254,38 @@ export class NativeScriptRenderer extends Renderer2 { // such as