1- var timer = null ;
2- var stepLimit = 1e6 ;
3-
41var Tracer = function ( module ) {
52 this . module = module || Tracer ;
6- this . traces = [ ] ;
7- this . traceIndex = - 1 ;
8- this . stepCnt = 0 ;
93 this . capsule = tm . allocate ( this ) ;
104
115 $ . extend ( this , this . capsule ) ;
126 return this . new ;
137} ;
148
159Tracer . prototype = {
16- resize : function ( ) {
17- } ,
18- clear : function ( ) {
19- } ,
20- reset : function ( ) {
21- this . traces = [ ] ;
22- this . stepCnt = 0 ;
23- if ( timer ) clearTimeout ( timer ) ;
24- $ ( '#tab_trace .wrapper' ) . empty ( ) ;
25- this . clear ( ) ;
26- } ,
2710 _setData : function ( arguments ) {
2811 var data = JSON . stringify ( arguments ) ;
2912 if ( ! this . new && this . lastData == data ) return true ;
3013 this . capsule . lastData = data ;
3114 return false ;
3215 } ,
33- pushStep : function ( step , delay ) {
34- if ( this . stepCnt ++ > stepLimit ) throw "Tracer's stack overflow" ;
35- var len = this . traces . length ;
36- var last = [ ] ;
37- if ( len == 0 ) {
38- this . traces . push ( last ) ;
39- } else {
40- last = this . traces [ len - 1 ] ;
41- }
42- last . push ( step ) ;
43- if ( delay ) this . traces . push ( [ ] ) ;
44- } ,
4516 _sleep : function ( duration ) {
46- this . pushStep ( { type : 'sleep' , duration : duration } , true ) ;
47- } ,
48- _print : function ( msg , delay ) {
49- this . pushStep ( { type : 'print' , msg : msg } , delay ) ;
17+ tm . pushStep ( this . capsule , { type : 'sleep' , duration : duration } ) ;
5018 } ,
5119 _clear : function ( ) {
52- this . pushStep ( { type : 'clear' } , true ) ;
53- } ,
54- visualize : function ( ) {
55- $ ( '#btn_trace' ) . click ( ) ;
56- this . traceIndex = - 1 ;
57- this . resumeStep ( ) ;
20+ tm . pushStep ( this . capsule , { type : 'clear' } ) ;
5821 } ,
59- pauseStep : function ( ) {
60- if ( this . traceIndex < 0 ) return ;
61- tm . pause = true ;
62- if ( timer ) clearTimeout ( timer ) ;
63- $ ( '#btn_pause' ) . addClass ( 'active' ) ;
22+ _next : function ( ) {
23+ tm . newStep ( ) ;
6424 } ,
65- resumeStep : function ( ) {
66- tm . pause = false ;
67- this . step ( this . traceIndex + 1 ) ;
68- $ ( '#btn_pause' ) . removeClass ( 'active' ) ;
69- } ,
70- step : function ( i , options ) {
71- var tracer = this ;
72-
73- if ( isNaN ( i ) || i >= this . traces . length || i < 0 ) return ;
74- options = options || { } ;
75-
76- this . traceIndex = i ;
77- var trace = this . traces [ i ] ;
78- var sleepDuration = 0 ;
79- trace . forEach ( function ( step ) {
80- switch ( step . type ) {
81- case 'sleep' :
82- sleepDuration = step . duration ;
83- break ;
84- case 'print' :
85- tracer . printTrace ( step . msg ) ;
86- break ;
87- case 'clear' :
88- tracer . clear ( ) ;
89- tracer . printTrace ( 'clear traces' ) ;
90- break ;
91- default :
92- tracer . module . prototype . processStep . call ( tracer , step , options ) ;
93- }
94- } ) ;
95- if ( ! options . virtual ) {
96- this . refresh ( ) ;
97- this . scrollToEnd ( Math . min ( 50 , tm . interval ) ) ;
25+ processStep : function ( step , options ) {
26+ switch ( step . type ) {
27+ case 'clear' :
28+ this . clear ( ) ;
29+ break ;
9830 }
99- if ( tm . pause ) return ;
100- timer = setTimeout ( function ( ) {
101- tracer . step ( i + 1 , options ) ;
102- } , sleepDuration || tm . interval ) ;
10331 } ,
104- refresh : function ( ) {
32+ resize : function ( ) {
10533 } ,
106- prevStep : function ( ) {
107- this . step ( this . traceIndex - 1 ) ;
34+ refresh : function ( ) {
10835 } ,
109- nextStep : function ( ) {
110- this . step ( this . traceIndex + 1 ) ;
36+ clear : function ( ) {
11137 } ,
11238 mousedown : function ( e ) {
11339 } ,
@@ -116,11 +42,5 @@ Tracer.prototype = {
11642 mouseup : function ( e ) {
11743 } ,
11844 mousewheel : function ( e ) {
119- } ,
120- printTrace : function ( message ) {
121- $ ( '#tab_trace .wrapper' ) . append ( $ ( '<span>' ) . append ( message + '<br/>' ) ) ;
122- } ,
123- scrollToEnd : function ( duration ) {
124- $ ( '#tab_trace' ) . animate ( { scrollTop : $ ( '#tab_trace' ) [ 0 ] . scrollHeight } , duration ) ;
12545 }
12646} ;
0 commit comments