1

I subscribe to an Observable with a pairwise pipe in order to get previous and new value.

But sometime I would like to tell the Observable that the value has been updated without applying the subscribe function. It will ensure that the previousValue is always updated but without always applying the subscribe function.

// Listen to FomControl -> update value variable.
this.valueChangesSubscription = this.control.valueChanges
    .pipe(startWith(this.defaultValue), pairwise())
    .subscribe(async ([previousValue, newValue]) => {
        // Do something
    });

I update the value of the formControl and sometime emitting the event, sometimes not:

this.control.setValue(formattedValue, { emitEvent });

But I would like that even if the event is not emitted, the previousValue of the subscribe function will be the real previousValue and not juste the latest emitted one.

Thank you.

MHogge
  • 5,408
  • 15
  • 61
  • 104

1 Answers1

0

You can try the filter operator to filter the subscribed result

private myBooleanValue = false;

// Listen to FomControl -> update value variable.
this.valueChangesSubscription = this.control.valueChanges
    .pipe(
       startWith(this.defaultValue), 
       pairwise(),
       // It will never emit a value to subscribe since its false
       filter(([previousValue, newValue]) => this.myBooleanValue),
     )
    .subscribe(async ([previousValue, newValue]) => {
        // Do something
    });
noririco
  • 765
  • 6
  • 15
  • Thanks but this is never going to the `subscribe` function. What I would like is a '`emitWithSubscribeApplied` and `emitWithoutSubscribeApplied` kind of functions. I've searched a way to pass metadata alongside the event but it doesn't seems feasible. – MHogge Jun 02 '21 at 09:25
  • It will if you change the value of `myBooleanValue` to true, what is the problem with that ? – noririco Jun 02 '21 at 09:28
  • I don't control every value change of the `formControl`. I don't want to miss an event because 2 events has been emitting nearly at the ame time: one programmatically and one through an user action (typing text in an input, ...). If I set the boolean to `false` when emitting programmatically and don't reset it to true before the "user event" is being called than I would miss that action. – MHogge Jun 02 '21 at 09:34