| 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'; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 /// task within [queue]; for nested tasks, this is the index within | 54 /// task within [queue]; for nested tasks, this is the index within |
| 55 /// [parent.children]. It's used for debugging when [description] isn't | 55 /// [parent.children]. It's used for debugging when [description] isn't |
| 56 /// provided. | 56 /// provided. |
| 57 int _id; | 57 int _id; |
| 58 | 58 |
| 59 /// 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 |
| 60 /// finishes running. | 60 /// finishes running. |
| 61 Future get result => _resultCompleter.future; | 61 Future get result => _resultCompleter.future; |
| 62 final _resultCompleter = new Completer(); | 62 final _resultCompleter = new Completer(); |
| 63 | 63 |
| 64 final Trace stackTrace; | 64 final Chain stackChain; |
| 65 | 65 |
| 66 Task(fn(), String description, TaskQueue queue) | 66 Task(fn(), String description, TaskQueue queue) |
| 67 : this._(fn, description, queue, null, queue.contents.length); | 67 : this._(fn, description, queue, null, queue.contents.length); |
| 68 | 68 |
| 69 Task._child(fn(), String description, Task parent) | 69 Task._child(fn(), String description, Task parent) |
| 70 : this._(fn, description, parent.queue, parent, parent.children.length); | 70 : this._(fn, description, parent.queue, parent, parent.children.length); |
| 71 | 71 |
| 72 Task._(fn(), this.description, TaskQueue queue, this.parent, this._id) | 72 Task._(fn(), this.description, TaskQueue queue, this.parent, this._id) |
| 73 : queue = queue, | 73 : queue = queue, |
| 74 stackTrace = new Trace.current() { | 74 stackChain = new Chain.current() { |
| 75 this.fn = () { | 75 this.fn = () { |
| 76 if (state != TaskState.WAITING) { | 76 if (state != TaskState.WAITING) { |
| 77 throw new StateError("Can't run $state task '$this'."); | 77 throw new StateError("Can't run $state task '$this'."); |
| 78 } | 78 } |
| 79 | 79 |
| 80 _state = TaskState.RUNNING; | 80 _state = TaskState.RUNNING; |
| 81 var future = new Future.value().then((_) => fn()) | 81 var future = new Future.value().then((_) => fn()) |
| 82 .whenComplete(() { | 82 .whenComplete(() { |
| 83 if (_childGroup == null || _childGroup.completed) return null; | 83 if (_childGroup == null || _childGroup.completed) return null; |
| 84 return _childGroup.future; | 84 return _childGroup.future; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, | 116 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, |
| 117 // and we don't want them to short-circuit the other Futures. | 117 // and we don't want them to short-circuit the other Futures. |
| 118 _childGroup.add(task.result.catchError((_) {})); | 118 _childGroup.add(task.result.catchError((_) {})); |
| 119 task.fn(); | 119 task.fn(); |
| 120 return task.result; | 120 return task.result; |
| 121 } | 121 } |
| 122 | 122 |
| 123 String toString() => description == null ? "#$_id" : description; | 123 String toString() => description == null ? "#$_id" : description; |
| 124 | 124 |
| 125 String toStringWithStackTrace() { | 125 String toStringWithStackTrace() { |
| 126 var result = toString(); | 126 var stackString = prefixLines(terseTraceString(stackChain)); |
| 127 if (stackTrace != null) { | 127 return "$this\n\nStack chain:\n$stackString"; |
| 128 var stackString = prefixLines(terseTraceString(stackTrace)); | |
| 129 result += "\n\nStack trace:\n$stackString"; | |
| 130 } | |
| 131 return result; | |
| 132 } | 128 } |
| 133 | 129 |
| 134 /// Returns a detailed representation of [queue] with this task highlighted. | 130 /// Returns a detailed representation of [queue] with this task highlighted. |
| 135 String generateTree() => queue.generateTree(this); | 131 String generateTree() => queue.generateTree(this); |
| 136 } | 132 } |
| 137 | 133 |
| 138 /// An enum of states for a [Task]. | 134 /// An enum of states for a [Task]. |
| 139 class TaskState { | 135 class TaskState { |
| 140 /// The task is waiting to be run. | 136 /// The task is waiting to be run. |
| 141 static const WAITING = const TaskState._("WAITING"); | 137 static const WAITING = const TaskState._("WAITING"); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 153 final String name; | 149 final String name; |
| 154 | 150 |
| 155 /// Whether the state indicates that the task has finished running. This is | 151 /// Whether the state indicates that the task has finished running. This is |
| 156 /// true for both the [SUCCESS] and [ERROR] states. | 152 /// true for both the [SUCCESS] and [ERROR] states. |
| 157 bool get isDone => this == SUCCESS || this == ERROR; | 153 bool get isDone => this == SUCCESS || this == ERROR; |
| 158 | 154 |
| 159 const TaskState._(this.name); | 155 const TaskState._(this.name); |
| 160 | 156 |
| 161 String toString() => name; | 157 String toString() => name; |
| 162 } | 158 } |
| OLD | NEW |