RxJS: How to emit original values, then reduce upon completion?

Viewed 425

I would like to emit all original values from an RxJS stream, and then emit a summary upon completion.

Reduce stops the original values from emitting. Scan emits each total rather than the original values.

Here is my hacky solution:

let total = {
  total: 0
};

Rx.Observable.range(1, 3)
  .do(val => {
    total.total += val;
  })
  .concat(Rx.Observable.of(total))
  .subscribe(
    value => {
      console.log('Next:', value)
    }
  );

// Next: 1
// Next: 2
// Next: 3
// Next: { total: 6 }

What is a simple way to do this with pure RxJS streams?

3 Answers
Related