-2

I'm new to RxJS. I basically wanted a BehaviorSubject that doesn't require an initial value. I created it like this:

class AsyncBehaviorSubject<T> extends ReplaySubject<T> {
  constructor() {
    super(1)
  }
}

It seems that this should be part of the library and that my class is unnecessary but I can't find anywhere in the documentation for an observable that provides this behavior. It makes me feel like I'm missing something.

Thanks!

Tim
  • 6,265
  • 5
  • 29
  • 24
  • I'm not sure this is what you want but you could use simply a `Subject`. You instantiate it without an initial value, can have multiple subscribers but doesn't immediately emit a value on subscription... – JuanDeLasNieves Apr 21 '22 at 19:02
  • @JuanDeLasNieves But does it provide the previous value on subscribe? – Tim Apr 21 '22 at 20:02
  • it does not, but if you require the previous value you have the other types of Subjects. I don't quite understand what you try to achieve, so I'll leave this link here to a similar question, although is related to Angular: https://stackoverflow.com/questions/43348463/what-is-the-difference-between-subject-and-behaviorsubject – JuanDeLasNieves Apr 21 '22 at 21:15

1 Answers1

1

I think you can do whatever you want as long as it makes sense and preserves the logic. But if you need BehaviorSubject, why to use ReplaySubject? Just set initial value to undefined or null, as in your way:

class AsyncBehaviorSubject<T> extends BehaviorSubject<T> {
  constructor() {
    super(undefined);
  }
}
Anton Marinenko
  • 2,749
  • 1
  • 10
  • 16