title: Operator similar to exhaustMap but that remembers the last skipped value from the source and executes it in the end
body: <p>I need an operator similar to <code>exahustMap</code>, but which remembers the last skipped observable and executes it after the current observable completes.</p>
  
  <p>For example, considering the marble diagram for <code>exhaustMap</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/ZynZP.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZynZP.png" alt="exhaustMap marble diagram"></a></p>
  
  <p>In my case, after the blue values are emitted, it will be followed by three values of 50. Of course, in this case it looks just like <code>concatMap</code>, but if there were also a 4 between 3 and 5, it would not reflect in the output.</p>
  
  <p>I've managed to write my own operator similar to how <code>exhaustMap</code> is implemented:</p>
  
  <pre><code>function exhaustLatestMap&lt;T, R&gt;(project: (value: T) =&gt; Subscribable&lt;R&gt;): OperatorFunction&lt;T, R&gt; {
      return source =&gt; new Observable&lt;R&gt;(observer =&gt; 
          source.subscribe(new ExhaustLatestMapOperatorSubscriber(observer, project)));
  }
  
  class ExhaustLatestMapOperatorSubscriber&lt;T, R&gt; implements Observer&lt;T&gt; {
  
      constructor(
          private observer: Subscriber&lt;R&gt;,
          private project: (value: T) =&gt; Subscribable&lt;R&gt;) { }
  
      innerSub: AnonymousSubscription = null;
      latestValue: T;
  
      next(value: T) {
          this.processNext(value);
      }
  
      error(err) {
          this.observer.error(err);
      }
  
      complete() {
          this.observer.complete();
      }
  
      private processNext(value: T) {
          this.latestValue = value;
          if (!this.innerSub) {
              this.innerSub = this.project(value).subscribe({
                  next: v =&gt; this.observer.next(v),
                  error: err =&gt; {
                      this.observer.error(err);
                      this.endInnerSub(value)
                  },
                  complete: () =&gt; {
                      this.endInnerSub(value);
                  }
              });
          }
      }
  
      private endInnerSub(value: T) {
          this.innerSub.unsubscribe();
          this.innerSub = null;
          if (this.latestValue !== value) {
              this.processNext(this.latestValue);
          }
      }
  }
  </code></pre>
  
  <p>But I was wondering if there is a way to implement it just by reusing and combining existing operators. Any ideas?</p>