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

Unified Diff: sdk/lib/async/stream_controller.dart

Issue 1242023007: Add setters for callbacks on StreamController. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comments. Created 5 years, 5 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: sdk/lib/async/stream_controller.dart
diff --git a/sdk/lib/async/stream_controller.dart b/sdk/lib/async/stream_controller.dart
index 5764ecdd2102e6dc975a6c1ca3d81a0ed4db3ad4..26bbb14706cb55d971db51bce691f7f41a0f6183 100644
--- a/sdk/lib/async/stream_controller.dart
+++ b/sdk/lib/async/stream_controller.dart
@@ -77,12 +77,6 @@ abstract class StreamController<T> implements StreamSink<T> {
void onResume(),
onCancel(),
bool sync: false}) {
- if (onListen == null && onPause == null &&
- onResume == null && onCancel == null) {
- return sync
- ? new _NoCallbackSyncStreamController<T>()
- : new _NoCallbackAsyncStreamController<T>();
- }
return sync
? new _SyncStreamController<T>(onListen, onPause, onResume, onCancel)
: new _AsyncStreamController<T>(onListen, onPause, onResume, onCancel);
@@ -139,6 +133,42 @@ abstract class StreamController<T> implements StreamSink<T> {
}
/**
+ * Sets the callback which is called when the stream is listened to.
+ *
+ * This overrides the previous callback, or clears it if the [onListenHandler]
+ * is `null`.
+ */
+ void set onListen(void onListenHandler());
nweiz 2015/07/17 20:40:16 It's kind of weird that these are setters, whereas
Lasse Reichstein Nielsen 2015/08/05 09:08:45 It is annoying that we didn't go for setters in St
nweiz 2015/08/06 01:02:13 I talked to Bob about this, and he pointed out tha
Lasse Reichstein Nielsen 2015/08/06 07:00:41 Well, it's quite symmetric - you have read-only fi
floitsch 2015/08/06 09:28:12 In this case I wouldn't onListen to setOnListen. I
floitsch 2015/08/06 09:28:12 I actually disagree. Sometimes a "setX" method is
Lasse Reichstein Nielsen 2015/08/06 14:09:40 I think we did screw up that particular type of na
nweiz 2015/08/06 19:56:12 I'm fine with these being setters as long as there
+
+ /**
+ * Sets the callback which is called when the stream is paused.
+ *
+ * This overrides the previous callback, or clears it if the [onPauseHandler]
+ * is `null`.
+ *
+ * Pause related callbacks are not supported on broadcast stream controllers.
+ */
+ void set onPause(void onPauseHandler());
+
+ /**
+ * Sets the callback which is called when the stream is resumed.
+ *
+ * This overrides the previous callback, or clears it if the [onResumeHandler]
+ * is `null`.
+ *
+ * Pause related callbacks are not supported on broadcast stream controllers.
+ */
+ void set onResume(void onResumeHandler());
+
+ /**
+ * Sets the callback which is called when the stream is canceled.
+ *
+ * This overrides the previous callback, or clears it if the [onCancelHandler]
+ * is `null`.
+ */
+ void set onCancel(onCancelHandler());
+
+ /**
* Returns a view of this object that only exposes the [StreamSink] interface.
*/
StreamSink<T> get sink;
@@ -383,12 +413,23 @@ abstract class _StreamController<T> implements StreamController<T>,
// accessed earlier, or if close is called before subscribing.
_Future _doneFuture;
- _StreamController();
+ _NotificationHandler _onListen;
+ _NotificationHandler _onPause;
+ _NotificationHandler _onResume;
+ _NotificationHandler _onCancel;
- _NotificationHandler get _onListen;
- _NotificationHandler get _onPause;
- _NotificationHandler get _onResume;
- _NotificationHandler get _onCancel;
+ _StreamController(void this._onListen(),
+ void this._onPause(),
+ void this._onResume(),
+ this._onCancel());
+
+ void set onListen(void onListenHandler()) { _onListen = onListenHandler; }
+
+ void set onPause(void onPauseHandler()) { _onPause = onPauseHandler; }
+
+ void set onResume(void onResumeHandler()) { _onResume = onResumeHandler; }
+
+ void set onCancel(onCancelHandler()) { _onCancel = onCancelHandler; }
// Return a new stream every time. The streams are equal, but not identical.
Stream<T> get stream => new _ControllerStream<T>(this);
@@ -722,44 +763,11 @@ abstract class _AsyncStreamControllerDispatch<T>
// TODO(lrn): Use common superclass for callback-controllers when VM supports
// constructors in mixin superclasses.
-class _AsyncStreamController<T> extends _StreamController<T>
- with _AsyncStreamControllerDispatch<T> {
- final _NotificationHandler _onListen;
- final _NotificationHandler _onPause;
- final _NotificationHandler _onResume;
- final _NotificationHandler _onCancel;
+class _AsyncStreamController<T> = _StreamController<T>
+ with _AsyncStreamControllerDispatch<T>;
- _AsyncStreamController(void this._onListen(),
- void this._onPause(),
- void this._onResume(),
- this._onCancel());
-}
-
-class _SyncStreamController<T> extends _StreamController<T>
- with _SyncStreamControllerDispatch<T> {
- final _NotificationHandler _onListen;
- final _NotificationHandler _onPause;
- final _NotificationHandler _onResume;
- final _NotificationHandler _onCancel;
-
- _SyncStreamController(void this._onListen(),
- void this._onPause(),
- void this._onResume(),
- this._onCancel());
-}
-
-abstract class _NoCallbacks {
- _NotificationHandler get _onListen => null;
- _NotificationHandler get _onPause => null;
- _NotificationHandler get _onResume => null;
- _NotificationHandler get _onCancel => null;
-}
-
-class _NoCallbackAsyncStreamController<T> = _StreamController<T>
- with _AsyncStreamControllerDispatch<T>, _NoCallbacks;
-
-class _NoCallbackSyncStreamController<T> = _StreamController<T>
- with _SyncStreamControllerDispatch<T>, _NoCallbacks;
+class _SyncStreamController<T> = _StreamController<T>
+ with _SyncStreamControllerDispatch<T>;
typedef _NotificationHandler();

Powered by Google App Engine
This is Rietveld 408576698