55 < title > Counting Pending Tasks</ title >
66 < link rel ="stylesheet " href ="style.css ">
77 < script src ="../zone.js "> </ script >
8- < script src ="../long-stack-trace -zone.js "> </ script >
8+ < script src ="../counting -zone.js "> </ script >
99</ head >
1010< body >
1111
@@ -26,59 +26,32 @@ <h1>Counting Pending Tasks</h1>
2626 /*
2727 * Zone that counts pending async tasks
2828 */
29- var countingZone = ( function ( ) {
30- var count = 0 ,
31- start ;
32- return {
33- _wrap : function ( fn ) {
34- return function ( ) {
35- var ret = fn . apply ( this , arguments ) ;
36- count -= 1 ;
37- zone . _print ( ) ;
38- return ret ;
39- } ;
40- } ,
41-
42- _print : function ( ) {
43- output . innerHTML = 'pending task count: ' + count +
44- ( count === 0 ? ( ' DONE! ' + ( Date . now ( ) - start ) / 1000 + 's' ) : '' ) ;
45- } ,
46-
47- reset : function ( ) {
48- start = 0 ;
49- } ,
50-
51- run : function ( ) {
52- if ( ! start ) {
53- start = Date . now ( ) ;
54- }
55- return zone . run . apply ( this , arguments ) ;
56- } ,
57-
58-
59- onError : function ( e ) {
60- console . log ( e . stack ) ;
61- } ,
62-
63- setTimeout : function ( ) {
64- count += 1 ;
65- zone . _print ( ) ;
66- arguments [ 0 ] = zone . _wrap ( arguments [ 0 ] ) ;
67- return zone . _setTimeout . apply ( this , arguments ) ;
68- } ,
69-
70- _setTimeout : zone . setTimeout
71- } ;
72- } ( ) ) ;
73-
29+ var myCountingZone = zone . fork ( zone . countingZone ) . fork ( {
30+ '+onZoneCreated' : function ( ) {
31+ zone . countingZone . start || ( zone . countingZone . start = Date . now ( ) ) ;
32+ this . print ( ) ;
33+ } ,
34+ '-onZoneLeave' : function ( delegate ) {
35+ this . print ( ) ;
36+ } ,
37+ '+reset' : function ( delegate ) {
38+ zone . countingZone . start = 0 ;
39+ } ,
40+ print : function ( ) {
41+ counter = this . counter ( ) ;
42+ output . innerHTML = counter ?
43+ 'pending task count: ' + counter :
44+ ' DONE! ' + ( Date . now ( ) - zone . countingZone . start ) / 1000 + 's' ;
45+ }
46+ } ) ;
7447
7548 /*
7649 * We want to profile just the actions that are a result of this button, so with
7750 * a single line of code, we can run `main` in the countingZone
7851 */
7952
8053 b1 . addEventListener ( 'click' , function ( ) {
81- zone . fork ( countingZone ) . run ( main ) ;
54+ myCountingZone . run ( main ) ;
8255 } ) ;
8356
8457
0 commit comments