Home > other >  RxJS - Find out wether a BehaviourSubjects initial value changed
RxJS - Find out wether a BehaviourSubjects initial value changed

Time:07-19

I would like to know wether there is a simple way to find out, wether I am dealing with the initial-value of a BehaviourSubject or wether it has been changed by "next".

CodePudding user response:

The BehaviourSubject has not such a field. But it is pretty simple to define it yourself.

const yourSubject$<string> = new BehaviourSubject('test');
let subjectChanged = false;

yourSubject
   .pipe(
      distinct(), // if you want to ignore same values
      skip(1), // ignore the initial value
      take(1), // only take one change
    )
   .subscribe(() => subjectChanged = true);

A emit with a other value will cause, that subjectChanged will switch to true.

  • Related