@@ -53,7 +53,6 @@ const setGlobal = (
53
53
} ;
54
54
55
55
export default class FakeTimers < TimerRef > {
56
- private _cancelledImmediates ! : Record < string , boolean > ;
57
56
private _cancelledTicks ! : Record < string , boolean > ;
58
57
private _config : StackTraceConfig ;
59
58
private _disposed ?: boolean ;
@@ -105,9 +104,7 @@ export default class FakeTimers<TimerRef> {
105
104
}
106
105
107
106
clearAllTimers ( ) {
108
- this . _immediates . forEach ( immediate =>
109
- this . _fakeClearImmediate ( immediate . uuid ) ,
110
- ) ;
107
+ this . _immediates = [ ] ;
111
108
this . _timers . clear ( ) ;
112
109
}
113
110
@@ -118,7 +115,6 @@ export default class FakeTimers<TimerRef> {
118
115
119
116
reset ( ) {
120
117
this . _cancelledTicks = { } ;
121
- this . _cancelledImmediates = { } ;
122
118
this . _now = 0 ;
123
119
this . _ticks = [ ] ;
124
120
this . _immediates = [ ] ;
@@ -177,10 +173,10 @@ export default class FakeTimers<TimerRef> {
177
173
}
178
174
179
175
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 {
183
177
immediate . callback ( ) ;
178
+ } finally {
179
+ this . _fakeClearImmediate ( immediate . uuid ) ;
184
180
}
185
181
}
186
182
@@ -402,7 +398,9 @@ export default class FakeTimers<TimerRef> {
402
398
}
403
399
404
400
private _fakeClearImmediate ( uuid : TimerID ) {
405
- this . _cancelledImmediates [ uuid ] = true ;
401
+ this . _immediates = this . _immediates . filter (
402
+ immediate => immediate . uuid !== uuid ,
403
+ ) ;
406
404
}
407
405
408
406
private _fakeNextTick ( callback : Callback , ...args : Array < any > ) {
@@ -432,19 +430,20 @@ export default class FakeTimers<TimerRef> {
432
430
return null ;
433
431
}
434
432
435
- const uuid = this . _uuidCounter ++ ;
433
+ const uuid = String ( this . _uuidCounter ++ ) ;
436
434
437
435
this . _immediates . push ( {
438
436
callback : ( ) => callback . apply ( null , args ) ,
439
- uuid : String ( uuid ) ,
437
+ uuid,
440
438
} ) ;
441
439
442
- const cancelledImmediates = this . _cancelledImmediates ;
443
440
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
+ }
448
447
}
449
448
} ) ;
450
449
0 commit comments