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

Unified Diff: lib/src/accumulator_sink.dart

Issue 1911053002: Add sinks to provide synchronous access to events. (Closed) Base URL: git@github.com:dart-lang/convert@master
Patch Set: Code review changes Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/convert.dart ('k') | lib/src/byte_accumulator_sink.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/accumulator_sink.dart
diff --git a/lib/src/accumulator_sink.dart b/lib/src/accumulator_sink.dart
new file mode 100644
index 0000000000000000000000000000000000000000..ca35f7add65e8b947770c4e2547d4f0d0ce390bd
--- /dev/null
+++ b/lib/src/accumulator_sink.dart
@@ -0,0 +1,37 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:collection';
+
+/// A sink that provides access to all the [events] that have been passed to it.
+///
+/// See also [ChunkedConversionSink.withCallback].
+class AccumulatorSink<T> implements Sink<T> {
+ /// An unmodifiable list of events passed to this sink so far.
+ List<T> get events => new UnmodifiableListView(_events);
+ final _events = <T>[];
+
+ /// Whether [close] has been called.
+ bool get isClosed => _isClosed;
+ var _isClosed = false;
+
+ /// Removes all events from [events].
+ ///
+ /// This can be used to avoid double-processing events.
+ void clear() {
+ _events.clear();
+ }
+
+ void add(T event) {
+ if (_isClosed) {
+ throw new StateError("Can't add to a closed sink.");
+ }
+
+ _events.add(event);
+ }
+
+ void close() {
+ _isClosed = true;
+ }
+}
« no previous file with comments | « lib/convert.dart ('k') | lib/src/byte_accumulator_sink.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698