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 watcher.async_queue; | |
6 | |
7 import 'dart:async'; | 5 import 'dart:async'; |
8 import 'dart:collection'; | 6 import 'dart:collection'; |
9 | 7 |
10 typedef Future ItemProcessor<T>(T item); | 8 typedef Future ItemProcessor<T>(T item); |
11 | 9 |
12 /// A queue of items that are sequentially, asynchronously processed. | 10 /// A queue of items that are sequentially, asynchronously processed. |
13 /// | 11 /// |
14 /// Unlike [Stream.map] or [Stream.forEach], the callback used to process each | 12 /// Unlike [Stream.map] or [Stream.forEach], the callback used to process each |
15 /// item returns a [Future], and it will not advance to the next item until the | 13 /// item returns a [Future], and it will not advance to the next item until the |
16 /// current item is finished processing. | 14 /// current item is finished processing. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 var item = _items.removeFirst(); | 62 var item = _items.removeFirst(); |
65 return _processor(item).then((_) { | 63 return _processor(item).then((_) { |
66 if (_items.isNotEmpty) return _processNextItem(); | 64 if (_items.isNotEmpty) return _processNextItem(); |
67 | 65 |
68 // We have drained the queue, stop processing and wait until something | 66 // We have drained the queue, stop processing and wait until something |
69 // has been enqueued. | 67 // has been enqueued. |
70 _isProcessing = false; | 68 _isProcessing = false; |
71 }); | 69 }); |
72 } | 70 } |
73 } | 71 } |
OLD | NEW |