1
- /** @module common */ /** for typedoc */
1
+ /**
2
+ * UI-Router Transition Tracing
3
+ *
4
+ * Enable transition tracing to print transition information to the console, in order to help debug your application.
5
+ * Tracing logs detailed information about each Transition to your console.
6
+ *
7
+ * To enable tracing, import the [[trace]] singleton and enable one or more categories.
8
+ *
9
+ * ES6
10
+ * ```
11
+ *
12
+ * import {trace} from "ui-router-ng2"; // or "angular-ui-router"
13
+ * trace.enable(1, 5); // TRANSITION and VIEWCONFIG
14
+ * ```
15
+ *
16
+ * CJS
17
+ * ```
18
+ *
19
+ * let trace = require("angular-ui-router").trace; // or "ui-router-ng2"
20
+ * trace.enable("TRANSITION", "VIEWCONFIG");
21
+ * ```
22
+ *
23
+ * Globals
24
+ * ```
25
+ *
26
+ * let trace = window["angular-ui-router"].trace; // or "ui-router-ng2"
27
+ * trace.enable(); // Trace everything (very verbose)
28
+ * ```
29
+ *
30
+ * @module trace
31
+ */ /** for typedoc */
2
32
import { parse } from "../common/hof" ;
3
33
import { isNumber } from "../common/predicates" ;
4
34
import { Transition } from "../transition/transition" ;
5
35
import { ActiveUIView , ViewConfig } from "../view/interface" ;
6
36
import { stringify , functionToString , maxLength , padString } from "./strings" ;
7
37
38
+ /** @hidden */
8
39
function uiViewString ( viewData ) {
9
40
if ( ! viewData ) return 'ui-view (defunct)' ;
10
41
return `ui-view id#${ viewData . id } , contextual name '${ viewData . name } @${ viewData . creationContext } ', fqn: '${ viewData . fqn } '` ;
11
42
}
12
43
44
+ /** @hidden */
13
45
const viewConfigString = ( viewConfig : ViewConfig ) =>
14
46
`ViewConfig targeting ui-view: '${ viewConfig . viewDecl . $uiViewName } @${ viewConfig . viewDecl . $uiViewContextAnchor } ', context: '${ viewConfig . viewDecl . $context . name } '` ;
15
47
48
+ /** @hidden */
16
49
function normalizedCat ( input : Category ) : string {
17
50
return isNumber ( input ) ? Category [ input ] : Category [ Category [ input ] ] ;
18
51
}
19
52
20
53
54
+ /**
55
+ * Trace categories
56
+ *
57
+ * [[Trace.enable]] or [[Trace.disable]] a category
58
+ *
59
+ * `trace.enable(Category.TRANSITION)`
60
+ *
61
+ * These can also be provided using a matching string, or position ordinal
62
+ *
63
+ * `trace.enable("TRANSITION")`
64
+ *
65
+ * `trace.enable(1)`
66
+ */
21
67
export enum Category {
22
68
RESOLVE , TRANSITION , HOOK , INVOKE , UIVIEW , VIEWCONFIG
23
69
}
24
70
71
+ /**
72
+ * Prints UI-Router Transition trace information to the console.
73
+ */
25
74
export class Trace {
26
75
approximateDigests : number ;
27
76
28
77
constructor ( ) {
29
78
this . approximateDigests = 0 ;
30
79
}
31
80
81
+ /** @hidden */
32
82
private _enabled : { [ key : string ] : boolean } = { } ;
33
83
84
+ /** @hidden */
34
85
private _set ( enabled : boolean , categories : Category [ ] ) {
35
86
if ( ! categories . length ) {
36
87
categories = Object . keys ( Category )
@@ -40,16 +91,43 @@ export class Trace {
40
91
categories . map ( normalizedCat ) . forEach ( category => this . _enabled [ category ] = enabled ) ;
41
92
}
42
93
43
- // TODO: Document enable(categories)
44
- enable = ( ...categories : Category [ ] ) => this . _set ( true , categories ) ;
45
- // TODO: Document disable(categories)
46
- disable = ( ...categories : Category [ ] ) => this . _set ( false , categories ) ;
94
+ /**
95
+ * Enables a trace [[Category]]
96
+ *
97
+ * ```
98
+ * trace.enable("TRANSITION");
99
+ * ```
100
+ *
101
+ * @param categories categories to enable. If `categories` is omitted, all categories are enabled.
102
+ * Also takes strings (category name) or ordinal (category position)
103
+ */
104
+ enable ( ...categories : Category [ ] ) { this . _set ( true , categories ) }
105
+ /**
106
+ * Disables a trace [[Category]]
107
+ *
108
+ * ```
109
+ * trace.disable("VIEWCONFIG");
110
+ * ```
111
+ *
112
+ * @param categories categories to disable. If `categories` is omitted, all categories are disabled.
113
+ * Also takes strings (category name) or ordinal (category position)
114
+ */
115
+ disable ( ...categories : Category [ ] ) { this . _set ( false , categories ) }
47
116
48
- // TODO: Document enabled(category)
49
- enabled ( category : Category ) {
117
+ /**
118
+ * Retrieves the enabled stateus of a [[Category]]
119
+ *
120
+ * ```
121
+ * trace.enabled("VIEWCONFIG"); // true or false
122
+ * ```
123
+ *
124
+ * @returns boolean true if the category is enabled
125
+ */
126
+ enabled ( category : Category ) : boolean {
50
127
return ! ! this . _enabled [ normalizedCat ( category ) ] ;
51
128
}
52
129
130
+ /** called by ui-router code */
53
131
traceTransitionStart ( transition : Transition ) {
54
132
if ( ! this . enabled ( Category . TRANSITION ) ) return ;
55
133
let tid = transition . $id ,
@@ -58,6 +136,7 @@ export class Trace {
58
136
console . log ( `Transition #${ tid } Digest #${ digest } : Started -> ${ transitionStr } ` ) ;
59
137
}
60
138
139
+ /** called by ui-router code */
61
140
traceTransitionIgnored ( transition : Transition ) {
62
141
if ( ! this . enabled ( Category . TRANSITION ) ) return ;
63
142
let tid = transition . $id ,
@@ -66,6 +145,7 @@ export class Trace {
66
145
console . log ( `Transition #${ tid } Digest #${ digest } : Ignored <> ${ transitionStr } ` ) ;
67
146
}
68
147
148
+ /** called by ui-router code */
69
149
traceHookInvocation ( step , options ) {
70
150
if ( ! this . enabled ( Category . HOOK ) ) return ;
71
151
let tid = parse ( "transition.$id" ) ( options ) ,
@@ -76,6 +156,7 @@ export class Trace {
76
156
console . log ( `Transition #${ tid } Digest #${ digest } : Hook -> ${ event } context: ${ context } , ${ maxLength ( 200 , name ) } ` ) ;
77
157
}
78
158
159
+ /** called by ui-router code */
79
160
traceHookResult ( hookResult , transitionResult , transitionOptions ) {
80
161
if ( ! this . enabled ( Category . HOOK ) ) return ;
81
162
let tid = parse ( "transition.$id" ) ( transitionOptions ) ,
@@ -85,6 +166,7 @@ export class Trace {
85
166
console . log ( `Transition #${ tid } Digest #${ digest } : <- Hook returned: ${ maxLength ( 200 , hookResultStr ) } , transition result: ${ maxLength ( 200 , transitionResultStr ) } ` ) ;
86
167
}
87
168
169
+ /** called by ui-router code */
88
170
traceResolvePath ( path , options ) {
89
171
if ( ! this . enabled ( Category . RESOLVE ) ) return ;
90
172
let tid = parse ( "transition.$id" ) ( options ) ,
@@ -94,6 +176,7 @@ export class Trace {
94
176
console . log ( `Transition #${ tid } Digest #${ digest } : Resolving ${ pathStr } (${ policyStr } )` ) ;
95
177
}
96
178
179
+ /** called by ui-router code */
97
180
traceResolvePathElement ( pathElement , resolvablePromises , options ) {
98
181
if ( ! this . enabled ( Category . RESOLVE ) ) return ;
99
182
if ( ! resolvablePromises . length ) return ;
@@ -105,6 +188,7 @@ export class Trace {
105
188
console . log ( `Transition #${ tid } Digest #${ digest } : Resolve ${ pathElementStr } resolvables: [${ resolvablePromisesStr } ] (${ policyStr } )` ) ;
106
189
}
107
190
191
+ /** called by ui-router code */
108
192
traceResolveResolvable ( resolvable , options ) {
109
193
if ( ! this . enabled ( Category . RESOLVE ) ) return ;
110
194
let tid = parse ( "transition.$id" ) ( options ) ,
@@ -113,6 +197,7 @@ export class Trace {
113
197
console . log ( `Transition #${ tid } Digest #${ digest } : Resolving -> ${ resolvableStr } ` ) ;
114
198
}
115
199
200
+ /** called by ui-router code */
116
201
traceResolvableResolved ( resolvable , options ) {
117
202
if ( ! this . enabled ( Category . RESOLVE ) ) return ;
118
203
let tid = parse ( "transition.$id" ) ( options ) ,
@@ -122,6 +207,7 @@ export class Trace {
122
207
console . log ( `Transition #${ tid } Digest #${ digest } : <- Resolved ${ resolvableStr } to: ${ maxLength ( 200 , result ) } ` ) ;
123
208
}
124
209
210
+ /** called by ui-router code */
125
211
tracePathElementInvoke ( node , fn , deps , options ) {
126
212
if ( ! this . enabled ( Category . INVOKE ) ) return ;
127
213
let tid = parse ( "transition.$id" ) ( options ) ,
@@ -131,6 +217,7 @@ export class Trace {
131
217
console . log ( `Transition #${ tid } Digest #${ digest } : Invoke ${ options . when } : context: ${ stateName } ${ maxLength ( 200 , fnName ) } ` ) ;
132
218
}
133
219
220
+ /** called by ui-router code */
134
221
traceError ( error , transition : Transition ) {
135
222
if ( ! this . enabled ( Category . TRANSITION ) ) return ;
136
223
let tid = transition . $id ,
@@ -139,6 +226,7 @@ export class Trace {
139
226
console . log ( `Transition #${ tid } Digest #${ digest } : <- Rejected ${ transitionStr } , reason: ${ error } ` ) ;
140
227
}
141
228
229
+ /** called by ui-router code */
142
230
traceSuccess ( finalState , transition : Transition ) {
143
231
if ( ! this . enabled ( Category . TRANSITION ) ) return ;
144
232
let tid = transition . $id ,
@@ -148,36 +236,52 @@ export class Trace {
148
236
console . log ( `Transition #${ tid } Digest #${ digest } : <- Success ${ transitionStr } , final state: ${ state } ` ) ;
149
237
}
150
238
239
+ /** called by ui-router code */
151
240
traceUiViewEvent ( event : string , viewData : ActiveUIView , extra = "" ) {
152
241
if ( ! this . enabled ( Category . UIVIEW ) ) return ;
153
242
console . log ( `ui-view: ${ padString ( 30 , event ) } ${ uiViewString ( viewData ) } ${ extra } ` ) ;
154
243
}
155
244
245
+ /** called by ui-router code */
156
246
traceUiViewConfigUpdated ( viewData : ActiveUIView , context ) {
157
247
if ( ! this . enabled ( Category . UIVIEW ) ) return ;
158
248
this . traceUiViewEvent ( "Updating" , viewData , ` with ViewConfig from context='${ context } '` ) ;
159
249
}
160
250
251
+ /** called by ui-router code */
161
252
traceUiViewScopeCreated ( viewData : ActiveUIView , newScope ) {
162
253
if ( ! this . enabled ( Category . UIVIEW ) ) return ;
163
254
this . traceUiViewEvent ( "Created scope for" , viewData , `, scope #${ newScope . $id } ` ) ;
164
255
}
165
256
257
+ /** called by ui-router code */
166
258
traceUiViewFill ( viewData : ActiveUIView , html ) {
167
259
if ( ! this . enabled ( Category . UIVIEW ) ) return ;
168
260
this . traceUiViewEvent ( "Fill" , viewData , ` with: ${ maxLength ( 200 , html ) } ` ) ;
169
261
}
170
262
263
+ /** called by ui-router code */
171
264
traceViewServiceEvent ( event : string , viewConfig : ViewConfig ) {
172
265
if ( ! this . enabled ( Category . VIEWCONFIG ) ) return ;
173
266
console . log ( `$view.ViewConfig: ${ event } ${ viewConfigString ( viewConfig ) } ` ) ;
174
267
}
175
268
269
+ /** called by ui-router code */
176
270
traceViewServiceUiViewEvent ( event : string , viewData : ActiveUIView ) {
177
271
if ( ! this . enabled ( Category . VIEWCONFIG ) ) return ;
178
272
console . log ( `$view.ViewConfig: ${ event } ${ uiViewString ( viewData ) } ` ) ;
179
273
}
180
274
}
181
275
276
+ /**
277
+ * The [[Trace]] singleton
278
+ *
279
+ * @example
280
+ * ```js
281
+ *
282
+ * import {trace} from "angular-ui-router";
283
+ * trace.enable(1, 5);
284
+ * ```
285
+ */
182
286
let trace = new Trace ( ) ;
183
287
export { trace } ;
0 commit comments