RxJS version: 5.5.2
Working with an array const v = [1, 2, 3];
The goal is to convert this array into a Subject initially acting like an Observable until all values (1, 2, 3) are consumed. After that, it should behave like a Subject.
The challenge lies in using reduce
on the initial values of v = [1, 2, 3]
, and then switching to scan
each time a new value is added by the Subject.
Current code snippet:
const v = [1, 2, 3];
const sub = new Subject<number>();
const observable = sub.pipe(
startWith(0),
concatMap(x => from(v)),
scan((a, b) => {
return a + b;
}, 0),
);
observable.subscribe(x => console.log(x));
When using scan
, the following sequence is printed to the console:
1
3
6
The desired output is just the last value 6
. Substituting scan
with reduce
achieves this, but only if the subject is completed (making it unable to receive future values).
To address this, the objective is to print 10
when a new value is sent by the subject through sub.next(4);
, and so forth.