@@ -66,11 +66,16 @@ let patchedTimers: {
6666 setInterval : typeof setInterval ,
6767 clearTimeout : typeof clearTimeout ,
6868 clearInterval : typeof clearInterval ,
69+ nativeSetTimeout : typeof setTimeout ,
70+ nativeClearTimeout : typeof clearTimeout ,
6971} | undefined ;
7072
73+ const timeoutCallback = function ( ) { } ;
74+
7175class Scheduler {
7276 // Next scheduler id.
73- public static nextId : number = 1 ;
77+ public static nextNodeJSId : number = 1 ;
78+ public static nextId : number = - 1 ;
7479
7580 // Scheduler queue with the tuple of end time and callback function - sorted by end time.
7681 private _schedulerQueue : ScheduledFunction [ ] = [ ] ;
@@ -83,6 +88,17 @@ class Scheduler {
8388
8489 constructor ( ) { }
8590
91+ static getNextId ( ) {
92+ const id = patchedTimers ! . nativeSetTimeout . call ( global , timeoutCallback , 0 ) ;
93+ patchedTimers ! . nativeClearTimeout . call ( global , id ) ;
94+ if ( typeof id === 'number' ) {
95+ return id ;
96+ }
97+ // in NodeJS, we just use a number for fakeAsync, since it will not
98+ // conflict with native TimeoutId
99+ return Scheduler . nextNodeJSId ++ ;
100+ }
101+
86102 getCurrentTickTime ( ) {
87103 return this . _currentTickTime ;
88104 }
@@ -116,7 +132,8 @@ class Scheduler {
116132 } ,
117133 ...options
118134 } ;
119- let currentId = options . id ! < 0 ? Scheduler . nextId ++ : options . id ! ;
135+ let currentId = options . id ! < 0 ? Scheduler . nextId : options . id ! ;
136+ Scheduler . nextId = Scheduler . getNextId ( ) ;
120137 let endTime = this . _currentTickTime + delay ;
121138
122139 // Insert so that scheduler queue remains sorted by end time.
@@ -877,6 +894,10 @@ export function patchFakeAsyncTest(Zone: ZoneType): void {
877894 setTimeout : global . setTimeout ,
878895 setInterval : global . setInterval ,
879896 clearTimeout : global . clearTimeout ,
880- clearInterval : global . clearInterval
897+ clearInterval : global . clearInterval ,
898+ nativeSetTimeout : global [ Zone . __symbol__ ( 'setTimeout' ) ] ,
899+ nativeClearTimeout : global [ Zone . __symbol__ ( 'clearTimeout' ) ] ,
881900 } ;
901+
902+ Scheduler . nextId = Scheduler . getNextId ( ) ;
882903}
0 commit comments