11'use strict' ;
22
33var utils = require ( '../utils' ) ;
4+ var wtf = require ( '../wtf' ) ;
5+ var Zone = require ( '../core' ) . Zone ;
46
5- function patchSetClearFunction ( obj , fnNames ) {
6- fnNames . map ( function ( name ) {
7- return name [ 0 ] . toUpperCase ( ) + name . substr ( 1 ) ;
8- } ) . forEach ( function ( name ) {
7+ function patchSetClearFunction ( window , fnNames ) {
8+ fnNames . forEach ( function ( name ) {
9+ var repeating = name == 'Interval' ;
910 var setName = 'set' + name ;
10- var delegate = obj [ setName ] ;
11-
12- if ( delegate ) {
13- var clearName = 'clear' + name ;
14- var ids = { } ;
11+ var clearName = 'clear' + name ;
12+ var setNative = window [ setName ] ;
13+ var clearNative = window [ clearName ] ;
14+ var ids = { } ;
15+
16+ if ( setNative ) {
17+ var wtfSetEventFn = wtf . createEvent ( 'Zone#' + setName + '(uint32 zone, uint32 id, uint32 delay)' ) ;
18+ var wtfClearEventFn = wtf . createEvent ( 'Zone#' + clearName + '(uint32 zone, uint32 id)' ) ;
19+ var wtfCallbackFn = wtf . createScope ( 'Zone#cb:' + name + '(uint32 zone, uint32 id, uint32 delay)' ) ;
20+
21+ // Forward all calls from the window through the zone.
22+ window [ setName ] = function ( ) {
23+ return global . zone [ setName ] . apply ( global . zone , arguments ) ;
24+ } ;
25+ window [ clearName ] = function ( ) {
26+ return global . zone [ clearName ] . apply ( global . zone , arguments ) ;
27+ } ;
1528
16- var bindArgs = setName === 'setInterval' ? utils . bindArguments : utils . bindArgumentsOnce ;
1729
18- global . zone [ setName ] = function ( fn ) {
19- var id , fnRef = fn ;
20- arguments [ 0 ] = function ( ) {
21- delete ids [ id ] ;
22- return fnRef . apply ( this , arguments ) ;
30+ // Set up zone processing for the set function.
31+ Zone . prototype [ setName ] = function ( fn , delay ) {
32+ // We need to save `fn` in var different then argument. This is because
33+ // in IE9 `argument[0]` and `fn` have same identity, and assigning to
34+ // `argument[0]` changes `fn`.
35+ var callbackFn = fn ;
36+ if ( typeof callbackFn !== 'function' ) {
37+ // force the error by calling the method with wrong args
38+ setNative . apply ( window , arguments ) ;
39+ }
40+ var zone = this ;
41+ var setId = null ;
42+ // wrap the callback function into the zone.
43+ arguments [ 0 ] = function ( ) {
44+ var callbackZone = zone . isRootZone ( ) || isRaf ? zone : zone . fork ( ) ;
45+ var callbackThis = this ;
46+ var callbackArgs = arguments ;
47+ return wtf . leaveScope (
48+ wtfCallbackFn ( callbackZone . $id , setId , delay ) ,
49+ callbackZone . run ( function ( ) {
50+ if ( ! repeating ) {
51+ delete ids [ setId ] ;
52+ callbackZone . removeTask ( callbackFn ) ;
53+ }
54+ return callbackFn . apply ( callbackThis , callbackArgs ) ;
55+ } )
56+ ) ;
2357 } ;
24- var args = bindArgs ( arguments ) ;
25- id = delegate . apply ( obj , args ) ;
26- ids [ id ] = true ;
27- return id ;
58+ if ( repeating ) {
59+ zone . addRepeatingTask ( callbackFn ) ;
60+ } else {
61+ zone . addTask ( callbackFn ) ;
62+ }
63+ setId = setNative . apply ( window , arguments ) ;
64+ ids [ setId ] = callbackFn ;
65+ wtfSetEventFn ( zone . $id , setId , delay ) ;
66+ return setId ;
2867 } ;
2968
30- obj [ setName ] = function ( ) {
31- return global . zone [ setName ] . apply ( this , arguments ) ;
69+ Zone . prototype [ setName + 'Unpatched' ] = function ( ) {
70+ return setNative . apply ( window , arguments ) ;
3271 } ;
3372
34- var clearDelegate = obj [ clearName ] ;
35-
36- global . zone [ clearName ] = function ( id ) {
37- if ( ids [ id ] ) {
73+ // Set up zone processing for the clear function.
74+ Zone . prototype [ clearName ] = function ( id ) {
75+ var scope = wtfClearEventFn ( this . $id , id ) ;
76+ if ( ids . hasOwnProperty ( id ) ) {
77+ var callbackFn = ids [ id ] ;
3878 delete ids [ id ] ;
39- global . zone . dequeueTask ( ) ;
79+ if ( repeating ) {
80+ this . removeRepeatingTask ( callbackFn ) ;
81+ } else {
82+ this . removeTask ( callbackFn ) ;
83+ }
4084 }
41- return clearDelegate . apply ( this , arguments ) ;
85+ return clearNative . apply ( window , arguments ) ;
4286 } ;
4387
44- global . zone [ setName + 'Unpatched' ] = function ( ) {
45- return delegate . apply ( obj , arguments ) ;
88+ Zone . prototype [ clearName + 'Unpatched' ] = function ( ) {
89+ return clearNative . apply ( window , arguments ) ;
4690 } ;
4791
48- global . zone [ clearName + 'Unpatched' ] = function ( ) {
49- return clearDelegate . apply ( obj , arguments ) ;
50- } ;
51-
52- obj [ clearName ] = function ( ) {
53- return global . zone [ clearName ] . apply ( this , arguments ) ;
54- } ;
5592 }
93+ }
94+ fnNames . forEach ( function ( args ) {
95+ patchMacroTaskMethod . apply ( null , args ) ;
5696 } ) ;
5797} ;
5898
@@ -83,26 +123,6 @@ function patchRequestAnimationFrame(obj, fnNames) {
83123 } ) ;
84124} ;
85125
86- function patchSetFunction ( obj , fnNames ) {
87- fnNames . forEach ( function ( name ) {
88- var delegate = obj [ name ] ;
89-
90- if ( delegate ) {
91- global . zone [ name ] = function ( fn ) {
92- arguments [ 0 ] = function ( ) {
93- return fn . apply ( this , arguments ) ;
94- } ;
95- var args = utils . bindArgumentsOnce ( arguments ) ;
96- return delegate . apply ( obj , args ) ;
97- } ;
98-
99- obj [ name ] = function ( ) {
100- return zone [ name ] . apply ( this , arguments ) ;
101- } ;
102- }
103- } ) ;
104- } ;
105-
106126function patchFunction ( obj , fnNames ) {
107127 fnNames . forEach ( function ( name ) {
108128 var delegate = obj [ name ] ;
@@ -119,7 +139,6 @@ function patchFunction(obj, fnNames) {
119139
120140module . exports = {
121141 patchSetClearFunction : patchSetClearFunction ,
122- patchSetFunction : patchSetFunction ,
123142 patchRequestAnimationFrame : patchRequestAnimationFrame ,
124143 patchFunction : patchFunction
125144} ;
0 commit comments