hareactive icon indicating copy to clipboard operation
hareactive copied to clipboard

How to accomplish `zip([ streamA, streamB ])` with hareactive?

Open m59peacemaker opened this issue 6 years ago • 0 comments

Given the scenario that occurrences are pushed from the outside world, such as incoming websocket messages, and needing to reply only when each of N streams have emitted since last reply, using the values of the streams to form the reply, how can this be accomplished with hareactive?

In some libraries, this is as simple as zip([ streamA, streamB ]), but it isn't clear to me how to do it with the behavior mentality. Making behaviors of A and B and lifting them to form the reply is close, but it disregards the timing/state problem, which remains by far the hardest part of the whole scenario.

Here's a psuedo code example:

const messageA$ = Stream()
const messageB$ = Stream()
wsA.on('message', messageA$.emit)
wsB.on('message', messageB$.emit)

const reply$ = zip([ messageA$, messageB$ ]).map(makeTheReply)
reply$.on(reply => [ wsA, wsB ].forEach(ws => ws.emit('reply', reply)))

m59peacemaker avatar Nov 11 '18 19:11 m59peacemaker