Martijn Bakker
Martijn Bakker

Reputation: 395

RxJS hold observable and next latest value on untold

I've two observables, one is a data observable that holds the data and I've one locker observable. When the locker value has value 'lock' the data observable should send his data to his subscribers. When we unlock the data observable it should then send the last next value to the subscribers.

I've included a diagram that shows what I want. When the B source is L (locked) it should hold the value, when the B source is U it should pass the A value and return also the last know value on change.

Hope that someone can help me out with finding the right operator for this.

enter image description here

Upvotes: 1

Views: 335

Answers (1)

martin
martin

Reputation: 96959

Looks like you could do that with combineLatest():

combineLatest([lock$, data$])
  .pipe(
    switchMap(([lock, data]) => {
      if (lock === 'L') {
        return EMPTY;
      }
      // When `lock` changes to unlocked then `data` will already have the most recent value from `data$`.
      return of(data);
    }),
   ),

Upvotes: 1

Related Questions