OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library task; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'schedule.dart'; |
| 10 import 'utils.dart'; |
| 11 |
| 12 typedef Future TaskBody(); |
| 13 |
| 14 /// A single task to be run as part of a [TaskQueue]. |
| 15 class Task { |
| 16 /// The queue to which this [Task] belongs. |
| 17 final TaskQueue queue; |
| 18 |
| 19 /// A description of this task. Used for debugging. May be `null`. |
| 20 final String description; |
| 21 |
| 22 /// The body of the task. |
| 23 TaskBody fn; |
| 24 |
| 25 /// The identifier of the task. This is unique within [queue]. It's used for |
| 26 /// debugging when [description] isn't provided. |
| 27 int _id; |
| 28 |
| 29 /// A Future that will complete to the return value of [fn] once this task |
| 30 /// finishes running. |
| 31 Future get result => _resultCompleter.future; |
| 32 final _resultCompleter = new Completer(); |
| 33 |
| 34 Task(fn(), this.queue, this.description) { |
| 35 _id = this.queue.contents.length; |
| 36 this.fn = () { |
| 37 var future = new Future.immediate(null).then((_) => fn()); |
| 38 chainToCompleter(future, _resultCompleter); |
| 39 return future; |
| 40 }; |
| 41 |
| 42 // Make sure any error thrown by fn isn't top-leveled by virtue of being |
| 43 // passed to the result future. |
| 44 result.catchError((_) {}); |
| 45 } |
| 46 |
| 47 String toString() => description == null ? "#$_id" : description; |
| 48 |
| 49 /// Returns a detailed representation of [queue] with this task highlighted. |
| 50 String generateTree() => queue.generateTree(this); |
| 51 } |
OLD | NEW |