I have a state$ stream that constains messages$s which is array of messages$ stream. State$ is updated and new messages$ appear.
I want subscriber to handle messages from all messages$ in one sinle stream an I want this this stream contain only correct events.
I try to flatMap merged messages$ every time, but got the problem that old messages$s (which where in previous states$ values) are subsribed multiple timed.
How do I solve this?
let allMessages$ = state$.flatMap(s => {
return Observable.merge(s.messages$s)
}
)
allMessages$.subscribe((x)=>{
console.log('message', x)
// message from single message$ appear multiple times
})
The problem is that after state$ is updated (with items pushed) old one became to be subscribed multiple times.
state$ --s(1)---------s(2)----
message$s[0]. --m1----m2-----------m4--
message$s[1] ---------------m3--------
allMessages$ --m1----m2-----m3----m4
m1 m4
s(1) - when state has 1 message$, s(2) when second message$ is added So allMessages$ fire with messages from item1.
What i want is:
state$ --s(1)---------s(2)-----
message$s[0] --m1----m2-----------m4--
message$s[1] ---------------m3--------
allMessages$ --m1----m2-----m3----m4
This fildle shows the situation simplified: http://jsfiddle.net/8jFJH/797/