Two buttons on screen with clicks plumbed in like this.
b1c: number;
b1s: Subject<number>;
o1: Observable<number>;
b2c: number;
b2s: Subject<number>;
o2: Observable<number>;
rxjs(): void {
this.b1c = 0;
this.b1s = new Subject<number>()
this.b2c = 0;
this.b2s = new Subject<number>()
this.b1s.pipe(
tap (z => console.log('do', z)),
mergeMap(z1 => this.b2s.pipe(
map(z2 => `${z1} / ${z2}`)
))
).subscribe(z => console.log(z));
// When you click button 1 nothing happens.
// When you click button 2 you get output i/j for all values of i from 0 to current, and the current value of j.
// This is incorrect because this.b1s is not a ReplaySubject and therefore should not remember the previous values.
}
buttonClick(buttonNumber: number) {
if (buttonNumber === 1) {
this.b1s.next(this.b1c);
this.b1c++;
}
else {
this.b2s.next(this.b2c);
this.b2c++;
}
}
Can you explain this behaviour? (Button clicks: 1, 1, 1, 1, 2, 1, 1, 1, 2.)
It should be only
5 / 1
6 / 1
7 / 1
because 0 to 4 have already been consumed; there is no reason why they should be remembered.
In fact maybe there should be no output at all because at no time do the two observables simultaneously fire -- you can't click two buttons at once.
How do you explain this behaviour, and how is it possible to deduce this from the documentation?
Furthermore: I don't understand why there are three 30s and three 50s and why they're mixed up. There should be six outputs becuase there are six events. https://rxjs-dev.firebaseapp.com/api/operators/flatMap And we still have this business about it remembering the last value from the other source.
And what on earth is
10*i--10*i--10*i-|
supposed to mean?