shareValueDistinct method

DistinctValueStream<T> shareValueDistinct(
  1. T seedValue, {
  2. bool equals(
    1. T previous,
    2. T next
    )?,
  3. bool sync = true,
})

Convert the this Stream into a new DistinctValueStream that can be listened to multiple times, providing an initial value. It will automatically begin emitting items when first listened to, and shut down when no listeners remain.

This is useful for converting a single-subscription stream into a broadcast Stream. It's also useful for providing sync access to the latest emitted value.

Example

// Convert a single-subscription fromIterable stream into a broadcast
// stream that will emit the latest value to any new listeners
final stream = Stream
  .fromIterable([1, 2, 2, 3, 3, 3])
  .shareValueDistinct(0);

// Start listening to the source Stream. Will start printing 1, 2, 3
final subscription = stream.listen(print);

// Synchronously print the latest value
print(stream.value);

// Subscribe again later. Does not print anything.
final subscription2 = stream.listen(print);

// Stop emitting items from the source stream and close the underlying
// ValueSubject by cancelling all subscriptions.
subscription.cancel();
subscription2.cancel();

Implementation

DistinctValueStream<T> shareValueDistinct(
  T seedValue, {
  bool Function(T previous, T next)? equals,
  bool sync = true,
}) =>
    publishValueDistinct(seedValue, equals: equals, sync: sync).refCount();