createReadableStreamSourceFromStream<T> function

ReadableStreamSource<T> createReadableStreamSourceFromStream<T>(
  1. Stream<T> stream
)

Create ReadableStreamSource from Dart Stream.

Implementation

ReadableStreamSource<T> createReadableStreamSourceFromStream<T>(Stream<T> stream) {
  late final StreamSubscription<T> subscription;
  return ReadableStreamSource(
    start: allowInterop((controller) {
      subscription = stream.listen(
        (event) {
          controller.enqueue(event);
        },
        onDone: () {
          controller.close();
        },
      );
    }),
    cancel: allowInterop(
      (reason) async => subscription.cancel(),
    ),
  );
}