I would like to merge two maps of the same source guaranteeing the order of the result. Here is a unit test that I would like to pass:
const source = xs.of(1,2,3)
const a = source.map(v=>v*10)
const b = source.map(v=>v*100)
const hist:number[] = []
xs.merge(a,b).addListener({
next: v=>hist.push(v),
})
expect(hist).toEqual([10,100,20,200,30,300])
Currently the result I'm getting is this:
Expected value to equal:
[10, 100, 20, 200, 30, 300]
Received:
[10, 20, 30, 100, 200, 300]
I am no expert with
xstream, so I can't propose you a solution. However, I think I can explain why you are getting the output you are getting as this is a common occurrence in other streaming libraries.You have a merge of two sources. The
ofoperator guarantees that it will emit the array values in order, themapoperator guarantees that it will emits the transformed values in the same order as the received values, etc. Butmerge(a,b)does not guarantee that it will interleave value of a and b. It does guarantee that it will pass on the values of a in order, those of b in order, i.e. it guarantees only a partial order on the resulting output.The question of, given some values to emit, which ones to emit at what time, and in which order relates to scheduling. I am not aware of
xstreamat this point of time exposing a scheduler interface , from which you could customize the scheduling of emission of values. Hence you are bound to the default scheduling.Now back to why you observe those values in that order :
If you want to get the values 1, 2, 3 not emitted synchronously, you need to use another operator than
ofto do so, or construct your timed sequence explicitly, i.e. emit 1 at t0, 2 at t0+1, 3 at t0+2.