I have a form with a bunch of fields (using reactiveForm). When the value change in field A or B, I want to execute doStuffWithAnB.
so combineLatest seems to be what i need.
combineLatest(A.valueChanges,B.valueChanges).subscribe(doStuffWithAnB)
now if the user goes in the form, and touches only B (Or A), I want to execute my function, > here comes startWith
combineLatest(A.valueChanges.pipe(startWith(someDefaultValue),B.valueChanges.pipe(startWith(someOtherDefaultValue)).subscribe(doStuffWithAnB)
but now doStuffWithAnB is fired from the start since my 2 streams have a startWith, but i don't want to execute doStuffWithAnB until one of the fields has been modified
how can i achieve that in a clean way ?
Why not go for a merge
instead which will take either one of the results! We also do not need startsWith
, since it should not execute initially!
From:
combineLatest(A.valueChanges.pipe(startWith(someDefaultValue),B.valueChanges.pipe(startWith(someOtherDefaultValue)).subscribe(doStuffWithAnB)
To:
merge(A.valueChanges,B.valueChanges).subscribe(doStuffWithAnB)