OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 async.null_stream_sink; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 /// A [StreamSink] that discards all events. |
| 10 /// |
| 11 /// The sink silently drops events until [close] is called, at which point it |
| 12 /// throws [StateError]s when events are added. This is the same behavior as a |
| 13 /// sink whose remote end has closed, such as when a [WebSocket] connection has |
| 14 /// been closed. |
| 15 /// |
| 16 /// This can be used when a sink is needed but no events are actually intended |
| 17 /// to be added. The [new NullStreamSink.error] constructor can be used to |
| 18 /// represent errors when creating a sink, since [StreamSink.done] exposes sink |
| 19 /// errors. For example: |
| 20 /// |
| 21 /// ```dart |
| 22 /// StreamSink<List<int>> openForWrite(String filename) { |
| 23 /// try { |
| 24 /// return new RandomAccessSink(new File(filename).openSync()); |
| 25 /// } on IOException catch (error, stackTrace) { |
| 26 /// return new NullStreamSink.error(error, stackTrace); |
| 27 /// } |
| 28 /// } |
| 29 /// ``` |
| 30 class NullStreamSink<T> implements StreamSink<T> { |
| 31 final Future done; |
| 32 |
| 33 /// Whether the sink has been closed. |
| 34 var _closed = false; |
| 35 |
| 36 /// Whether an [addStream] call is pending. |
| 37 /// |
| 38 /// We don't actually add any events from streams, but it does return the |
| 39 /// [StreamSubscription.cancel] future so to be [StreamSink]-complaint we |
| 40 /// reject events until that completes. |
| 41 var _addingStream = false; |
| 42 |
| 43 /// Creates a null sink. |
| 44 /// |
| 45 /// If [done] is passed, it's used as the [Sink.done] future. Otherwise, a |
| 46 /// completed future is used. |
| 47 NullStreamSink({Future done}) : done = done ?? new Future.value(); |
| 48 |
| 49 /// Creates a null sink whose [done] future emits [error]. |
| 50 /// |
| 51 /// Note that this error will not be considered uncaught. |
| 52 NullStreamSink.error(error, [StackTrace stackTrace]) |
| 53 : done = new Future.error(error, stackTrace) |
| 54 // Don't top-level the error. This gives the user a change to call |
| 55 // [close] or [done], and matches the behavior of a remote endpoint |
| 56 // experiencing an error. |
| 57 ..catchError((_) {}); |
| 58 |
| 59 void add(T data) { |
| 60 _checkEventAllowed(); |
| 61 } |
| 62 |
| 63 void addError(error, [StackTrace stackTrace]) { |
| 64 _checkEventAllowed(); |
| 65 } |
| 66 |
| 67 Future addStream(Stream<T> stream) { |
| 68 _checkEventAllowed(); |
| 69 |
| 70 _addingStream = true; |
| 71 var future = stream.listen(null).cancel() ?? new Future.value(); |
| 72 return future.whenComplete(() { |
| 73 _addingStream = false; |
| 74 }); |
| 75 } |
| 76 |
| 77 /// Throws a [StateError] if [close] has been called or an [addStream] call is |
| 78 /// pending. |
| 79 void _checkEventAllowed() { |
| 80 if (_closed) throw new StateError("Cannot add to a closed sink."); |
| 81 if (_addingStream) { |
| 82 throw new StateError("Cannot add to a sink while adding a stream."); |
| 83 } |
| 84 } |
| 85 |
| 86 Future close() { |
| 87 _closed = true; |
| 88 return done; |
| 89 } |
| 90 } |
OLD | NEW |