Skip to content

Commit d6e0e13

Browse files
committed
The tns-core-modules tracer was intended to be called after an isEnabledCheck() to avoid string concatenation
1 parent 147d35a commit d6e0e13

File tree

4 files changed

+119
-52
lines changed

4 files changed

+119
-52
lines changed

nativescript-angular/renderer.ts

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { APP_ROOT_VIEW, DEVICE, getRootPage } from "./platform-providers";
1414
import { isBlank } from "./lang-facade";
1515
import { ViewUtil } from "./view-util";
1616
import { NgView, InvisibleNode } from "./element-registry";
17-
import { rendererLog as traceLog } from "./trace";
17+
import { rendererLog as log, isEnabled as isLogEnabled } from "./trace";
1818

1919
// CONTENT_ATTR not exported from NativeScript_renderer - we need it for styles application.
2020
const COMPONENT_REGEX = /%COMP%/g;
@@ -81,153 +81,202 @@ export class NativeScriptRenderer extends Renderer2 {
8181
private viewUtil: ViewUtil
8282
) {
8383
super();
84-
traceLog("NativeScriptRenderer created");
84+
if (isLogEnabled()) {
85+
log("NativeScriptRenderer created");
86+
}
8587
}
8688

8789
@profile
8890
appendChild(parent: any, newChild: NgView): void {
89-
traceLog(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`);
91+
if (isLogEnabled()) {
92+
log(`NativeScriptRenderer.appendChild child: ${newChild} parent: ${parent}`);
93+
}
9094
this.viewUtil.insertChild(parent, newChild);
9195
}
9296

9397
@profile
9498
insertBefore(parent: NgView, newChild: NgView, refChildIndex: number): void {
95-
traceLog(`NativeScriptRenderer.insertBefore child: ${newChild} parent: ${parent}`);
99+
if (isLogEnabled()) {
100+
log(`NativeScriptRenderer.insertBefore child: ${newChild} parent: ${parent}`);
101+
}
96102
this.viewUtil.insertChild(parent, newChild, refChildIndex);
97103
}
98104

99105
@profile
100106
removeChild(parent: any, oldChild: NgView): void {
101-
traceLog(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`);
107+
if (isLogEnabled()) {
108+
log(`NativeScriptRenderer.removeChild child: ${oldChild} parent: ${parent}`);
109+
}
102110
this.viewUtil.removeChild(parent, oldChild);
103111
}
104112

105113
@profile
106114
selectRootElement(selector: string): NgView {
107-
traceLog("NativeScriptRenderer.selectRootElement: " + selector);
115+
if (isLogEnabled()) {
116+
log("NativeScriptRenderer.selectRootElement: " + selector);
117+
}
108118
return this.rootView;
109119
}
110120

111121
@profile
112122
parentNode(node: NgView): any {
113-
traceLog("NativeScriptRenderer.parentNode for node: " + node);
123+
if (isLogEnabled()) {
124+
log("NativeScriptRenderer.parentNode for node: " + node);
125+
}
114126
return node.parent || node.templateParent;
115127
}
116128

117129
@profile
118130
nextSibling(node: NgView): number {
119-
traceLog(`NativeScriptRenderer.nextSibling ${node}`);
131+
if (isLogEnabled()) {
132+
log(`NativeScriptRenderer.nextSibling ${node}`);
133+
}
120134
return this.viewUtil.nextSiblingIndex(node);
121135
}
122136

123137
@profile
124138
createComment(_value: any): InvisibleNode {
125-
traceLog(`NativeScriptRenderer.createComment ${_value}`);
139+
if (isLogEnabled()) {
140+
log(`NativeScriptRenderer.createComment ${_value}`);
141+
}
126142
return this.viewUtil.createComment();
127143
}
128144

129145
@profile
130146
createElement(name: any, _namespace: string): NgView {
131-
traceLog(`NativeScriptRenderer.createElement: ${name}`);
147+
if (isLogEnabled()) {
148+
log(`NativeScriptRenderer.createElement: ${name}`);
149+
}
132150
return this.viewUtil.createView(name);
133151
}
134152

135153
@profile
136154
createText(_value: string): InvisibleNode {
137-
traceLog(`NativeScriptRenderer.createText ${_value}`);
155+
if (isLogEnabled()) {
156+
log(`NativeScriptRenderer.createText ${_value}`);
157+
}
138158
return this.viewUtil.createText();
139159
}
140160

141161
@profile
142162
createViewRoot(hostElement: NgView): NgView {
143-
traceLog(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`);
163+
if (isLogEnabled()) {
164+
log(`NativeScriptRenderer.createViewRoot ${hostElement.nodeName}`);
165+
}
144166
return hostElement;
145167
}
146168

147169
@profile
148170
projectNodes(parentElement: NgView, nodes: NgView[]): void {
149-
traceLog("NativeScriptRenderer.projectNodes");
171+
if (isLogEnabled()) {
172+
log("NativeScriptRenderer.projectNodes");
173+
}
150174
nodes.forEach((node) => this.viewUtil.insertChild(parentElement, node));
151175
}
152176

153177
@profile
154178
destroy() {
155-
traceLog("NativeScriptRenderer.destroy");
179+
if (isLogEnabled()) {
180+
log("NativeScriptRenderer.destroy");
181+
}
156182
// Seems to be called on component dispose only (router outlet)
157183
// TODO: handle this when we resolve routing and navigation.
158184
}
159185

160186
@profile
161187
setAttribute(view: NgView, name: string, value: string, namespace?: string) {
162-
traceLog(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}, namespace: ${namespace}`);
188+
if (isLogEnabled()) {
189+
log(`NativeScriptRenderer.setAttribute ${view} : ${name} = ${value}, namespace: ${namespace}`);
190+
}
163191
return this.viewUtil.setProperty(view, name, value, namespace);
164192
}
165193

166194
@profile
167195
removeAttribute(_el: NgView, _name: string): void {
168-
traceLog(`NativeScriptRenderer.removeAttribute ${_el}: ${_name}`);
196+
if (isLogEnabled()) {
197+
log(`NativeScriptRenderer.removeAttribute ${_el}: ${_name}`);
198+
}
169199
}
170200

171201
@profile
172202
setProperty(view: any, name: string, value: any) {
173-
traceLog(`NativeScriptRenderer.setProperty ${view} : ${name} = ${value}`);
203+
if (isLogEnabled()) {
204+
log(`NativeScriptRenderer.setProperty ${view} : ${name} = ${value}`);
205+
}
174206
return this.viewUtil.setProperty(view, name, value);
175207
}
176208

177209
@profile
178210
addClass(view: NgView, name: string): void {
179-
traceLog(`NativeScriptRenderer.addClass ${name}`);
211+
if (isLogEnabled()) {
212+
log(`NativeScriptRenderer.addClass ${name}`);
213+
}
180214
this.viewUtil.addClass(view, name);
181215
}
182216

183217
@profile
184218
removeClass(view: NgView, name: string): void {
185-
traceLog(`NativeScriptRenderer.removeClass ${name}`);
219+
if (isLogEnabled()) {
220+
log(`NativeScriptRenderer.removeClass ${name}`);
221+
}
186222
this.viewUtil.removeClass(view, name);
187223
}
188224

189225
@profile
190226
setStyle(view: NgView, styleName: string, value: any, _flags?: RendererStyleFlags2): void {
191-
traceLog(`NativeScriptRenderer.setStyle: ${styleName} = ${value}`);
227+
if (isLogEnabled()) {
228+
log(`NativeScriptRenderer.setStyle: ${styleName} = ${value}`);
229+
}
192230
this.viewUtil.setStyle(view, styleName, value);
193231
}
194232

195233
@profile
196234
removeStyle(view: NgView, styleName: string, _flags?: RendererStyleFlags2): void {
197-
traceLog("NativeScriptRenderer.removeStyle: ${styleName}");
235+
if (isLogEnabled()) {
236+
log("NativeScriptRenderer.removeStyle: ${styleName}");
237+
}
198238
this.viewUtil.removeStyle(view, styleName);
199239
}
200240

201241
// Used only in debug mode to serialize property changes to comment nodes,
202242
// such as <template> placeholders.
203243
@profile
204244
setBindingDebugInfo(renderElement: NgView, propertyName: string, propertyValue: string): void {
205-
traceLog("NativeScriptRenderer.setBindingDebugInfo: " + renderElement + ", " +
206-
propertyName + " = " + propertyValue);
245+
if (isLogEnabled()) {
246+
log("NativeScriptRenderer.setBindingDebugInfo: " + renderElement + ", " +
247+
propertyName + " = " + propertyValue);
248+
}
207249
}
208250

209251
@profile
210252
setElementDebugInfo(renderElement: any, _info: any /*RenderDebugInfo*/): void {
211-
traceLog("NativeScriptRenderer.setElementDebugInfo: " + renderElement);
253+
if (isLogEnabled()) {
254+
log("NativeScriptRenderer.setElementDebugInfo: " + renderElement);
255+
}
212256
}
213257

214258
@profile
215259
invokeElementMethod(_renderElement: NgView, methodName: string, args: Array<any>) {
216-
traceLog("NativeScriptRenderer.invokeElementMethod " + methodName + " " + args);
260+
if (isLogEnabled()) {
261+
log("NativeScriptRenderer.invokeElementMethod " + methodName + " " + args);
262+
}
217263
}
218264

219265
@profile
220266
setValue(_renderNode: any, _value: string) {
221-
traceLog(
222-
`NativeScriptRenderer.setValue ` +
223-
`renderNode: ${_renderNode}, value: ${_value}`
224-
);
267+
if (isLogEnabled()) {
268+
log(
269+
`NativeScriptRenderer.setValue ` +
270+
`renderNode: ${_renderNode}, value: ${_value}`
271+
);
272+
}
225273
}
226274

227275
@profile
228-
listen(renderElement: any, eventName: string, callback: (event: any) => boolean):
229-
() => void {
230-
traceLog("NativeScriptRenderer.listen: " + eventName);
276+
listen(renderElement: any, eventName: string, callback: (event: any) => boolean): () => void {
277+
if (isLogEnabled()) {
278+
log("NativeScriptRenderer.listen: " + eventName);
279+
}
231280
// Explicitly wrap in zone
232281
let zonedCallback = (...args) => {
233282
this.zone.run(() => {

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BehaviorSubject } from "rxjs/BehaviorSubject";
2020

2121
import { isPresent } from "../lang-facade";
2222
import { DEVICE, PAGE_FACTORY, PageFactory } from "../platform-providers";
23-
import { routerLog } from "../trace";
23+
import { routerLog as log, isEnabled as isLogEnabled } from "../trace";
2424
import { DetachedLoader } from "../common/detached-loader";
2525
import { ViewUtil } from "../view-util";
2626
import { NSLocationStrategy } from "./ns-location-strategy";
@@ -89,11 +89,8 @@ interface CacheItem {
8989
loaderRef?: ComponentRef<any>;
9090
}
9191

92-
9392
type ProviderMap = Map<Type<any>|InjectionToken<any>, any>;
9493

95-
const log = (msg: string) => routerLog(msg);
96-
9794
@Directive({ selector: "page-router-outlet" }) // tslint:disable-line:directive-selector
9895
export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-line:directive-class-suffix
9996
private activated: ComponentRef<any>|null = null;
@@ -148,7 +145,9 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
148145

149146
this.viewUtil = new ViewUtil(device);
150147
this.detachedLoaderFactory = resolver.resolveComponentFactory(DetachedLoader);
151-
log("DetachedLoaderFactory loaded");
148+
if (isLogEnabled()) {
149+
log("DetachedLoaderFactory loaded");
150+
}
152151
}
153152

154153
ngOnDestroy(): void {
@@ -178,7 +177,9 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
178177

179178
deactivate(): void {
180179
if (this.locationStrategy._isPageNavigatingBack()) {
181-
log("PageRouterOutlet.deactivate() while going back - should destroy");
180+
if (isLogEnabled()) {
181+
log("PageRouterOutlet.deactivate() while going back - should destroy");
182+
}
182183
if (!this.isActivated) {
183184
return;
184185
}
@@ -193,7 +194,9 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
193194
RefCache.destroyItem(poppedItem);
194195
this.activated = null;
195196
} else {
196-
log("PageRouterOutlet.deactivate() while going forward - do nothing");
197+
if (isLogEnabled()) {
198+
log("PageRouterOutlet.deactivate() while going forward - do nothing");
199+
}
197200
}
198201
}
199202

@@ -232,9 +235,11 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
232235
* Called when the `RouteReuseStrategy` instructs to re-attach a previously detached subtree
233236
*/
234237
attach(ref: ComponentRef<any>, activatedRoute: ActivatedRoute) {
235-
log("PageRouterOutlet.attach()" +
236-
"when RouteReuseStrategy instructs to re-attach " +
237-
"previously detached subtree");
238+
if (isLogEnabled()) {
239+
log("PageRouterOutlet.attach()" +
240+
"when RouteReuseStrategy instructs to re-attach " +
241+
"previously detached subtree");
242+
}
238243

239244
this.activated = ref;
240245
this._activatedRoute = activatedRoute;
@@ -250,9 +255,10 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
250255
activatedRoute: ActivatedRoute,
251256
resolver: ComponentFactoryResolver|null
252257
): void {
253-
254-
log("PageRouterOutlet.activateWith() - " +
255-
"instanciating new component during commit phase of a navigation");
258+
if (isLogEnabled()) {
259+
log("PageRouterOutlet.activateWith() - " +
260+
"instanciating new component during commit phase of a navigation");
261+
}
256262

257263
this._activatedRoute = activatedRoute;
258264
resolver = resolver || this.resolver;
@@ -275,7 +281,9 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
275281
const factory = this.getComponentFactory(activatedRoute, loadedResolver);
276282

277283
if (this.isInitialPage) {
278-
log("PageRouterOutlet.activate() initial page - just load component");
284+
if (isLogEnabled()) {
285+
log("PageRouterOutlet.activate() initial page - just load component");
286+
}
279287

280288
this.isInitialPage = false;
281289

@@ -289,8 +297,10 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
289297
loaderRef: null,
290298
});
291299
} else {
292-
log("PageRouterOutlet.activate() forward navigation - " +
293-
"create detached loader in the loader container");
300+
if (isLogEnabled()) {
301+
log("PageRouterOutlet.activate() forward navigation - " +
302+
"create detached loader in the loader container");
303+
}
294304

295305
const page = this.pageFactory({
296306
isNavigation: true,
@@ -330,7 +340,9 @@ export class PageRouterOutlet implements OnDestroy, OnInit { // tslint:disable-l
330340
}
331341

332342
private activateOnGoBack(activatedRoute: ActivatedRoute): void {
333-
log("PageRouterOutlet.activate() - Back navigation, so load from cache");
343+
if (isLogEnabled()) {
344+
log("PageRouterOutlet.activate() - Back navigation, so load from cache");
345+
}
334346

335347
this.locationStrategy._finishBackPageNavigation();
336348

nativescript-angular/trace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { write, categories, messageType } from "tns-core-modules/trace";
2+
export { isEnabled } from "tns-core-modules/trace";
23

34
export const animationsTraceCategory = "ns-animations";
45
export const rendererTraceCategory = "ns-renderer";

nativescript-angular/view-util.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
} from "./element-registry";
1818

1919
import { platformNames, Device } from "tns-core-modules/platform";
20-
import { rendererLog as traceLog } from "./trace";
20+
import { rendererLog as log, isEnabled as isLogEnabled } from "./trace";
21+
import { profile } from "tns-core-modules/profiling";
2122

2223
const XML_ATTRIBUTES = Object.freeze(["style", "rows", "columns", "fontAttributes"]);
2324
const ELEMENT_NODE_TYPE = 1;
@@ -118,7 +119,9 @@ export class ViewUtil {
118119
}
119120

120121
public createView(name: string): NgView {
121-
traceLog(`Creating view: ${name}`);
122+
if (isLogEnabled()) {
123+
log(`Creating view: ${name}`);
124+
}
122125

123126
if (!isKnownView(name)) {
124127
name = "ProxyViewContainer";
@@ -197,7 +200,9 @@ export class ViewUtil {
197200

198201

199202
private setPropertyInternal(view: NgView, attributeName: string, value: any): void {
200-
traceLog(`Setting attribute: ${attributeName}=${value} to ${view}`);
203+
if (isLogEnabled()) {
204+
log(`Setting attribute: ${attributeName}=${value} to ${view}`);
205+
}
201206

202207
if (attributeName === "class") {
203208
this.setClasses(view, value);

0 commit comments

Comments
 (0)