| 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 for (int len = _pending.length; len > 0 && _pending.isNotEmpty; len--) { |
| 34 final callback = _pending.removeFirst(); |
| 35 try { | 35 try { |
| 36 callback(); | 36 callback(); |
| 37 } catch (e, s) { | 37 } catch (e, s) { |
| 38 new Completer().completeError(e, s); | 38 new Completer().completeError(e, s); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 | 41 |
| 42 Observable.dirtyCheck(); | 42 Observable.dirtyCheck(); |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 List<Function> _pending = []; | 46 final Queue<Function> _pending = new Queue<Function>(); |
| 47 | 47 |
| 48 /** | 48 /** |
| 49 * Wraps the [testCase] in a zone that supports [performMicrotaskCheckpoint], | 49 * Wraps the [body] in a zone that supports [performMicrotaskCheckpoint], |
| 50 * and returns the test case. | 50 * and returns the test case. |
| 51 */ | 51 */ |
| 52 wrapMicrotask(void testCase()) { | 52 // TODO(jmesserly): do we want to support nested microtasks similar to nested |
| 53 // zones? Instead of a single pending list we'd need one per wrapMicrotask, |
| 54 // and [performMicrotaskCheckpoint] would only run pending callbacks |
| 55 // corresponding to the innermost wrapMicrotask body. |
| 56 wrapMicrotask(void body()) { |
| 53 return () { | 57 return () { |
| 54 runZonedExperimental(() { | 58 runZonedExperimental(() { |
| 55 try { | 59 try { |
| 56 testCase(); | 60 body(); |
| 57 } finally { | 61 } finally { |
| 58 performMicrotaskCheckpoint(); | 62 performMicrotaskCheckpoint(); |
| 59 } | 63 } |
| 60 }, onRunAsync: (callback) => _pending.add(callback)); | 64 }, onRunAsync: (callback) => _pending.add(callback)); |
| 61 }; | 65 }; |
| 62 } | 66 } |
| OLD | NEW |