ReplaySubject<T> constructor

ReplaySubject<T>({int maxSize, void onListen(), void onCancel(), bool sync: false })

Constructs a ReplaySubject, optionally pass handlers for onListen, onCancel and a flag to handle events sync.

See also StreamController.broadcast

Implementation

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

  final queue = Queue<T>();

  return ReplaySubject<T>._(
    controller,
    Observable<T>.defer(
        () => Observable<T>(controller.stream)
            .startWithMany(queue.toList(growable: false)),
        reusable: true),
    queue,
    maxSize,
  );
}