File tree Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Expand file tree Collapse file tree 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ class NgZone {
39
39
// }); // we should only check for the end of a turn once the top-level run ends
40
40
int _nestedRun = 0 ;
41
41
42
+ bool _inVmTurnDone = false ;
43
+
42
44
/**
43
45
* Associates with this
44
46
*
@@ -143,12 +145,14 @@ class NgZone {
143
145
} finally {
144
146
_nestedRun-- ;
145
147
// If there are no more pending microtasks and we are not in a recursive call, this is the end of a turn
146
- if (_pendingMicrotasks == 0 && _nestedRun == 0 ) {
148
+ if (_pendingMicrotasks == 0 && _nestedRun == 0 && ! _inVmTurnDone ) {
147
149
if (_onTurnDone != null && _hasExecutedCodeInInnerZone) {
148
150
// Trigger onTurnDone at the end of a turn if _innerZone has executed some code
149
151
try {
152
+ _inVmTurnDone = true ;
150
153
parent.run (_innerZone, _onTurnDone);
151
154
} finally {
155
+ _inVmTurnDone = false ;
152
156
_hasExecutedCodeInInnerZone = false ;
153
157
}
154
158
}
Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ export class NgZone {
39
39
// This disabled flag is only here to please cjs tests
40
40
_disabled : boolean ;
41
41
42
+ _inVmTurnDone : boolean = false ;
43
+
42
44
/**
43
45
* Associates with this
44
46
*
@@ -166,11 +168,14 @@ export class NgZone {
166
168
// _nestedRun will be 0 at the end of a macrotasks (it could be > 0 when there are
167
169
// nested calls
168
170
// to run()).
169
- if ( ngZone . _pendingMicrotasks == 0 && ngZone . _nestedRun == 0 ) {
171
+ if ( ngZone . _pendingMicrotasks == 0 && ngZone . _nestedRun == 0 &&
172
+ ! this . _inVmTurnDone ) {
170
173
if ( ngZone . _onTurnDone && ngZone . _hasExecutedCodeInInnerZone ) {
171
174
try {
175
+ this . _inVmTurnDone = true ;
172
176
parentRun . call ( ngZone . _innerZone , ngZone . _onTurnDone ) ;
173
177
} finally {
178
+ this . _inVmTurnDone = false ;
174
179
ngZone . _hasExecutedCodeInInnerZone = false ;
175
180
}
176
181
}
Original file line number Diff line number Diff line change @@ -200,6 +200,30 @@ function commonTests() {
200
200
} , 50 ) ;
201
201
} ) ) ;
202
202
203
+ it ( 'should not run onTurnStart and onTurnDone for nested Zone.run invoked from onTurnDone' ,
204
+ inject ( [ AsyncTestCompleter ] , ( async ) => {
205
+ _zone . initCallbacks ( {
206
+ onTurnDone : ( ) => {
207
+ _log . add ( 'onTurnDone:started' ) ;
208
+ _zone . run ( ( ) => _log . add ( 'nested run' ) )
209
+ _log . add ( 'onTurnDone:finished' ) ;
210
+ }
211
+ } ) ;
212
+
213
+ macroTask ( ( ) => {
214
+ _zone . run ( ( ) => {
215
+ _log . add ( 'start run' ) ;
216
+ } ) ;
217
+ } ) ;
218
+
219
+ macroTask ( ( ) => {
220
+ expect ( _log . result ( ) )
221
+ . toEqual (
222
+ 'start run; onTurnDone:started; nested run; onTurnDone:finished' ) ;
223
+ async . done ( ) ;
224
+ } , 50 ) ;
225
+ } ) ) ;
226
+
203
227
it ( 'should call onTurnStart and onTurnDone before and after each top-level run' ,
204
228
inject ( [ AsyncTestCompleter ] , ( async ) => {
205
229
macroTask ( ( ) => { _zone . run ( _log . fn ( 'run1' ) ) ; } ) ;
You can’t perform that action at this time.
0 commit comments