from method

Connect<T, S> from(
  1. Stream<S> source, {
  2. bool? cancelOnError,
  3. Function? onError,
  4. Function? onDone,
  5. void onValue(
    1. T
    )?,
})

Connects a Stream to a Signal.

final counter = signal(0);
final c = connect(counter);

final s1 = Stream.value(1);
final s2 = Stream.value(2);

c.from(s1).from(s2);

c.dispose();

Implementation

Connect<T, S> from(
  Stream<S> source, {
  bool? cancelOnError,
  Function? onError,
  Function? onDone,
  void Function(T)? onValue,
}) {
  // stop multiple subscriptions to the same stream
  if (_subscriptions.containsKey(source.hashCode)) {
    return this;
  }
  final subscription = source.listen(
    (event) {
      signal.value = event;
      onValue?.call(event);
    },
    onError: onError,
    onDone: () {
      _subscriptions.removeWhere((key, value) => key == source.hashCode);
      onDone?.call();
    },
    cancelOnError: cancelOnError,
  );
  _subscriptions[source.hashCode] = subscription;
  return this;
}