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 library task; | 5 library task; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:stack_trace/stack_trace.dart'; | 10 import 'package:stack_trace/stack_trace.dart'; |
11 | 11 |
12 import '../scheduled_test.dart' show currentSchedule; | 12 import '../scheduled_test.dart' show currentSchedule; |
13 import 'future_group.dart'; | 13 import 'future_group.dart'; |
14 import 'schedule.dart'; | 14 import 'schedule.dart'; |
15 import 'utils.dart'; | 15 import 'utils.dart'; |
16 | 16 |
17 typedef Future TaskBody(); | 17 typedef Future TaskBody(); |
18 | 18 |
19 /// A single task to be run as part of a [TaskQueue]. | 19 /// A single task to be run as part of a [TaskQueue]. |
20 /// | 20 /// |
21 /// There are two levels of tasks. **Top-level tasks** are created by calling | 21 /// There are two levels of tasks. **Top-level tasks** are created by calling |
22 /// [TaskQueue.schedule] before the queue in question is running. They're run in | 22 /// [TaskQueue.schedule] before the queue in question is running. They're run in |
23 /// sequence as part of that [TaskQueue]. **Nested tasks** are created by | 23 /// sequence as part of that [TaskQueue]. **Nested tasks** are created by |
24 /// calling [TaskQueue.schedule] once the queue is already running, and are run | 24 /// calling [TaskQueue.schedule] once the queue is already running, and are run |
25 /// in parallel as part of a top-level task. | 25 /// in parallel as part of a top-level task. |
26 class Task { | 26 class Task { |
27 /// The queue to which this [Task] belongs. | 27 /// The queue to which this [Task] belongs. |
28 final TaskQueue queue; | 28 final TaskQueue queue; |
29 | 29 |
30 // TODO(nweiz): make this a read-only view when issue 8321 is fixed. | |
31 /// Child tasks that have been spawned while running this task. This will be | 30 /// Child tasks that have been spawned while running this task. This will be |
32 /// empty if this task is a nested task. | 31 /// empty if this task is a nested task. |
33 final children = new Queue<Task>(); | 32 List<Task> get children => new UnmodifiableListView(_children); |
| 33 final _children = new Queue<Task>(); |
34 | 34 |
35 /// A [FutureGroup] that will complete once all current child tasks are | 35 /// A [FutureGroup] that will complete once all current child tasks are |
36 /// finished running. This will be null if no child tasks are currently | 36 /// finished running. This will be null if no child tasks are currently |
37 /// running. | 37 /// running. |
38 FutureGroup _childGroup; | 38 FutureGroup _childGroup; |
39 | 39 |
40 /// A description of this task. Used for debugging. May be `null`. | 40 /// A description of this task. Used for debugging. May be `null`. |
41 final String description; | 41 final String description; |
42 | 42 |
43 /// The parent task, if this is a nested task that was started while another | 43 /// The parent task, if this is a nested task that was started while another |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 _state = TaskState.ERROR; | 103 _state = TaskState.ERROR; |
104 throw e; | 104 throw e; |
105 }).catchError((_) {}); | 105 }).catchError((_) {}); |
106 } | 106 } |
107 | 107 |
108 /// Run [fn] as a child of this task. Returns a Future that will complete with | 108 /// Run [fn] as a child of this task. Returns a Future that will complete with |
109 /// the result of the child task. This task will not complete until [fn] has | 109 /// the result of the child task. This task will not complete until [fn] has |
110 /// finished. | 110 /// finished. |
111 Future runChild(fn(), String description) { | 111 Future runChild(fn(), String description) { |
112 var task = new Task._child(fn, description, this); | 112 var task = new Task._child(fn, description, this); |
113 children.add(task); | 113 _children.add(task); |
114 if (_childGroup == null || _childGroup.completed) { | 114 if (_childGroup == null || _childGroup.completed) { |
115 _childGroup = new FutureGroup(); | 115 _childGroup = new FutureGroup(); |
116 } | 116 } |
117 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, | 117 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, |
118 // and we don't want them to short-circuit the other Futures. | 118 // and we don't want them to short-circuit the other Futures. |
119 _childGroup.add(task.result.catchError((_) {})); | 119 _childGroup.add(task.result.catchError((_) {})); |
120 task.fn(); | 120 task.fn(); |
121 return task.result; | 121 return task.result; |
122 } | 122 } |
123 | 123 |
(...skipping 30 matching lines...) Expand all Loading... |
154 final String name; | 154 final String name; |
155 | 155 |
156 /// Whether the state indicates that the task has finished running. This is | 156 /// Whether the state indicates that the task has finished running. This is |
157 /// true for both the [SUCCESS] and [ERROR] states. | 157 /// true for both the [SUCCESS] and [ERROR] states. |
158 bool get isDone => this == SUCCESS || this == ERROR; | 158 bool get isDone => this == SUCCESS || this == ERROR; |
159 | 159 |
160 const TaskState._(this.name); | 160 const TaskState._(this.name); |
161 | 161 |
162 String toString() => name; | 162 String toString() => name; |
163 } | 163 } |
OLD | NEW |