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.utils; | 5 library watcher.utils; |
6 | 6 |
7 import 'dart:async'; | |
7 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:collection'; | |
8 | 10 |
9 /// Returns `true` if [error] is a [FileSystemException] for a missing | 11 /// Returns `true` if [error] is a [FileSystemException] for a missing |
10 /// directory. | 12 /// directory. |
11 bool isDirectoryNotFoundException(error) { | 13 bool isDirectoryNotFoundException(error) { |
12 if (error is! FileSystemException) return false; | 14 if (error is! FileSystemException) return false; |
13 | 15 |
14 // See dartbug.com/12461 and tests/standalone/io/directory_error_test.dart. | 16 // See dartbug.com/12461 and tests/standalone/io/directory_error_test.dart. |
15 var notFoundCode = Platform.operatingSystem == "windows" ? 3 : 2; | 17 var notFoundCode = Platform.operatingSystem == "windows" ? 3 : 2; |
16 return error.osError.errorCode == notFoundCode; | 18 return error.osError.errorCode == notFoundCode; |
17 } | 19 } |
20 | |
21 /// Returns a buffered stream that will emit the same values as the stream | |
22 /// returned by [future] once [future] completes. | |
23 /// | |
24 /// If [future] completes to an error, the return value will emit that error and | |
25 /// then close. | |
26 Stream futureStream(Future<Stream> future) { | |
27 var controller = new StreamController(sync: true); | |
28 future.then((stream) { | |
29 stream.listen( | |
30 controller.add, | |
31 onError: controller.addError, | |
32 onDone: controller.close); | |
33 }).catchError((e, stackTrace) { | |
34 controller.addError(e, stackTrace); | |
35 controller.close(); | |
36 }); | |
37 return controller.stream; | |
38 } | |
Bob Nystrom
2013/11/06 19:24:04
We have an increasingly large set of copy/pasted f
nweiz
2013/11/07 00:46:37
I'd love to, but there are a bunch of hurdles we h
Bob Nystrom
2013/11/07 18:16:05
Some of those hurdles have been jumped already (fo
| |
39 | |
40 /// Like [new Future], but avoids around issue 11911 by using [new Future.value] | |
41 /// under the covers. | |
42 Future newFuture(callback()) => new Future.value().then((_) => callback()); | |
43 | |
44 /// A stream transformer that batches all events sent in adjacent event loops. | |
Bob Nystrom
2013/11/06 19:24:04
I don't know what "adjacent event loops" means. Do
nweiz
2013/11/07 00:46:37
Done.
| |
45 class BatchedStreamTransformer<T> implements StreamTransformer<T, List<T>> { | |
46 Stream<List<T>> bind(Stream<T> input) { | |
47 var batch = new Queue(); | |
48 return new StreamTransformer<T, List<T>>.fromHandlers( | |
49 handleData: (event, sink) { | |
50 batch.add(event); | |
51 var batchLength = batch.length; | |
52 | |
53 Timer.run(() { | |
Bob Nystrom
2013/11/06 19:24:04
Document what's going on here.
nweiz
2013/11/07 00:46:37
Done.
| |
54 if (batch.length != batchLength) return; | |
55 sink.add(batch.toList()); | |
56 batch.clear(); | |
57 }); | |
58 }, handleDone: (sink) { | |
59 if (batch.isNotEmpty) { | |
60 sink.add(batch.toList()); | |
61 batch.clear(); | |
62 } | |
63 sink.close(); | |
64 }).bind(input); | |
65 } | |
66 } | |
OLD | NEW |