BehaviorSubject<T> constructor

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

Implementation

factory BehaviorSubject({
  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>();

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

        return controller.stream;
      }, reusable: true),
      wrapper);
}