Single<T>.unsafeFromStream constructor

Single<T>.unsafeFromStream(
  1. Stream<T> source
)

Converts source Stream into a Single. If the source Stream is already a Single, it will be returned as-is.

If the source Stream does NOT emit exactly ONE data event or ONE error event before successfully completing, the returned Single will emit an APIContractViolationError.

NOTE: Because of that, consider using take(1).doneOnError() to the source Stream before using this constructor to create a Single safety.

Example

Single.unsafeFromStream(Stream.value(1)); // Single of 1

Single.unsafeFromStream(Stream<int>.error(Exception())); // Single of Exception()

Single.unsafeFromStream(Stream.fromIterable([1, 2])); // Single of APIContractViolationError

Single.unsafeFromStream(
  Rx.concat<int>([
    Stream.value(1),
    Stream.error(Exception())
  ])
); // Single of APIContractViolationError

Single.unsafeFromStream(
  Rx.concat<int>([
    Stream.error(Exception()),
    Stream.error(Exception())
  ])
) // Single of APIContractViolationError

Single.unsafeFromStream(
    Rx.concat<int>([
      Stream.fromIterable([1, 2, 3, 4]),
      Stream<int>.error(Exception()),
      Stream.value(1),
      Stream<int>.error(Exception()),
    ])
     .take(1)
     .doneOnError()
); // Single of 1

Implementation

factory Single.unsafeFromStream(Stream<T> source) => source is Single<T>
    ? source
    : Single.safe(Stream<T>.eventTransformed(
        source, (sink) => _SingleOrErrorStreamSink<T>(sink)));