@@ -18,6 +18,9 @@ void main() {
18
18
zone.onTurnDone = () {
19
19
log ('onTurnDone' );
20
20
};
21
+ zone.onTurnStart = () {
22
+ log ('onTurnStart' );
23
+ };
21
24
zone.onError = (e, s, ls) => eh (e, s);
22
25
});
23
26
@@ -139,7 +142,7 @@ void main() {
139
142
zone.run (() {
140
143
log ('run' );
141
144
});
142
- expect (log.result ()).toEqual ('run; onTurnDone' );
145
+ expect (log.result ()).toEqual ('onTurnStart; run; onTurnDone' );
143
146
});
144
147
145
148
@@ -148,43 +151,52 @@ void main() {
148
151
});
149
152
150
153
151
- it ('should call onTurnDone for a scheduleMicrotask in onTurnDone' , async ((Logger log) {
154
+ it ('should call onTurnStart before executing a microtask scheduled in onTurnDone as well as '
155
+ 'onTurnDone after executing the task' , async ((Logger log) {
152
156
var ran = false ;
153
157
zone.onTurnDone = () {
158
+ log ('onTurnDone(begin)' );
154
159
if (! ran) {
155
- scheduleMicrotask (() { ran = true ; log ('onTurnAsync ' ); });
160
+ scheduleMicrotask (() { ran = true ; log ('executedMicrotask ' ); });
156
161
}
157
- log ('onTurnDone' );
162
+ log ('onTurnDone(end) ' );
158
163
};
159
164
zone.run (() {
160
165
log ('run' );
161
166
});
162
167
microLeap ();
163
168
164
- expect (log.result ()).toEqual ('run; onTurnDone; onTurnAsync; onTurnDone' );
169
+ expect (log.result ()).toEqual ('onTurnStart; run; onTurnDone(begin); onTurnDone(end); onTurnStart; executedMicrotask; onTurnDone(begin); onTurnDone(end) ' );
165
170
}));
166
171
167
172
168
- it ('should call onTurnDone for a scheduleMicrotask in onTurnDone triggered by a scheduleMicrotask in run' , async ((Logger log) {
173
+ it ('should call onTurnStart and onTurnDone for a scheduleMicrotask in onTurnDone triggered by a scheduleMicrotask in run' , async ((Logger log) {
169
174
var ran = false ;
170
175
zone.onTurnDone = () {
176
+ log ('onTurnDone(begin)' );
171
177
if (! ran) {
172
- scheduleMicrotask (() { ran = true ; log ('onTurnAsync' ); });
178
+ log ('onTurnDone(scheduleMicrotask)' );
179
+ scheduleMicrotask (() {
180
+ ran = true ;
181
+ log ('onTurnDone(executeMicrotask)' );
182
+ });
173
183
}
174
- log ('onTurnDone' );
184
+ log ('onTurnDone(end) ' );
175
185
};
176
186
zone.run (() {
177
- scheduleMicrotask (() { log ('scheduleMicrotask' ); });
178
- log ('run' );
187
+ log ('scheduleMicrotask' );
188
+ scheduleMicrotask (() {
189
+ log ('run(executeMicrotask)' );
190
+ });
179
191
});
180
192
microLeap ();
181
193
182
- expect (log.result ()).toEqual ('run; scheduleMicrotask; onTurnDone; onTurnAsync ; onTurnDone' );
194
+ expect (log.result ()).toEqual ('onTurnStart; scheduleMicrotask; run(executeMicrotask); onTurnDone(begin); onTurnDone( scheduleMicrotask) ; onTurnDone(end); onTurnStart ; onTurnDone(executeMicrotask); onTurnDone(begin); onTurnDone(end) ' );
183
195
}));
184
196
185
197
186
198
187
- it ('should call onTurnDone once after a turn' , async ((Logger log) {
199
+ it ('should call onTurnStart once before a turn and onTurnDone once after the turn' , async ((Logger log) {
188
200
zone.run (() {
189
201
log ('run start' );
190
202
scheduleMicrotask (() {
@@ -194,18 +206,61 @@ void main() {
194
206
});
195
207
microLeap ();
196
208
197
- expect (log.result ()).toEqual ('run start; run end; async; onTurnDone' );
209
+ expect (log.result ()).toEqual ('onTurnStart; run start; run end; async; onTurnDone' );
198
210
}));
199
211
200
212
201
213
it ('should work for Future.value as well' , async ((Logger log) {
202
214
var futureRan = false ;
203
215
zone.onTurnDone = () {
216
+ log ('onTurnDone(begin)' );
204
217
if (! futureRan) {
205
- new Future .value (null ).then ((_) { log ('onTurn future' ); });
218
+ log ('onTurnDone(scheduleFuture)' );
219
+ new Future .value (null ).then ((_) { log ('onTurnDone(executeFuture)' ); });
206
220
futureRan = true ;
207
221
}
208
- log ('onTurnDone' );
222
+ log ('onTurnDone(end)' );
223
+ };
224
+
225
+ zone.run (() {
226
+ log ('run start' );
227
+ new Future .value (null )
228
+ .then ((_) {
229
+ log ('future then' );
230
+ new Future .value (null )
231
+ .then ((_) { log ('future foo' ); });
232
+ return new Future .value (null );
233
+ })
234
+ .then ((_) {
235
+ log ('future bar' );
236
+ });
237
+ log ('run end' );
238
+ });
239
+ microLeap ();
240
+
241
+ expect (log.result ()).toEqual ('onTurnStart; run start; run end; future then; future foo; future bar; onTurnDone(begin); onTurnDone(scheduleFuture); onTurnDone(end); onTurnStart; onTurnDone(executeFuture); onTurnDone(begin); onTurnDone(end)' );
242
+ }));
243
+
244
+ it ('should execute futures scheduled in onTurnStart before Futures scheduled in run' , async ((Logger log) {
245
+ var doneFutureRan = false ;
246
+ var startFutureRan = false ;
247
+ zone.onTurnStart = () {
248
+ log ('onTurnStart(begin)' );
249
+ if (! startFutureRan) {
250
+ log ('onTurnStart(scheduleFuture)' );
251
+ new Future .value (null ).then ((_) { log ('onTurnStart(executeFuture)' ); });
252
+ startFutureRan = true ;
253
+ }
254
+ log ('onTurnStart(end)' );
255
+ };
256
+ zone.onTurnDone = () {
257
+ log ('onTurnDone(begin)' );
258
+ if (! doneFutureRan) {
259
+ log ('onTurnDone(scheduleFuture)' );
260
+ new Future .value (null ).then ((_) { log ('onTurnDone(executeFuture)' ); });
261
+ doneFutureRan = true ;
262
+ }
263
+ log ('onTurnDone(end)' );
209
264
};
210
265
211
266
zone.run (() {
@@ -214,21 +269,21 @@ void main() {
214
269
.then ((_) {
215
270
log ('future then' );
216
271
new Future .value (null )
217
- .then ((_) { log ('future ? ' ); });
272
+ .then ((_) { log ('future foo ' ); });
218
273
return new Future .value (null );
219
274
})
220
275
.then ((_) {
221
- log ('future ? ' );
276
+ log ('future bar ' );
222
277
});
223
278
log ('run end' );
224
279
});
225
280
microLeap ();
226
281
227
- expect (log.result ()).toEqual ('run start; run end; future then; future ? ; future ? ; onTurnDone; onTurn future; onTurnDone' );
282
+ expect (log.result ()).toEqual ('onTurnStart(begin); onTurnStart(scheduleFuture); onTurnStart(end); run start; run end; onTurnStart(executeFuture); future then; future foo ; future bar ; onTurnDone(begin); onTurnDone(scheduleFuture); onTurnDone(end); onTurnStart(begin); onTurnStart(end); onTurnDone(executeFuture); onTurnDone(begin); onTurnDone(end) ' );
228
283
}));
229
284
230
285
231
- it ('should call onTurnDone after each turn' , async ((Logger log) {
286
+ it ('should call onTurnStart and onTurnDone before and after each turn, respectively ' , async ((Logger log) {
232
287
Completer a, b;
233
288
zone.run (() {
234
289
a = new Completer ();
@@ -247,11 +302,11 @@ void main() {
247
302
});
248
303
microLeap ();
249
304
250
- expect (log.result ()).toEqual ('run start; onTurnDone; a then; onTurnDone; b then; onTurnDone' );
305
+ expect (log.result ()).toEqual ('onTurnStart; run start; onTurnDone; onTurnStart; a then; onTurnDone; onTurnStart ; b then; onTurnDone' );
251
306
}));
252
307
253
308
254
- it ('should call onTurnDone after each turn in a chain' , async ((Logger log) {
309
+ it ('should call onTurnStart and onTurnDone before and after (respectively) all turns in a chain' , async ((Logger log) {
255
310
zone.run (() {
256
311
log ('run start' );
257
312
scheduleMicrotask (() {
@@ -264,18 +319,18 @@ void main() {
264
319
});
265
320
microLeap ();
266
321
267
- expect (log.result ()).toEqual ('run start; run end; async1; async2; onTurnDone' );
322
+ expect (log.result ()).toEqual ('onTurnStart; run start; run end; async1; async2; onTurnDone' );
268
323
}));
269
324
270
- it ('should call onTurnDone for futures created outside of run body' , async ((Logger log) {
325
+ it ('should call onTurnStart and onTurnDone for futures created outside of run body' , async ((Logger log) {
271
326
var future = new Future .value (4 ).then ((x) => new Future .value (x));
272
327
zone.run (() {
273
328
future.then ((_) => log ('future then' ));
274
329
log ('zone run' );
275
330
});
276
331
microLeap ();
277
332
278
- expect (log.result ()).toEqual ('zone run; onTurnDone; future then; onTurnDone' );
333
+ expect (log.result ()).toEqual ('onTurnStart; zone run; onTurnDone; onTurnStart ; future then; onTurnDone' );
279
334
}));
280
335
281
336
@@ -286,7 +341,20 @@ void main() {
286
341
throw 'zoneError' ;
287
342
})).toThrow ('zoneError' );
288
343
expect (() => zone.assertInTurn ()).toThrow ();
289
- expect (log.result ()).toEqual ('zone run; onError; onTurnDone' );
344
+ expect (log.result ()).toEqual ('onTurnStart; zone run; onError; onTurnDone' );
345
+ }));
346
+
347
+ it ('should call onTurnDone even if there was an exception in onTurnStart' , async ((Logger log) {
348
+ zone.onError = (e, s, l) => log ('onError' );
349
+ zone.onTurnStart = (){
350
+ log ('onTurnStart' );
351
+ throw 'zoneError' ;
352
+ };
353
+ expect (() => zone.run (() {
354
+ log ('zone run' );
355
+ })).toThrow ('zoneError' );
356
+ expect (() => zone.assertInTurn ()).toThrow ();
357
+ expect (log.result ()).toEqual ('onTurnStart; onError; onTurnDone' );
290
358
}));
291
359
292
360
@@ -303,11 +371,15 @@ void main() {
303
371
microLeap ();
304
372
305
373
expect (() => zone.assertInTurn ()).toThrow ();
306
- expect (log.result ()).toEqual ('zone run; scheduleMicrotask; onError; onTurnDone' );
374
+ expect (log.result ()).toEqual ('onTurnStart; zone run; scheduleMicrotask; onError; onTurnDone' );
307
375
}));
308
376
309
377
it ('should support assertInZone' , async (() {
310
378
var calls = '' ;
379
+ zone.onTurnStart = () {
380
+ zone.assertInZone ();
381
+ calls += 'start;' ;
382
+ };
311
383
zone.onTurnDone = () {
312
384
zone.assertInZone ();
313
385
calls += 'done;' ;
@@ -322,7 +394,7 @@ void main() {
322
394
});
323
395
324
396
microLeap ();
325
- expect (calls).toEqual ('sync;async;done;' );
397
+ expect (calls).toEqual ('start; sync;async;done;' );
326
398
}));
327
399
328
400
it ('should throw outside of the zone' , () {
@@ -335,6 +407,10 @@ void main() {
335
407
336
408
it ('should support assertInTurn' , async (() {
337
409
var calls = '' ;
410
+ zone.onTurnStart = () {
411
+ zone.assertInTurn ();
412
+ calls += 'start;' ;
413
+ };
338
414
zone.onTurnDone = () {
339
415
calls += 'done;' ;
340
416
zone.assertInTurn ();
@@ -349,7 +425,7 @@ void main() {
349
425
});
350
426
351
427
microLeap ();
352
- expect (calls).toEqual ('sync;async;done;' );
428
+ expect (calls).toEqual ('start; sync;async;done;' );
353
429
}));
354
430
355
431
0 commit comments