@@ -53,7 +53,6 @@ const setGlobal = (
5353} ;
5454
5555export default class FakeTimers < TimerRef > {
56- private _cancelledImmediates ! : Record < string , boolean > ;
5756 private _cancelledTicks ! : Record < string , boolean > ;
5857 private _config : StackTraceConfig ;
5958 private _disposed ?: boolean ;
@@ -105,9 +104,7 @@ export default class FakeTimers<TimerRef> {
105104 }
106105
107106 clearAllTimers ( ) {
108- this . _immediates . forEach ( immediate =>
109- this . _fakeClearImmediate ( immediate . uuid ) ,
110- ) ;
107+ this . _immediates = [ ] ;
111108 this . _timers . clear ( ) ;
112109 }
113110
@@ -118,7 +115,6 @@ export default class FakeTimers<TimerRef> {
118115
119116 reset ( ) {
120117 this . _cancelledTicks = { } ;
121- this . _cancelledImmediates = { } ;
122118 this . _now = 0 ;
123119 this . _ticks = [ ] ;
124120 this . _immediates = [ ] ;
@@ -177,10 +173,10 @@ export default class FakeTimers<TimerRef> {
177173 }
178174
179175 private _runImmediate ( immediate : Tick ) {
180- if ( ! this . _cancelledImmediates . hasOwnProperty ( immediate . uuid ) ) {
181- // Callback may throw, so update the map prior calling.
182- this . _cancelledImmediates [ immediate . uuid ] = true ;
176+ try {
183177 immediate . callback ( ) ;
178+ } finally {
179+ this . _fakeClearImmediate ( immediate . uuid ) ;
184180 }
185181 }
186182
@@ -402,7 +398,9 @@ export default class FakeTimers<TimerRef> {
402398 }
403399
404400 private _fakeClearImmediate ( uuid : TimerID ) {
405- this . _cancelledImmediates [ uuid ] = true ;
401+ this . _immediates = this . _immediates . filter (
402+ immediate => immediate . uuid !== uuid ,
403+ ) ;
406404 }
407405
408406 private _fakeNextTick ( callback : Callback , ...args : Array < any > ) {
@@ -432,19 +430,20 @@ export default class FakeTimers<TimerRef> {
432430 return null ;
433431 }
434432
435- const uuid = this . _uuidCounter ++ ;
433+ const uuid = String ( this . _uuidCounter ++ ) ;
436434
437435 this . _immediates . push ( {
438436 callback : ( ) => callback . apply ( null , args ) ,
439- uuid : String ( uuid ) ,
437+ uuid,
440438 } ) ;
441439
442- const cancelledImmediates = this . _cancelledImmediates ;
443440 this . _timerAPIs . setImmediate ( ( ) => {
444- if ( ! cancelledImmediates . hasOwnProperty ( uuid ) ) {
445- // Callback may throw, so update the map prior calling.
446- cancelledImmediates [ String ( uuid ) ] = true ;
447- callback . apply ( null , args ) ;
441+ if ( this . _immediates . find ( x => x . uuid === uuid ) ) {
442+ try {
443+ callback . apply ( null , args ) ;
444+ } finally {
445+ this . _fakeClearImmediate ( uuid ) ;
446+ }
448447 }
449448 } ) ;
450449
0 commit comments