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