BehaviorSubject<T>.seeded constructor

BehaviorSubject<T>.seeded(T seedValue, { void onListen(), void onCancel(), bool sync: false })

Implementation

factory BehaviorSubject.seeded(
  T seedValue, {
  void onListen(),
  void onCancel(),
  bool sync = false,
}) {
  // ignore: close_sinks
  final controller = StreamController<T>.broadcast(
    onListen: onListen,
    onCancel: onCancel,
    sync: sync,
  );

  final wrapper = _Wrapper<T>.seeded(seedValue);

  return BehaviorSubject<T>._(
      controller,
      Observable<T>.defer(() {
        if (wrapper.latestIsError) {
          scheduleMicrotask(() => controller.addError(
              wrapper.latestError, wrapper.latestStackTrace));
        }

        return Observable<T>(controller.stream)
            .startWith(wrapper.latestValue);
      }, reusable: true),
      wrapper);
}