Skip to content

Commit 3928845

Browse files
committed
refactor: call tracer only after isEnabled check
original PR: #958
1 parent 3b434c9 commit 3928845

13 files changed

+368
-143
lines changed

nativescript-angular/animations/animation-player.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { KeyframeAnimation }
44

55
import { Keyframe, createKeyframeAnimation } from "./utils";
66
import { NgView } from "../element-registry";
7-
import { animationsLog as traceLog } from "../trace";
7+
import { animationsLog as traceLog, isLogEnabled } from "../trace";
88

99
export class NativeScriptAnimationPlayer implements AnimationPlayer {
1010
public parentPlayer: AnimationPlayer = null;
@@ -41,7 +41,9 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer {
4141
onDestroy(fn: Function): void { this._doneSubscriptions.push(fn); }
4242

4343
play(): void {
44-
traceLog(`NativeScriptAnimationPlayer.play`);
44+
if (isLogEnabled()) {
45+
traceLog(`NativeScriptAnimationPlayer.play`);
46+
}
4547

4648
if (!this.animation) {
4749
return;
@@ -66,22 +68,28 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer {
6668
}
6769

6870
reset(): void {
69-
traceLog(`NativeScriptAnimationPlayer.reset`);
71+
if (isLogEnabled()) {
72+
traceLog(`NativeScriptAnimationPlayer.reset`);
73+
}
7074

7175
if (this.animation && this.animation.isPlaying) {
7276
this.animation.cancel();
7377
}
7478
}
7579

7680
restart(): void {
77-
traceLog(`NativeScriptAnimationPlayer.restart`);
81+
if (isLogEnabled()) {
82+
traceLog(`NativeScriptAnimationPlayer.restart`);
83+
}
7884

7985
this.reset();
8086
this.play();
8187
}
8288

8389
destroy(): void {
84-
traceLog(`NativeScriptAnimationPlayer.destroy`);
90+
if (isLogEnabled()) {
91+
traceLog(`NativeScriptAnimationPlayer.destroy`);
92+
}
8593
this.onFinish();
8694
}
8795

@@ -94,13 +102,17 @@ export class NativeScriptAnimationPlayer implements AnimationPlayer {
94102
}
95103

96104
private initKeyframeAnimation(keyframes: Keyframe[], duration: number, delay: number, easing: string) {
97-
traceLog(`NativeScriptAnimationPlayer.initKeyframeAnimation`);
105+
if (isLogEnabled()) {
106+
traceLog(`NativeScriptAnimationPlayer.initKeyframeAnimation`);
107+
}
98108

99109
this.animation = createKeyframeAnimation(keyframes, duration, delay, easing);
100110
}
101111

102112
private onFinish() {
103-
traceLog(`NativeScriptAnimationPlayer.onFinish`);
113+
if (isLogEnabled()) {
114+
traceLog(`NativeScriptAnimationPlayer.onFinish`);
115+
}
104116

105117
if (this._finished) {
106118
return;

nativescript-angular/directives/list-view-comp.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { ObservableArray } from "tns-core-modules/data/observable-array";
2626
import { profile } from "tns-core-modules/profiling";
2727

2828
import { getSingleViewRecursive } from "../element-registry";
29-
import { listViewLog, listViewError } from "../trace";
29+
import { listViewLog, listViewError, isLogEnabled } from "../trace";
3030

3131
const NG_VIEW = "_ngViewRef";
3232

@@ -101,7 +101,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
101101
}
102102

103103
ngAfterContentInit() {
104-
listViewLog("ListView.ngAfterContentInit()");
104+
if (isLogEnabled()) {
105+
listViewLog("ListView.ngAfterContentInit()");
106+
}
105107
this.setItemTemplates();
106108
}
107109

@@ -115,7 +117,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
115117
this.itemTemplate = this.itemTemplateQuery;
116118

117119
if (this._templateMap) {
118-
listViewLog("Setting templates");
120+
if (isLogEnabled()) {
121+
listViewLog("Setting templates");
122+
}
119123

120124
const templates: KeyedTemplate[] = [];
121125
this._templateMap.forEach(value => {
@@ -126,15 +130,19 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
126130
}
127131

128132
public registerTemplate(key: string, template: TemplateRef<ListItemContext>) {
129-
listViewLog("registerTemplate for key: " + key);
133+
if (isLogEnabled()) {
134+
listViewLog(`registerTemplate for key: ${key}`);
135+
}
130136
if (!this._templateMap) {
131137
this._templateMap = new Map<string, KeyedTemplate>();
132138
}
133139

134140
const keyedTemplate = {
135141
key,
136142
createView: () => {
137-
listViewLog("registerTemplate for key: " + key);
143+
if (isLogEnabled()) {
144+
listViewLog(`registerTemplate for key: ${key}`);
145+
}
138146

139147
const viewRef = this.loader.createEmbeddedView(template, new ListItemContext(), 0);
140148
const resultView = getItemViewRoot(viewRef);
@@ -159,7 +167,9 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
159167
let viewRef: EmbeddedViewRef<ListItemContext>;
160168

161169
if (args.view) {
162-
listViewLog("onItemLoading: " + index + " - Reusing existing view");
170+
if (isLogEnabled()) {
171+
listViewLog(`onItemLoading: ${index} - Reusing existing view`);
172+
}
163173
viewRef = args.view[NG_VIEW];
164174
// Getting angular view from original element (in cases when ProxyViewContainer
165175
// is used NativeScript internally wraps it in a StackLayout)
@@ -168,12 +178,16 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
168178
}
169179

170180
if (!viewRef) {
171-
listViewError("ViewReference not found for item " + index + ". View recycling is not working");
181+
if (isLogEnabled()) {
182+
listViewError(`ViewReference not found for item ${index}. View recycling is not working`);
183+
}
172184
}
173185
}
174186

175187
if (!viewRef) {
176-
listViewLog("onItemLoading: " + index + " - Creating view from template");
188+
if (isLogEnabled()) {
189+
listViewLog(`onItemLoading: ${index} - Creating view from template`);
190+
}
177191
viewRef = this.loader.createEmbeddedView(this.itemTemplate, new ListItemContext(), 0);
178192
args.view = getItemViewRoot(viewRef);
179193
args.view[NG_VIEW] = viewRef;
@@ -197,17 +211,23 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
197211

198212
@profile
199213
private detectChangesOnChild(viewRef: EmbeddedViewRef<ListItemContext>, index: number) {
200-
listViewLog("Manually detect changes in child: " + index);
214+
if (isLogEnabled()) {
215+
listViewLog(`Manually detect changes in child: ${index}`);
216+
}
201217
viewRef.markForCheck();
202218
viewRef.detectChanges();
203219
}
204220

205221
ngDoCheck() {
206222
if (this._differ) {
207-
listViewLog("ngDoCheck() - execute differ");
223+
if (isLogEnabled()) {
224+
listViewLog("ngDoCheck() - execute differ");
225+
}
208226
const changes = this._differ.diff(this._items);
209227
if (changes) {
210-
listViewLog("ngDoCheck() - refresh");
228+
if (isLogEnabled()) {
229+
listViewLog("ngDoCheck() - refresh");
230+
}
211231
this.listView.refresh();
212232
}
213233
}

nativescript-angular/directives/tab-view.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { TabView, TabViewItem } from "tns-core-modules/ui/tab-view";
1111
import { TextTransform } from "tns-core-modules/ui/text-base";
1212

1313
import { InvisibleNode } from "../element-registry";
14-
import { rendererLog } from "../trace";
14+
import { rendererLog, isLogEnabled } from "../trace";
1515
import { isBlank } from "../lang-facade";
1616

1717
export interface TabViewItemDef {
@@ -46,7 +46,9 @@ export class TabViewDirective implements AfterViewInit {
4646

4747
ngAfterViewInit() {
4848
this.viewInitialized = true;
49-
rendererLog("this._selectedIndex: " + this._selectedIndex);
49+
if (isLogEnabled()) {
50+
rendererLog("this._selectedIndex: " + this._selectedIndex);
51+
}
5052
if (!isBlank(this._selectedIndex)) {
5153
this.tabView.selectedIndex = this._selectedIndex;
5254
}

nativescript-angular/dom-adapter.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* tslint:disable */
22
import { Type } from "@angular/core";
33
import { ɵDomAdapter } from "@angular/platform-browser";
4-
import { rendererLog } from "./trace";
4+
import { rendererLog, isLogEnabled } from "./trace";
55

66
export class NativeScriptDomAdapter implements ɵDomAdapter {
77
static makeCurrent() {
@@ -12,12 +12,16 @@ export class NativeScriptDomAdapter implements ɵDomAdapter {
1212
const privateAPI = global.require("@angular/platform-browser");
1313
const setRootDomAdapter = privateAPI.ɵsetRootDomAdapter;
1414

15-
rendererLog("Setting root DOM adapter...");
15+
if (isLogEnabled()) {
16+
rendererLog("Setting root DOM adapter...");
17+
}
1618
setRootDomAdapter(new NativeScriptDomAdapter());
1719
} catch (e) {
18-
rendererLog("@angular/platform-browser package not present. NOT setting root DOM adapter...");
20+
if (isLogEnabled()) {
21+
rendererLog("@angular/platform-browser package not present. NOT setting root DOM adapter...");
22+
}
1923
}
20-
}
24+
}
2125
}
2226

2327
hasProperty(_element: any, _name: string) {

nativescript-angular/platform-common.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from "@angular/core";
2323
import { DOCUMENT } from "@angular/common";
2424

25-
import { bootstrapLog, bootstrapLogError } from "./trace";
25+
import { bootstrapLog, bootstrapLogError, isLogEnabled } from "./trace";
2626
import { defaultPageFactoryProvider, setRootPage, PageFactory, PAGE_FACTORY } from "./platform-providers";
2727
import { AppHostView } from "./app-host-view";
2828

@@ -156,18 +156,24 @@ export class NativeScriptPlatformRef extends PlatformRef {
156156
setRootPage(<any>tempAppHostView);
157157
}
158158

159-
bootstrapLog("NativeScriptPlatform bootstrap started.");
159+
if (isLogEnabled()) {
160+
bootstrapLog("NativeScriptPlatform bootstrap started.");
161+
}
160162
const launchCallback = profile(
161163
"nativescript-angular/platform-common.launchCallback",
162164
(args: LaunchEventData) => {
163-
bootstrapLog("Application launch event fired");
165+
if (isLogEnabled()) {
166+
bootstrapLog("Application launch event fired");
167+
}
164168

165169
let bootstrapPromiseCompleted = false;
166170
this._bootstrapper().then(
167171
moduleRef => {
168172
bootstrapPromiseCompleted = true;
169173

170-
bootstrapLog(`Angular bootstrap bootstrap done. uptime: ${uptime()}`);
174+
if (isLogEnabled()) {
175+
bootstrapLog(`Angular bootstrap bootstrap done. uptime: ${uptime()}`);
176+
}
171177

172178
if (!autoCreateFrame) {
173179
rootContent = tempAppHostView.content;
@@ -179,20 +185,30 @@ export class NativeScriptPlatformRef extends PlatformRef {
179185
bootstrapPromiseCompleted = true;
180186

181187
const errorMessage = err.message + "\n\n" + err.stack;
182-
bootstrapLogError("ERROR BOOTSTRAPPING ANGULAR");
183-
bootstrapLogError(errorMessage);
188+
if (isLogEnabled()) {
189+
bootstrapLogError("ERROR BOOTSTRAPPING ANGULAR");
190+
}
191+
if (isLogEnabled()) {
192+
bootstrapLogError(errorMessage);
193+
}
184194

185195
rootContent = this.createErrorUI(errorMessage);
186196
}
187197
);
188198

189-
bootstrapLog("bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
199+
if (isLogEnabled()) {
200+
bootstrapLog("bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
201+
}
190202
(<any>global).Zone.drainMicroTaskQueue();
191-
bootstrapLog("bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
203+
if (isLogEnabled()) {
204+
bootstrapLog("bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
205+
}
192206

193207
if (!bootstrapPromiseCompleted) {
194208
const errorMessage = "Bootstrap promise didn't resolve";
195-
bootstrapLogError(errorMessage);
209+
if (isLogEnabled()) {
210+
bootstrapLogError(errorMessage);
211+
}
196212
rootContent = this.createErrorUI(errorMessage);
197213
}
198214

@@ -206,7 +222,9 @@ export class NativeScriptPlatformRef extends PlatformRef {
206222

207223
@profile
208224
public _livesync() {
209-
bootstrapLog("Angular livesync started.");
225+
if (isLogEnabled()) {
226+
bootstrapLog("Angular livesync started.");
227+
}
210228
onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null);
211229

212230
const autoCreateFrame = !!this.appOptions.createFrameOnBootstrap;
@@ -227,7 +245,9 @@ export class NativeScriptPlatformRef extends PlatformRef {
227245
this._bootstrapper().then(
228246
moduleRef => {
229247
bootstrapPromiseCompleted = true;
230-
bootstrapLog("Angular livesync done.");
248+
if (isLogEnabled()) {
249+
bootstrapLog("Angular livesync done.");
250+
}
231251
onAfterLivesync.next({ moduleRef });
232252

233253
if (!autoCreateFrame) {
@@ -238,23 +258,33 @@ export class NativeScriptPlatformRef extends PlatformRef {
238258
},
239259
error => {
240260
bootstrapPromiseCompleted = true;
241-
bootstrapLogError("ERROR LIVESYNC BOOTSTRAPPING ANGULAR");
261+
if (isLogEnabled()) {
262+
bootstrapLogError("ERROR LIVESYNC BOOTSTRAPPING ANGULAR");
263+
}
242264
const errorMessage = error.message + "\n\n" + error.stack;
243-
bootstrapLogError(errorMessage);
265+
if (isLogEnabled()) {
266+
bootstrapLogError(errorMessage);
267+
}
244268

245269
rootContent = this.createErrorUI(errorMessage);
246270

247271
onAfterLivesync.next({ error });
248272
}
249273
);
250274

251-
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
275+
if (isLogEnabled()) {
276+
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
277+
}
252278
(<any>global).Zone.drainMicroTaskQueue();
253-
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
279+
if (isLogEnabled()) {
280+
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
281+
}
254282

255283
if (!bootstrapPromiseCompleted) {
256284
const result = "Livesync bootstrap promise didn't resolve";
257-
bootstrapLogError(result);
285+
if (isLogEnabled()) {
286+
bootstrapLogError(result);
287+
}
258288
rootContent = this.createErrorUI(result);
259289

260290
onAfterLivesync.next({ error: new Error(result) });

0 commit comments

Comments
 (0)