Rxjs: combineAll does not emit on empty array

Created on 30 Aug 2016  路  4Comments  路  Source: ReactiveX/rxjs

Using rxjsv5 combineAll, I have an issue forming a state tree of children states.

If I have an array of state streams, I can get the state data in an array corresponding to the streams. But if I switchMap from an array with n state streams to an array with zero state streams (empty array), combineAll will not emit anything.

This makes the app state tree contain the old state data of the last non-empty array of state streams.
I have fixed it right now by mapping the array of state streams to always concat a dummy stream with a dummy value, which is really confusing for other developers looking at the code and state tree.

Most helpful comment

combineAll won't emit unless the combined Observables emit at least one value, but you could check to ensure the collection you're combining is empty or not, and either combine or emit an empty Array:

var arrayOfStreamsStream = Rx.Observable
    .of(
        [], [
            Rx.Observable.of('blah-1'), // component state.
            Rx.Observable.of('blah-2'),
            Rx.Observable.of('blah-3')
        ], [], [
            Rx.Observable.of('foo-1'),
            Rx.Observable.of('qux-2')
        ]
    )
    .switchMap(function onMap(coll) {
        return coll.length === 0 ?
            Observable.of(coll) :
            Observable.combineLatest(...coll);
    })
    .subscribe(function onSubscribe(data) {
        console.log('onSubscribe START')
        console.dir(data)
        console.log('onSubscribe END')
    })

All 4 comments

combineAll won't emit unless the combined Observables emit at least one value, but you could check to ensure the collection you're combining is empty or not, and either combine or emit an empty Array:

var arrayOfStreamsStream = Rx.Observable
    .of(
        [], [
            Rx.Observable.of('blah-1'), // component state.
            Rx.Observable.of('blah-2'),
            Rx.Observable.of('blah-3')
        ], [], [
            Rx.Observable.of('foo-1'),
            Rx.Observable.of('qux-2')
        ]
    )
    .switchMap(function onMap(coll) {
        return coll.length === 0 ?
            Observable.of(coll) :
            Observable.combineLatest(...coll);
    })
    .subscribe(function onSubscribe(data) {
        console.log('onSubscribe START')
        console.dir(data)
        console.log('onSubscribe END')
    })

I will totally accept that as a good solution! Many thanks.

Please post to Stackoverflow if you'd like an accepted answer, or tell me otherwise so I can post it and close that one.

Wrote the answer on SO.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benlesh picture benlesh  路  3Comments

Agraphie picture Agraphie  路  3Comments

Zzzen picture Zzzen  路  3Comments

jakovljevic-mladen picture jakovljevic-mladen  路  3Comments

dooreelko picture dooreelko  路  3Comments