Skip to content

Commit d3f7940

Browse files
nateboschCommit Queue
authored andcommitted
Expand StreamTransform.fromHandlers docs
Closes #27740 Mention the convenience constructors `fromhandlers` and `fromBind` in the class level doc. Describe the default behavior of each handlers. [email protected] Change-Id: Ica55d5a2f531e11106085122aa32173cc436991a CoreLibraryReviewExempt: Doc changes only. Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/310765 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Nate Bosch <[email protected]>
1 parent 5b3f799 commit d3f7940

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

sdk/lib/async/stream.dart

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,12 @@ abstract interface class StreamSink<S>
23232323
/// [Stream.where] or [Stream.expand] can be implemented using
23242324
/// [Stream.transform]. A [StreamTransformer] is thus very powerful but often
23252325
/// also a bit more complicated to use.
2326+
///
2327+
/// The [StreamTransformer.fromHandlers] constructor allows passing separate
2328+
/// callbacks to react to events, errors, and the end of the stream.
2329+
/// The [StreamTransformer.fromBind] constructor creates a `StreamTransformer`
2330+
/// whose [bind] method is implemented by calling the function passed to the
2331+
/// constructor.
23262332
abstract interface class StreamTransformer<S, T> {
23272333
/// Creates a [StreamTransformer] based on the given [onListen] callback.
23282334
///
@@ -2409,11 +2415,50 @@ abstract interface class StreamTransformer<S, T> {
24092415
/// }));
24102416
/// ```
24112417
///
2412-
/// Transformers that are constructed this way cannot use captured state if
2413-
/// they are used in streams that can be listened to multiple times.
2418+
/// When a transformed stream returned from a call to [bind] is listened to,
2419+
/// the source stream is listened to, and a handler function is called for
2420+
/// each event of the source stream.
2421+
///
2422+
/// The handlers are invoked with the event data and with a sink that can be
2423+
/// used to emit events on the transformed stream.
2424+
///
2425+
/// The [handleData] handler is invoked for data events on the source stream.
2426+
/// If [handleData] was omitted, data events are added directly to the created
2427+
/// stream, as if calling [EventSink.add] on the sink with the event value.
2428+
/// If [handleData] is omitted the source stream event type, [S], must be a
2429+
/// subtype of the transformed stream event type [T].
2430+
///
2431+
/// The [handleError] handler is invoked for each error of the source stream.
2432+
/// If [handleError] is omitted, errors are forwarded directly to the
2433+
/// transformed stream, as if calling [EventSink.addError] with the error and
2434+
/// stack trace.
2435+
///
2436+
/// The [handleDone] handler is invoked when the source stream closes, as
2437+
/// signaled by sending a done event. The done handler takes no event value,
2438+
/// but can still send other events before calling [EventSink.close]. If
2439+
/// [handleDone] is omitted, a done event on the source stream closes the
2440+
/// transformed stream.
2441+
///
2442+
/// If any handler calls [EventSink.close] on the provided sink,
2443+
/// the transformed sink closes and the source stream subscription
2444+
/// is cancelled. No further events can be added to the sink by
2445+
/// that handler, and no further source stream events will occur.
2446+
///
2447+
/// The sink provided to the event handlers must only be used during
2448+
/// the call to that handler. It must not be stored and used at a later
2449+
/// time.
2450+
///
2451+
/// Transformers created this way should be *stateless*.
2452+
/// They should not retain state between invocations of handlers,
2453+
/// because the same transformer, and therefore the same handlers,
2454+
/// may be used on multiple streams, or on streams which can be listened
2455+
/// to more than once.
2456+
/// _To create per-stream handlers, [StreamTransformer.fromBind]
2457+
/// could be used to create a new [StreamTransformer.fromHandlers] per
2458+
/// stream to transform._
24142459
///
24152460
/// ```dart
2416-
/// StreamController<String> controller = StreamController.broadcast();
2461+
/// var controller = StreamController<String>.broadcast();
24172462
/// controller.onListen = () {
24182463
/// scheduleMicrotask(() {
24192464
/// controller.addError("Bad");

0 commit comments

Comments
 (0)