Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * *Warning*: this library is **internal**, and APIs are subject to change. | 6 * *Warning*: this library is **internal**, and APIs are subject to change. |
| 7 * | 7 * |
| 8 * Wraps a callback using [wrapMicrotask] and provides the ability to pump all | 8 * Wraps a callback using [wrapMicrotask] and provides the ability to pump all |
| 9 * observable objects and [runAsync] calls via [performMicrotaskCheckpoint]. | 9 * observable objects and [runAsync] calls via [performMicrotaskCheckpoint]. |
| 10 */ | 10 */ |
| 11 library observe.src.microtask; | 11 library observe.src.microtask; |
| 12 | 12 |
| 13 import 'dart:async' show Completer, runZonedExperimental; | 13 import 'dart:async' show Completer, runZonedExperimental; |
| 14 import 'dart:collection'; | |
| 14 import 'package:observe/observe.dart' show Observable; | 15 import 'package:observe/observe.dart' show Observable; |
| 15 | 16 |
| 16 // TODO(jmesserly): remove "microtask" from these names and instead import | 17 // TODO(jmesserly): remove "microtask" from these names and instead import |
| 17 // the library "as microtask"? | 18 // the library "as microtask"? |
| 18 | 19 |
| 19 /** | 20 /** |
| 20 * This change pumps events relevant to observers and data-binding tests. | 21 * This change pumps events relevant to observers and data-binding tests. |
| 21 * This must be used inside an [observeTest]. | 22 * This must be used inside an [observeTest]. |
| 22 * | 23 * |
| 23 * Executes all pending [runAsync] calls on the event loop, as well as | 24 * Executes all pending [runAsync] calls on the event loop, as well as |
| 24 * performing [Observable.dirtyCheck], until there are no more pending events. | 25 * performing [Observable.dirtyCheck], until there are no more pending events. |
| 25 * It will always dirty check at least once. | 26 * It will always dirty check at least once. |
| 26 */ | 27 */ |
| 27 void performMicrotaskCheckpoint() { | 28 void performMicrotaskCheckpoint() { |
| 28 Observable.dirtyCheck(); | 29 Observable.dirtyCheck(); |
| 29 | 30 |
| 30 while (_pending.length > 0) { | 31 while (_pending.isNotEmpty) { |
| 31 var pending = _pending; | |
| 32 _pending = []; | |
| 33 | 32 |
| 34 for (var callback in pending) { | 33 final startLength = _pending.length; |
|
Siggi Cherem (dart-lang)
2013/08/28 19:46:51
delete (unused variable)
Jennifer Messerly
2013/08/28 19:56:04
Done.
| |
| 34 for (int len = _pending.length; len > 0 && _pending.isNotEmpty; len--) { | |
| 35 final callback = _pending.removeFirst(); | |
| 35 try { | 36 try { |
| 36 callback(); | 37 callback(); |
| 37 } catch (e, s) { | 38 } catch (e, s) { |
| 38 new Completer().completeError(e, s); | 39 new Completer().completeError(e, s); |
| 39 } | 40 } |
| 40 } | 41 } |
| 41 | 42 |
| 42 Observable.dirtyCheck(); | 43 Observable.dirtyCheck(); |
| 43 } | 44 } |
| 44 } | 45 } |
| 45 | 46 |
| 46 List<Function> _pending = []; | 47 final Queue<Function> _pending = new Queue<Function>(); |
| 47 | 48 |
| 48 /** | 49 /** |
| 49 * Wraps the [testCase] in a zone that supports [performMicrotaskCheckpoint], | 50 * Wraps the [body] in a zone that supports [performMicrotaskCheckpoint], |
| 50 * and returns the test case. | 51 * and returns the test case. |
| 51 */ | 52 */ |
| 52 wrapMicrotask(void testCase()) { | 53 // TODO(jmesserly): do we want to support nested microtasks similar to nested |
| 54 // zones? Instead of a single pending list we'd need one per wrapMicrotask, | |
| 55 // and [performMicrotaskCheckpoint] would only run pending callbacks | |
| 56 // corresponding to the innermost wrapMicrotask body. | |
| 57 wrapMicrotask(void body()) { | |
| 53 return () { | 58 return () { |
| 54 runZonedExperimental(() { | 59 runZonedExperimental(() { |
| 55 try { | 60 try { |
| 56 testCase(); | 61 body(); |
| 57 } finally { | 62 } finally { |
| 58 performMicrotaskCheckpoint(); | 63 performMicrotaskCheckpoint(); |
| 59 } | 64 } |
| 60 }, onRunAsync: (callback) => _pending.add(callback)); | 65 }, onRunAsync: (callback) => _pending.add(callback)); |
| 61 }; | 66 }; |
| 62 } | 67 } |
| OLD | NEW |