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 | 9 |
| 10 import 'future_group.dart'; | |
| 9 import 'schedule.dart'; | 11 import 'schedule.dart'; |
| 10 import 'utils.dart'; | 12 import 'utils.dart'; |
| 11 | 13 |
| 12 typedef Future TaskBody(); | 14 typedef Future TaskBody(); |
| 13 | 15 |
| 14 /// A single task to be run as part of a [TaskQueue]. | 16 /// A single task to be run as part of a [TaskQueue]. |
| 15 class Task { | 17 class Task { |
| 16 /// The queue to which this [Task] belongs. | 18 /// The queue to which this [Task] belongs. |
| 17 final TaskQueue queue; | 19 final TaskQueue queue; |
| 18 | 20 |
| 21 // TODO(nweiz): make this a read-only view when issue 8321 is fixed. | |
| 22 /// Child tasks that have been spawned while running this task. This will be | |
| 23 /// empty if this task is itself a child task. | |
|
Bob Nystrom
2013/02/19 23:15:04
Does this mean child tasks cannot have their own c
nweiz
2013/02/20 00:23:12
Correct.
Bob Nystrom
2013/02/20 17:30:25
Let's make sure that's documented somewhere. When
nweiz
2013/02/20 23:54:58
Done.
| |
| 24 final children = new Queue<Task>(); | |
| 25 | |
| 26 /// A [FutureGroup] that will complete once all current child tasks are | |
| 27 /// finished running. This will be null if no child tasks are currently | |
| 28 /// running. | |
| 29 FutureGroup _childGroup; | |
| 30 | |
| 19 /// A description of this task. Used for debugging. May be `null`. | 31 /// A description of this task. Used for debugging. May be `null`. |
| 20 final String description; | 32 final String description; |
| 21 | 33 |
| 34 /// The parent task, if this is a nested task that was started while another | |
| 35 /// task was running. This will be `null` for top-level tasks. | |
| 36 final Task parent; | |
| 37 | |
| 22 /// The body of the task. | 38 /// The body of the task. |
| 23 TaskBody fn; | 39 TaskBody fn; |
| 24 | 40 |
| 25 /// The identifier of the task. This is unique within [queue]. It's used for | 41 /// The identifier of the task. This is unique within [queue]. It's used for |
| 26 /// debugging when [description] isn't provided. | 42 /// debugging when [description] isn't provided. |
| 27 int _id; | 43 int _id; |
| 28 | 44 |
| 29 /// A Future that will complete to the return value of [fn] once this task | 45 /// A Future that will complete to the return value of [fn] once this task |
| 30 /// finishes running. | 46 /// finishes running. |
| 31 Future get result => _resultCompleter.future; | 47 Future get result => _resultCompleter.future; |
| 32 final _resultCompleter = new Completer(); | 48 final _resultCompleter = new Completer(); |
| 33 | 49 |
| 34 Task(fn(), this.queue, this.description) { | 50 Task(fn(), String description, TaskQueue queue) |
| 35 _id = this.queue.contents.length; | 51 : this._(fn, description, queue, null, queue.contents.length); |
| 52 | |
| 53 Task._child(fn(), String description, Task parent) | |
| 54 : this._(fn, description, parent.queue, parent, parent.children.length); | |
|
Bob Nystrom
2013/02/19 23:15:04
I think this means that ids are not unique. If top
nweiz
2013/02/20 00:23:12
Yes.
| |
| 55 | |
| 56 Task._(fn(), this.description, this.queue, this.parent, this._id) { | |
| 36 this.fn = () { | 57 this.fn = () { |
| 37 var future = new Future.immediate(null).then((_) => fn()); | 58 var future = new Future.immediate(null).then((_) => fn()) |
| 59 .whenComplete(() { | |
| 60 if (_childGroup == null || _childGroup.completed) return; | |
| 61 return _childGroup.future; | |
| 62 }); | |
| 38 chainToCompleter(future, _resultCompleter); | 63 chainToCompleter(future, _resultCompleter); |
| 39 return future; | 64 return future; |
| 40 }; | 65 }; |
| 41 | 66 |
| 42 // Make sure any error thrown by fn isn't top-leveled by virtue of being | 67 // Make sure any error thrown by fn isn't top-leveled by virtue of being |
| 43 // passed to the result future. | 68 // passed to the result future. |
| 44 result.catchError((_) {}); | 69 result.catchError((_) {}); |
| 45 } | 70 } |
| 46 | 71 |
| 72 /// Run [fn] as a child of this task. Returns a Future that will complete with | |
| 73 /// the result of the child task. This task will not complete until [fn] has | |
| 74 /// finished. | |
| 75 Future runChild(fn(), String description) { | |
| 76 var task = new Task._child(fn, description, this); | |
| 77 children.add(task); | |
| 78 if (_childGroup == null || _childGroup.completed) { | |
| 79 _childGroup = new FutureGroup(); | |
| 80 } | |
| 81 // Ignore errors in the FutureGroup; they'll get picked up via wrapFuture, | |
| 82 // and we don't want them to short-circuit the other Futures. | |
| 83 _childGroup.add(task.result.catchError((_) {})); | |
| 84 task.fn(); | |
| 85 return task.result; | |
| 86 } | |
| 87 | |
| 47 String toString() => description == null ? "#$_id" : description; | 88 String toString() => description == null ? "#$_id" : description; |
| 48 | 89 |
| 49 /// Returns a detailed representation of [queue] with this task highlighted. | 90 /// Returns a detailed representation of [queue] with this task highlighted. |
| 50 String generateTree() => queue.generateTree(this); | 91 String generateTree() => queue.generateTree(this); |
| 51 } | 92 } |
| OLD | NEW |