Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observable.zip does not call subscribe.next if sources are given as array

Tags:

rxjs

rxjs5

I have a two dimentional array of BehaviorSubject<number>s. For debugging purposes I want to write the values in a formatted manner as soon as all the array cells emit value. So I wrote this:

    Observable.zip(universe.map(row => Observable.zip(row)))
        .takeUntil(stopper)
        .subscribe(u =>
            console.log(`[\n\t[${u.map(r => r.toString()).join("],\n\t[")}]\n]`))

Nothing written. And also this hasn't work:

    Observable.zip(universe[0])
        .takeUntil(stopper)
        .subscribe(u => console.log(`1[${u.toString()}]`))

But these following worked (the array has 5 columns):

    Observable.zip(universe[0][0], universe[0][1], universe[0][2], universe[0][3], universe[0][4])
        .takeUntil(stopper)
        .subscribe(u => console.log(`2[${u.toString()}]`))

    Observable.zip(Observable.zip(Observable.zip(Observable.zip(universe[0][0], universe[0][1]), universe[0][2]), universe[0][3]), universe[0][4])
        .takeUntil(stopper)
        .subscribe(u => console.log(`3[${u.toString()}]`))

Also I have considered .zipAll() operator but there is no document about it.

This may be a bug in Observable.zip() code as it shows ArrayLike<BehaviorSubject<number>> as possible argument type in code assistance.

So is there any other way to get this functionality? How can I get the array values written down once all of the values are reassigned, without knowing the actual dimensions of course?

like image 638
koducu Avatar asked Dec 15 '22 00:12

koducu


1 Answers

The important thing is that zip() operator doesn't take an array of Observables but an unpacked series of Observables instead.

That's why Observable.zip([obs1, obs2, obs3]) doesn't work.

But Observable.zip(obs1, obs2, obs3) works.

It's not possible to help you when we don't know what universe is. From what you have now it seems like you could use destructuring assignment (assuming you're using ES6 or TypeScript):

Observable.zip(...universe[0]);

I don't know what plans are with zipAll() but right now it just callls zip().

like image 66
martin Avatar answered Apr 25 '23 21:04

martin