Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rx.Observable.merge is not accepting Array of Observables #1308

Closed
leesiongchan opened this issue Feb 5, 2016 · 18 comments
Closed

Rx.Observable.merge is not accepting Array of Observables #1308

leesiongchan opened this issue Feb 5, 2016 · 18 comments

Comments

@leesiongchan
Copy link

Given:

const name$ = Rx.Observable.merge([
  Rx.Observable.of('Bruce Lee'),
  Rx.Observable.of('Brad Pitt'),
]);

Above code will return as a nested Observable. But if I remove the square bracket it works as expected.

@benlesh
Copy link
Member

benlesh commented Feb 5, 2016

I think this is one of the cases for a static mergeAll which I've proposed elsewhere #786

If you're using Babel or TypeScript, for now you can just do:

Rx.Observable.merge(...[observable1, observable2]);

@benlesh
Copy link
Member

benlesh commented Jun 17, 2016

This as been fixed.

@benlesh benlesh closed this as completed Jun 17, 2016
@patroza
Copy link

patroza commented Jul 13, 2016

@Blesh Not seeing the fix in beta10, also not as a function signature in the merge.d.ts.
Can't seem to find a linked commit.. Am I overlooking something?

@david-driscoll
Copy link
Member

@Blesh was a static operator added? The other closed issue referred back to this one. :)

@jdelaune
Copy link

@Blesh I don't think this has been fixed. In RC1 passing an array into merge still returns a nested Observable as stated in the original issue.

@ghost
Copy link

ghost commented Nov 10, 2016

I have the same issue here. I build up an array of observables which do, lets say install steps, then I merge them with Observable merge. And then I got the same behaviour.

@sfmskywalker
Copy link

Same issue as described by @jdelaune and @oka-garaio.

@sfmskywalker
Copy link

That being said, adding the ... before the array variable name being passed into merge as mentioned by @Blesh does make it work.

@ORESoftware
Copy link

ORESoftware commented Jan 8, 2017

Rx.Observable.merge(...[1,2,3])  // (?)

@ORESoftware
Copy link

I must be doing something wrong

  const Rx = require('rxjs');

   Rx.Observable.merge([
            Rx.Observable.timer(100),
            Rx.Observable.timer(2),
            Rx.Observable.timer(40)
        ])
       .subscribe();

I get subscribe is not a function. No idea what's up with that. Anyone see the same?
I also tried calling do() on the result of merge, same thing:

TypeError: Rx.Observable.merge(...).do is not a function

Weird.

@larssn
Copy link

larssn commented Jan 12, 2017

Same problem in the context of an Ionic 2 app utilising Observables.

This subsection of a larger observable chain breaks:

let test: Observable<Order>[] = [];

for (let orderId of orderIds) {
    test.push(this.orderService.get(orderId)) //Produces Observables with an "order" in it.
}

return Observable.merge(test)
   .flatMap(flat => flat)
   .toArray()
   .do(array => console.info('*ARRAY', array));
TypeError: __WEBPACK_IMPORTED_MODULE_1_rxjs_Observable__["Observable"].merge(test)
                .flatMap is not a function. (In '__WEBPACK_IMPORTED_MODULE_1_rxjs_Observable__["Observable"].merge(test)
                .flatMap(function (test) { return test; })', '__WEBPACK_IMPORTED_MODULE_1_rxjs_Observable__["Observable"].merge(test)
                .flatMap' is undefined)

@david-driscoll
Copy link
Member

merge doesn't currently accept an array, it only accepts rest args. The work around is to use the ... operator.

 const Rx = require('rxjs');

   Rx.Observable.merge(...[
            Rx.Observable.timer(100),
            Rx.Observable.timer(2),
            Rx.Observable.timer(40)
        ])
       .subscribe();

and

let test: Observable<Order>[] = [];

for (let orderId of orderIds) {
    test.push(this.orderService.get(orderId)) //Produces Observables with an "order" in it.
}

return Observable.merge(...test)
   .flatMap(flat => flat)
   .toArray()
   .do(array => console.info('*ARRAY', array));

@kamok
Copy link

kamok commented May 30, 2017

Why was this closed and marked as fixed? @benlesh I'm using the ... operator as a workaround, and not really a big deal, but it isn't "fixed".

@Kolokial
Copy link

Kolokial commented Jun 23, 2017

I'm currently experiencing the same issue...Though, my complete() method also does not fire.

@jimleroyer
Copy link

I spent some tim trying to make this work, only to find out I had to add the .... Thanks a lot to the commenters on this thread for the solution. I wish the feedback will be consider here because this API is not easy to use as-is.

@samal-rasmussen
Copy link
Contributor

Is there any reason not to have a version of .merge that takes an array? Cause this seems like a simple PR to make, but I'd like to know if there's a reason not to do it before going ahead.

@martinsik
Copy link
Contributor

@samal84 I think that's because merge internally comes down to subscribeToResult which means you can use it with arrays of items as well. For example you can write this:

Observable.zip(
    Observable.interval(1000),
    Observable.merge([1, 2, 3, 4, 5])
).subscribe(console.log);

The merge operator takes the array and emits each number as a separate item.

So if you wanted merge to accept an array of Observables it can't know whether you want to subscribe to each one of them or emit them as Observables (higher-order Observable).

@lock
Copy link

lock bot commented Jun 6, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests