OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** | |
6 * *Warning*: this library is **internal**, and APIs are subject to change. | |
7 * | |
8 * Wraps a callback using [wrapMicrotask] and provides the ability to pump all | |
9 * observable objects and [scheduleMicrotask] calls via | |
10 * [performMicrotaskCheckpoint]. | |
11 */ | |
12 library observe.src.microtask; | |
13 | |
14 import 'dart:async' show Completer, runZoned, ZoneSpecification; | |
15 import 'dart:collection'; | |
16 import 'package:observe/observe.dart' show Observable; | |
17 | |
18 // TODO(jmesserly): remove "microtask" from these names and instead import | |
19 // the library "as microtask"? | |
20 | |
21 /** | |
22 * This change pumps events relevant to observers and data-binding tests. | |
23 * This must be used inside an [observeTest]. | |
24 * | |
25 * Executes all pending [scheduleMicrotask] calls on the event loop, as well as | |
26 * performing [Observable.dirtyCheck], until there are no more pending events. | |
27 * It will always dirty check at least once. | |
28 */ | |
29 // TODO(jmesserly): do we want to support nested microtasks similar to nested | |
30 // zones? Instead of a single pending list we'd need one per wrapMicrotask, | |
31 // and [performMicrotaskCheckpoint] would only run pending callbacks | |
32 // corresponding to the innermost wrapMicrotask body. | |
33 void performMicrotaskCheckpoint() { | |
34 Observable.dirtyCheck(); | |
35 | |
36 while (_pending.isNotEmpty) { | |
37 | |
38 for (int len = _pending.length; len > 0 && _pending.isNotEmpty; len--) { | |
39 final callback = _pending.removeFirst(); | |
40 try { | |
41 callback(); | |
42 } catch (e, s) { | |
43 new Completer().completeError(e, s); | |
44 } | |
45 } | |
46 | |
47 Observable.dirtyCheck(); | |
48 } | |
49 } | |
50 | |
51 final Queue<Function> _pending = new Queue<Function>(); | |
52 | |
53 /** | |
54 * Wraps the [body] in a zone that supports [performMicrotaskCheckpoint], | |
55 * and returns the body. | |
56 */ | |
57 // TODO(jmesserly): deprecate? this doesn't add much over runMicrotask. | |
58 wrapMicrotask(body()) => () => runMicrotask(body); | |
59 | |
60 /** | |
61 * Runs the [body] in a zone that supports [performMicrotaskCheckpoint], | |
62 * and returns the result. | |
63 */ | |
64 runMicrotask(body()) => runZoned(() { | |
65 try { | |
66 return body(); | |
67 } finally { | |
68 performMicrotaskCheckpoint(); | |
69 } | |
70 }, zoneSpecification: new ZoneSpecification( | |
71 scheduleMicrotask: (self, parent, zone, callback) => _pending.add(callback)) | |
72 ); | |
OLD | NEW |