@@ -2323,6 +2323,12 @@ abstract interface class StreamSink<S>
2323
2323
/// [Stream.where] or [Stream.expand] can be implemented using
2324
2324
/// [Stream.transform] . A [StreamTransformer] is thus very powerful but often
2325
2325
/// 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.
2326
2332
abstract interface class StreamTransformer <S , T > {
2327
2333
/// Creates a [StreamTransformer] based on the given [onListen] callback.
2328
2334
///
@@ -2409,11 +2415,50 @@ abstract interface class StreamTransformer<S, T> {
2409
2415
/// }));
2410
2416
/// ```
2411
2417
///
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._
2414
2459
///
2415
2460
/// ```dart
2416
- /// StreamController<String> controller = StreamController.broadcast();
2461
+ /// var controller = StreamController<String> .broadcast();
2417
2462
/// controller.onListen = () {
2418
2463
/// scheduleMicrotask(() {
2419
2464
/// controller.addError("Bad");
0 commit comments