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 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 captureStackTraces; | |
| 12 import 'future_group.dart'; | 13 import 'future_group.dart'; |
| 13 import 'schedule.dart'; | 14 import 'schedule.dart'; |
| 14 import 'utils.dart'; | 15 import 'utils.dart'; |
| 15 | 16 |
| 16 typedef Future TaskBody(); | 17 typedef Future TaskBody(); |
| 17 | 18 |
| 18 /// A single task to be run as part of a [TaskQueue]. | 19 /// A single task to be run as part of a [TaskQueue]. |
| 19 /// | 20 /// |
| 20 /// 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 |
| 21 /// [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 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 /// task within [queue]; for nested tasks, this is the index within | 55 /// task within [queue]; for nested tasks, this is the index within |
| 55 /// [parent.children]. It's used for debugging when [description] isn't | 56 /// [parent.children]. It's used for debugging when [description] isn't |
| 56 /// provided. | 57 /// provided. |
| 57 int _id; | 58 int _id; |
| 58 | 59 |
| 59 /// A Future that will complete to the return value of [fn] once this task | 60 /// A Future that will complete to the return value of [fn] once this task |
| 60 /// finishes running. | 61 /// finishes running. |
| 61 Future get result => _resultCompleter.future; | 62 Future get result => _resultCompleter.future; |
| 62 final _resultCompleter = new Completer(); | 63 final _resultCompleter = new Completer(); |
| 63 | 64 |
| 64 final stackTrace = new Trace.current(); | 65 final stackTrace = captureStackTraces ? new Trace.current() : null; |
| 65 | 66 |
| 66 Task(fn(), String description, TaskQueue queue) | 67 Task(fn(), String description, TaskQueue queue) |
| 67 : this._(fn, description, queue, null, queue.contents.length); | 68 : this._(fn, description, queue, null, queue.contents.length); |
| 68 | 69 |
| 69 Task._child(fn(), String description, Task parent) | 70 Task._child(fn(), String description, Task parent) |
| 70 : this._(fn, description, parent.queue, parent, parent.children.length); | 71 : this._(fn, description, parent.queue, parent, parent.children.length); |
| 71 | 72 |
| 72 Task._(fn(), this.description, this.queue, this.parent, this._id) { | 73 Task._(fn(), this.description, this.queue, this.parent, this._id) { |
| 73 this.fn = () { | 74 this.fn = () { |
| 74 if (state != TaskState.WAITING) { | 75 if (state != TaskState.WAITING) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, | 115 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, |
| 115 // and we don't want them to short-circuit the other Futures. | 116 // and we don't want them to short-circuit the other Futures. |
| 116 _childGroup.add(task.result.catchError((_) {})); | 117 _childGroup.add(task.result.catchError((_) {})); |
| 117 task.fn(); | 118 task.fn(); |
| 118 return task.result; | 119 return task.result; |
| 119 } | 120 } |
| 120 | 121 |
| 121 String toString() => description == null ? "#$_id" : description; | 122 String toString() => description == null ? "#$_id" : description; |
| 122 | 123 |
| 123 String toStringWithStackTrace() { | 124 String toStringWithStackTrace() { |
| 124 var stackString = prefixLines(terseTraceString(stackTrace)); | 125 var result = "$this"; |
|
nweiz
2013/04/09 21:25:58
I think I prefer `this.toString()` here.
Bob Nystrom
2013/04/10 21:52:00
Done.
| |
| 125 return "$this\n\nStack trace:\n$stackString"; | 126 if (stackTrace != null) { |
| 127 var stackString = prefixLines(terseTraceString(stackTrace)); | |
| 128 result += "\n\nStack trace:\n$stackString"; | |
| 129 } | |
| 130 return result; | |
| 126 } | 131 } |
| 127 | 132 |
| 128 /// Returns a detailed representation of [queue] with this task highlighted. | 133 /// Returns a detailed representation of [queue] with this task highlighted. |
| 129 String generateTree() => queue.generateTree(this); | 134 String generateTree() => queue.generateTree(this); |
| 130 } | 135 } |
| 131 | 136 |
| 132 /// An enum of states for a [Task]. | 137 /// An enum of states for a [Task]. |
| 133 class TaskState { | 138 class TaskState { |
| 134 /// The task is waiting to be run. | 139 /// The task is waiting to be run. |
| 135 static const WAITING = const TaskState._("WAITING"); | 140 static const WAITING = const TaskState._("WAITING"); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 147 final String name; | 152 final String name; |
| 148 | 153 |
| 149 /// Whether the state indicates that the task has finished running. This is | 154 /// Whether the state indicates that the task has finished running. This is |
| 150 /// true for both the [SUCCESS] and [ERROR] states. | 155 /// true for both the [SUCCESS] and [ERROR] states. |
| 151 bool get isDone => this == SUCCESS || this == ERROR; | 156 bool get isDone => this == SUCCESS || this == ERROR; |
| 152 | 157 |
| 153 const TaskState._(this.name); | 158 const TaskState._(this.name); |
| 154 | 159 |
| 155 String toString() => name; | 160 String toString() => name; |
| 156 } | 161 } |
| OLD | NEW |