1
votes

I would like to create an observable which takes N observable sources and transform them whith an N-ary function. The onNext() of this observable will call this function whenever one of the source observables emits an item, like this: f(null,null,null,o3.val,null,null) where o3 is the source which just emitted a value.

Is like the combineLatest where the f is called with the last emitted values from all the sources combined together but here in the f we get null value for all the others.

The body of the f could act like a switch:

 function f(v1,v2,...vn) {
        if (v1) { ... }
        else if(v2) { ... }
    }

Is this possible? There are other way round for accomplish this behaviour?

1
so you want it to emit an array of nulls but only at the index of the source Observable that just emitted you want its value?martin
yes it could be a wayFruff
It seems odd that you have n observables that you combine into a single array of values and then you use a series of if statements to choose a code path based on the value emitted. Why not just have n observables with n subscriptions to the code you need?Enigmativity
You would need to select the values according to some time slice. Otherwise if they consistently produced values simultaneously you would be unnecessarily forcing sequential processing.Sentinel
Isn't this basically just merge?Asti

1 Answers

2
votes

You may want to think about something like this

const obsS1 = obsSource1.pipe(map(data => [data, 'o1']));
const obsS2 = obsSource2.pipe(map(data => [data, 'o2']));
....
const obsSN = obsSourceN.pipe(map(data => [data, 'oN']));

merge(obs1, obs2, ..., obsN)
.subscribe(
  dataObs => {
    // do what you need to do
    // dataObs[0] contains the value emitted by the source Observable
    // dataObs[1] contains the identifier of the source Observable which emitted last
  }
)