Skip to content

Commit 70d5457

Browse files
authored
refactor: call tracer only after isEnabled check (#1500)
original PR: #958
1 parent 29f2b2f commit 70d5457

13 files changed

+368
-143
lines changed

Diff for: nativescript-angular/animations/animation-player.ts

+19-7
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;

Diff for: nativescript-angular/directives/list-view-comp.ts

+31-11
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
}

Diff for: nativescript-angular/directives/tab-view.ts

+4-2
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
}

Diff for: nativescript-angular/dom-adapter.ts

+8-4
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) {

Diff for: nativescript-angular/platform-common.ts

+46-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
} from "@angular/core";
2222
import { DOCUMENT } from "@angular/common";
2323

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

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

158-
bootstrapLog("NativeScriptPlatform bootstrap started.");
158+
if (isLogEnabled()) {
159+
bootstrapLog("NativeScriptPlatform bootstrap started.");
160+
}
159161
const launchCallback = profile(
160162
"nativescript-angular/platform-common.launchCallback",
161163
(args: LaunchEventData) => {
162-
bootstrapLog("Application launch event fired");
164+
if (isLogEnabled()) {
165+
bootstrapLog("Application launch event fired");
166+
}
163167

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

169-
bootstrapLog(`Angular bootstrap bootstrap done. uptime: ${uptime()}`);
173+
if (isLogEnabled()) {
174+
bootstrapLog(`Angular bootstrap bootstrap done. uptime: ${uptime()}`);
175+
}
170176

171177
if (!autoCreateFrame) {
172178
rootContent = tempAppHostView.content;
@@ -178,20 +184,30 @@ export class NativeScriptPlatformRef extends PlatformRef {
178184
bootstrapPromiseCompleted = true;
179185

180186
const errorMessage = err.message + "\n\n" + err.stack;
181-
bootstrapLogError("ERROR BOOTSTRAPPING ANGULAR");
182-
bootstrapLogError(errorMessage);
187+
if (isLogEnabled()) {
188+
bootstrapLogError("ERROR BOOTSTRAPPING ANGULAR");
189+
}
190+
if (isLogEnabled()) {
191+
bootstrapLogError(errorMessage);
192+
}
183193

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

188-
bootstrapLog("bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
198+
if (isLogEnabled()) {
199+
bootstrapLog("bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
200+
}
189201
(<any>global).Zone.drainMicroTaskQueue();
190-
bootstrapLog("bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
202+
if (isLogEnabled()) {
203+
bootstrapLog("bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
204+
}
191205

192206
if (!bootstrapPromiseCompleted) {
193207
const errorMessage = "Bootstrap promise didn't resolve";
194-
bootstrapLogError(errorMessage);
208+
if (isLogEnabled()) {
209+
bootstrapLogError(errorMessage);
210+
}
195211
rootContent = this.createErrorUI(errorMessage);
196212
}
197213

@@ -205,7 +221,9 @@ export class NativeScriptPlatformRef extends PlatformRef {
205221

206222
@profile
207223
public _livesync() {
208-
bootstrapLog("Angular livesync started.");
224+
if (isLogEnabled()) {
225+
bootstrapLog("Angular livesync started.");
226+
}
209227
onBeforeLivesync.next(lastBootstrappedModule ? lastBootstrappedModule.get() : null);
210228

211229
const autoCreateFrame = !!this.appOptions.createFrameOnBootstrap;
@@ -226,7 +244,9 @@ export class NativeScriptPlatformRef extends PlatformRef {
226244
this._bootstrapper().then(
227245
moduleRef => {
228246
bootstrapPromiseCompleted = true;
229-
bootstrapLog("Angular livesync done.");
247+
if (isLogEnabled()) {
248+
bootstrapLog("Angular livesync done.");
249+
}
230250
onAfterLivesync.next({ moduleRef });
231251

232252
if (!autoCreateFrame) {
@@ -237,23 +257,33 @@ export class NativeScriptPlatformRef extends PlatformRef {
237257
},
238258
error => {
239259
bootstrapPromiseCompleted = true;
240-
bootstrapLogError("ERROR LIVESYNC BOOTSTRAPPING ANGULAR");
260+
if (isLogEnabled()) {
261+
bootstrapLogError("ERROR LIVESYNC BOOTSTRAPPING ANGULAR");
262+
}
241263
const errorMessage = error.message + "\n\n" + error.stack;
242-
bootstrapLogError(errorMessage);
264+
if (isLogEnabled()) {
265+
bootstrapLogError(errorMessage);
266+
}
243267

244268
rootContent = this.createErrorUI(errorMessage);
245269

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

250-
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
274+
if (isLogEnabled()) {
275+
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue. Root: " + rootContent);
276+
}
251277
(<any>global).Zone.drainMicroTaskQueue();
252-
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
278+
if (isLogEnabled()) {
279+
bootstrapLog("livesync bootstrapAction called, draining micro tasks queue finished! Root: " + rootContent);
280+
}
253281

254282
if (!bootstrapPromiseCompleted) {
255283
const result = "Livesync bootstrap promise didn't resolve";
256-
bootstrapLogError(result);
284+
if (isLogEnabled()) {
285+
bootstrapLogError(result);
286+
}
257287
rootContent = this.createErrorUI(result);
258288

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

0 commit comments

Comments
 (0)