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 */
232import { parse } from "../common/hof" ;
333import { isNumber } from "../common/predicates" ;
434import { Transition } from "../transition/transition" ;
535import { ActiveUIView , ViewConfig } from "../view/interface" ;
636import { stringify , functionToString , maxLength , padString } from "./strings" ;
737
38+ /** @hidden */
839function uiViewString ( viewData ) {
940 if ( ! viewData ) return 'ui-view (defunct)' ;
1041 return `ui-view id#${ viewData . id } , contextual name '${ viewData . name } @${ viewData . creationContext } ', fqn: '${ viewData . fqn } '` ;
1142}
1243
44+ /** @hidden */
1345const viewConfigString = ( viewConfig : ViewConfig ) =>
1446 `ViewConfig targeting ui-view: '${ viewConfig . viewDecl . $uiViewName } @${ viewConfig . viewDecl . $uiViewContextAnchor } ', context: '${ viewConfig . viewDecl . $context . name } '` ;
1547
48+ /** @hidden */
1649function normalizedCat ( input : Category ) : string {
1750 return isNumber ( input ) ? Category [ input ] : Category [ Category [ input ] ] ;
1851}
1952
2053
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+ */
2167export enum Category {
2268 RESOLVE , TRANSITION , HOOK , INVOKE , UIVIEW , VIEWCONFIG
2369}
2470
71+ /**
72+ * Prints UI-Router Transition trace information to the console.
73+ */
2574export class Trace {
2675 approximateDigests : number ;
2776
2877 constructor ( ) {
2978 this . approximateDigests = 0 ;
3079 }
3180
81+ /** @hidden */
3282 private _enabled : { [ key : string ] : boolean } = { } ;
3383
84+ /** @hidden */
3485 private _set ( enabled : boolean , categories : Category [ ] ) {
3586 if ( ! categories . length ) {
3687 categories = Object . keys ( Category )
@@ -40,16 +91,43 @@ export class Trace {
4091 categories . map ( normalizedCat ) . forEach ( category => this . _enabled [ category ] = enabled ) ;
4192 }
4293
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 ) }
47116
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 {
50127 return ! ! this . _enabled [ normalizedCat ( category ) ] ;
51128 }
52129
130+ /** called by ui-router code */
53131 traceTransitionStart ( transition : Transition ) {
54132 if ( ! this . enabled ( Category . TRANSITION ) ) return ;
55133 let tid = transition . $id ,
@@ -58,6 +136,7 @@ export class Trace {
58136 console . log ( `Transition #${ tid } Digest #${ digest } : Started -> ${ transitionStr } ` ) ;
59137 }
60138
139+ /** called by ui-router code */
61140 traceTransitionIgnored ( transition : Transition ) {
62141 if ( ! this . enabled ( Category . TRANSITION ) ) return ;
63142 let tid = transition . $id ,
@@ -66,6 +145,7 @@ export class Trace {
66145 console . log ( `Transition #${ tid } Digest #${ digest } : Ignored <> ${ transitionStr } ` ) ;
67146 }
68147
148+ /** called by ui-router code */
69149 traceHookInvocation ( step , options ) {
70150 if ( ! this . enabled ( Category . HOOK ) ) return ;
71151 let tid = parse ( "transition.$id" ) ( options ) ,
@@ -76,6 +156,7 @@ export class Trace {
76156 console . log ( `Transition #${ tid } Digest #${ digest } : Hook -> ${ event } context: ${ context } , ${ maxLength ( 200 , name ) } ` ) ;
77157 }
78158
159+ /** called by ui-router code */
79160 traceHookResult ( hookResult , transitionResult , transitionOptions ) {
80161 if ( ! this . enabled ( Category . HOOK ) ) return ;
81162 let tid = parse ( "transition.$id" ) ( transitionOptions ) ,
@@ -85,6 +166,7 @@ export class Trace {
85166 console . log ( `Transition #${ tid } Digest #${ digest } : <- Hook returned: ${ maxLength ( 200 , hookResultStr ) } , transition result: ${ maxLength ( 200 , transitionResultStr ) } ` ) ;
86167 }
87168
169+ /** called by ui-router code */
88170 traceResolvePath ( path , options ) {
89171 if ( ! this . enabled ( Category . RESOLVE ) ) return ;
90172 let tid = parse ( "transition.$id" ) ( options ) ,
@@ -94,6 +176,7 @@ export class Trace {
94176 console . log ( `Transition #${ tid } Digest #${ digest } : Resolving ${ pathStr } (${ policyStr } )` ) ;
95177 }
96178
179+ /** called by ui-router code */
97180 traceResolvePathElement ( pathElement , resolvablePromises , options ) {
98181 if ( ! this . enabled ( Category . RESOLVE ) ) return ;
99182 if ( ! resolvablePromises . length ) return ;
@@ -105,6 +188,7 @@ export class Trace {
105188 console . log ( `Transition #${ tid } Digest #${ digest } : Resolve ${ pathElementStr } resolvables: [${ resolvablePromisesStr } ] (${ policyStr } )` ) ;
106189 }
107190
191+ /** called by ui-router code */
108192 traceResolveResolvable ( resolvable , options ) {
109193 if ( ! this . enabled ( Category . RESOLVE ) ) return ;
110194 let tid = parse ( "transition.$id" ) ( options ) ,
@@ -113,6 +197,7 @@ export class Trace {
113197 console . log ( `Transition #${ tid } Digest #${ digest } : Resolving -> ${ resolvableStr } ` ) ;
114198 }
115199
200+ /** called by ui-router code */
116201 traceResolvableResolved ( resolvable , options ) {
117202 if ( ! this . enabled ( Category . RESOLVE ) ) return ;
118203 let tid = parse ( "transition.$id" ) ( options ) ,
@@ -122,6 +207,7 @@ export class Trace {
122207 console . log ( `Transition #${ tid } Digest #${ digest } : <- Resolved ${ resolvableStr } to: ${ maxLength ( 200 , result ) } ` ) ;
123208 }
124209
210+ /** called by ui-router code */
125211 tracePathElementInvoke ( node , fn , deps , options ) {
126212 if ( ! this . enabled ( Category . INVOKE ) ) return ;
127213 let tid = parse ( "transition.$id" ) ( options ) ,
@@ -131,6 +217,7 @@ export class Trace {
131217 console . log ( `Transition #${ tid } Digest #${ digest } : Invoke ${ options . when } : context: ${ stateName } ${ maxLength ( 200 , fnName ) } ` ) ;
132218 }
133219
220+ /** called by ui-router code */
134221 traceError ( error , transition : Transition ) {
135222 if ( ! this . enabled ( Category . TRANSITION ) ) return ;
136223 let tid = transition . $id ,
@@ -139,6 +226,7 @@ export class Trace {
139226 console . log ( `Transition #${ tid } Digest #${ digest } : <- Rejected ${ transitionStr } , reason: ${ error } ` ) ;
140227 }
141228
229+ /** called by ui-router code */
142230 traceSuccess ( finalState , transition : Transition ) {
143231 if ( ! this . enabled ( Category . TRANSITION ) ) return ;
144232 let tid = transition . $id ,
@@ -148,36 +236,52 @@ export class Trace {
148236 console . log ( `Transition #${ tid } Digest #${ digest } : <- Success ${ transitionStr } , final state: ${ state } ` ) ;
149237 }
150238
239+ /** called by ui-router code */
151240 traceUiViewEvent ( event : string , viewData : ActiveUIView , extra = "" ) {
152241 if ( ! this . enabled ( Category . UIVIEW ) ) return ;
153242 console . log ( `ui-view: ${ padString ( 30 , event ) } ${ uiViewString ( viewData ) } ${ extra } ` ) ;
154243 }
155244
245+ /** called by ui-router code */
156246 traceUiViewConfigUpdated ( viewData : ActiveUIView , context ) {
157247 if ( ! this . enabled ( Category . UIVIEW ) ) return ;
158248 this . traceUiViewEvent ( "Updating" , viewData , ` with ViewConfig from context='${ context } '` ) ;
159249 }
160250
251+ /** called by ui-router code */
161252 traceUiViewScopeCreated ( viewData : ActiveUIView , newScope ) {
162253 if ( ! this . enabled ( Category . UIVIEW ) ) return ;
163254 this . traceUiViewEvent ( "Created scope for" , viewData , `, scope #${ newScope . $id } ` ) ;
164255 }
165256
257+ /** called by ui-router code */
166258 traceUiViewFill ( viewData : ActiveUIView , html ) {
167259 if ( ! this . enabled ( Category . UIVIEW ) ) return ;
168260 this . traceUiViewEvent ( "Fill" , viewData , ` with: ${ maxLength ( 200 , html ) } ` ) ;
169261 }
170262
263+ /** called by ui-router code */
171264 traceViewServiceEvent ( event : string , viewConfig : ViewConfig ) {
172265 if ( ! this . enabled ( Category . VIEWCONFIG ) ) return ;
173266 console . log ( `$view.ViewConfig: ${ event } ${ viewConfigString ( viewConfig ) } ` ) ;
174267 }
175268
269+ /** called by ui-router code */
176270 traceViewServiceUiViewEvent ( event : string , viewData : ActiveUIView ) {
177271 if ( ! this . enabled ( Category . VIEWCONFIG ) ) return ;
178272 console . log ( `$view.ViewConfig: ${ event } ${ uiViewString ( viewData ) } ` ) ;
179273 }
180274}
181275
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+ */
182286let trace = new Trace ( ) ;
183287export { trace } ;
0 commit comments