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

Unified Diff: lib/src/delegate/stream_subscription.dart

Issue 1870543004: Add typed wrapper functions to delegate classes. (Closed) Base URL: git@github.com:dart-lang/async.git@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
Index: lib/src/delegate/stream_subscription.dart
diff --git a/lib/src/delegate/stream_subscription.dart b/lib/src/delegate/stream_subscription.dart
index e7153b2a6db6a640547abec2e5879f592cfbfba0..b86e91f9192bf9bdc3a3a38f79bbba581b4e83fd 100644
--- a/lib/src/delegate/stream_subscription.dart
+++ b/lib/src/delegate/stream_subscription.dart
@@ -4,6 +4,8 @@
import 'dart:async';
+import '../typed/stream_subscription.dart';
+
/// Simple delegating wrapper around a [StreamSubscription].
///
/// Subclasses can override individual methods.
@@ -11,9 +13,22 @@ class DelegatingStreamSubscription<T> implements StreamSubscription<T> {
final StreamSubscription _source;
/// Create delegating subscription forwarding calls to [sourceSubscription].
- DelegatingStreamSubscription(StreamSubscription sourceSubscription)
+ DelegatingStreamSubscription(StreamSubscription<T> sourceSubscription)
: _source = sourceSubscription;
+ /// Creates a wrapper which throws if [subscription]'s events aren't instances
+ /// of `T`.
+ ///
+ /// This soundly converts a [StreamSubscription] to a `StreamSubscription<T>`,
+ /// regardless of its original generic type, by asserting that its events are
+ /// instances of `T` whenever they're provided. If they're not, the
+ /// subscription throws a [CastError].
+ static StreamSubscription/*<T>*/ typed/*<T>*/(
+ StreamSubscription subscription) =>
+ subscription is StreamSubscription/*<T>*/
+ ? subscription
+ : new TypeSafeStreamSubscription/*<T>*/(subscription);
+
void onData(void handleData(T data)) {
_source.onData(handleData);
}

Powered by Google App Engine
This is Rietveld 408576698