Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: lib/src/byte_collector.dart

Issue 2661603002: Add collectBytesCancelable function. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 import "dart:async"; 5 import "dart:async";
6 import "dart:typed_data"; 6 import "dart:typed_data";
7 import "cancelable_operation.dart";
7 8
8 /// Collects an asynchronous sequence of byte lists into a single list of bytes. 9 /// Collects an asynchronous sequence of byte lists into a single list of bytes.
9 /// 10 ///
10 /// If the [source] stream emits an error event, 11 /// If the [source] stream emits an error event,
11 /// the collection fails and the returned future completes with the same error. 12 /// the collection fails and the returned future completes with the same error.
12 /// 13 ///
13 /// If any of the input data are not valid bytes, they will be truncated to 14 /// If any of the input data are not valid bytes, they will be truncated to
14 /// an eight-bit unsigned value in the resulting list. 15 /// an eight-bit unsigned value in the resulting list.
15 Future<Uint8List> collectBytes(Stream<List<int>> source) { 16 Future<Uint8List> collectBytes(Stream<List<int>> source) {
16 var byteLists = List<List<int>>[]; 17 return _collectBytes<Future<Uint8List>>(source, _futureResult);
Lasse Reichstein Nielsen 2017/01/27 12:20:48 The type here was actually wrong (I just moved it
nweiz 2017/01/27 21:56:00 I think you shouldn't need an explicit generic typ
Lasse Reichstein Nielsen 2017/01/30 15:06:33 It probably doesn't even need to be generic, I can
nweiz 2017/01/30 22:21:26 It does need to be generic in strong mode—but I be
Lasse Reichstein Nielsen 2017/01/31 11:53:54 I've removed all the explicit type declarations (e
18 }
19
20 /// Collects an asynchronous sequence of byte lists into a single list of bytes.
21 ///
22 /// Returns a [CancelableOperation] that provides the result future and a way
23 /// to cancel the collection early.
24 ///
25 /// If the [source] stream emits an error event,
26 /// the collection fails and the returned future completes with the same error.
27 ///
28 /// If any of the input data are not valid bytes, they will be truncated to
29 /// an eight-bit unsigned value in the resulting list.
30 CancelableOperation<Uint8List> collectBytesCancelable(
31 Stream<List<int>> source) {
32 return _collectBytes<CancelableOperation<Uint8List>>(
33 source, _cancelOperationResult);
34 }
35
36 // Helper function for [collectBytes].
37 Future<Uint8List> _futureResult(_, Completer<Uint8List> completer) =>
38 completer.future;
nweiz 2017/01/27 21:56:00 I'd make these inline—I think it reads clearer, an
Lasse Reichstein Nielsen 2017/01/30 15:06:33 Done.
39
40 // Helper function for [collectBytesCancelable].
41 CancelableOperation<Uint8List> _cancelOperationResult(
42 StreamSubscription<List<int>> subscription,
43 Completer<Uint8List> completer) {
44 return new CancelableOperation.fromFuture(completer.future,
45 onCancel: subscription.cancel);
46 }
47
48 /// Generalization over [collectBytes] and [collectBytesCancelable].
49 ///
50 /// Performs all the same operations, but the final result is created
51 /// by the [result] function, which has access to the stream subscription
52 /// so it can cancel the operation.
53 T _collectBytes<T>(
54 Stream<List<int>> source,
55 T result(StreamSubscription<List<int>> subscription,
56 Completer<Uint8List> completer)) {
nweiz 2017/01/27 21:56:00 I'd just pass the Future here, since that's the on
Lasse Reichstein Nielsen 2017/01/30 15:06:33 True. Might as well extract it early instead of la
57 var byteLists = <List<int>>[];
17 var length = 0; 58 var length = 0;
18 var completer = new Completer<Uint8List>.sync(); 59 var completer = new Completer<Uint8List>.sync();
19 source.listen( 60 var subscription = source.listen(
20 (bytes) { 61 (bytes) {
21 byteLists.add(bytes); 62 byteLists.add(bytes);
22 length += bytes.length; 63 length += bytes.length;
23 }, 64 },
24 onError: completer.completeError, 65 onError: completer.completeError,
25 onDone: () { 66 onDone: () {
26 completer.complete(_collect(length, byteLists)); 67 completer.complete(_collect(length, byteLists));
27 }, 68 },
28 cancelOnError: true); 69 cancelOnError: true);
29 return completer.future; 70 return result(subscription, completer);
nweiz 2017/01/27 21:56:00 I really wish we had efficient multiple returns -_
Lasse Reichstein Nielsen 2017/01/30 15:06:33 Not sure what "multiple returns" means here, but I
nweiz 2017/01/30 22:21:26 So we could write "return subscription, completer;
Lasse Reichstein Nielsen 2017/01/31 11:53:54 Oh, I absolutely would love to have that. It's so
30 } 71 }
31 72
32 // Join a lists of bytes with a known total length into a single [Uint8List]. 73 // Join a lists of bytes with a known total length into a single [Uint8List].
33 Uint8List _collect(int length, List<List<int>> byteLists) { 74 Uint8List _collect(int length, List<List<int>> byteLists) {
34 var result = new Uint8List(length); 75 var result = new Uint8List(length);
35 int i = 0; 76 int i = 0;
36 for (var byteList in byteLists) { 77 for (var byteList in byteLists) {
37 var end = i + byteList.length; 78 var end = i + byteList.length;
38 result.setRange(i, end, byteList); 79 result.setRange(i, end, byteList);
39 i = end; 80 i = end;
40 } 81 }
41 return result; 82 return result;
42 } 83 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | test/byte_collection_test.dart » ('j') | test/byte_collection_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698