Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Behavior of #zip with #groupBy & #publish #978

Open
niklasfasching opened this issue Oct 17, 2015 · 5 comments
Open

Behavior of #zip with #groupBy & #publish #978

niklasfasching opened this issue Oct 17, 2015 · 5 comments

Comments

@niklasfasching
Copy link

Why does only the grouped observable of child emit values when zipped?

When subscribing to the grouped observables on their own, both emit their respective values
see https://jsbin.com/coqeqaxoci/edit?js,console

var parent = Rx.Observable.from([1,2,3]).publish();
var child = parent.map(x => x).publish();

var groupedParent = parent.groupBy(x => x);
var groupedChild = child.groupBy(x => x);

Rx.Observable.zip([groupedParent, groupedChild])
  .subscribe(groups => {
    groups[0].subscribe(x => console.log('zipped parent ' + x)); // -> nothing
    groups[1].subscribe(x => console.log('zipped child ' + x)); // -> emits
  });


groupedChild.subscribe(group => {
  group.subscribe(value => console.log('child ' + value)); // -> emits
});

groupedParent.subscribe(group => {
  group.subscribe(value => console.log('parent ' + value)); // -> emits
});

child.connect();
parent.connect();
@xgrommx
Copy link
Contributor

xgrommx commented Oct 17, 2015

@Nupf This example works for me:

var parent = Rx.Observable.from([1,2,3]).publish();
var child = parent.map(x => x).publish();

var groupedParent = parent.groupBy(x => x).flatMap(x => x);
var groupedChild = child.groupBy(x => x).flatMap(x => x);

Rx.Observable.zip([groupedParent, groupedChild])
  .subscribe(x => {
    console.log('zipped parent ' + x); // -> nothing
    console.log('zipped child ' + x); // -> emits
  });


groupedChild.subscribe(value => {
  console.log('child ' + value); // -> emits
});

groupedParent.subscribe(value => {
  console.log('parent ' + value); // -> emits
});

child.connect();
parent.connect();

@niklasfasching
Copy link
Author

@xgrommx I need to zip groups together.

The goal is to xprod the elements of the zipped together groups - so parent_group1 with child_group1, parent_group2 with child_group2, etc.

Rx.Observable.zip([groupedParent, groupedChild])
  .flatMap(groups => xprod(groups[0], groups[1]))
  .subscribe();

xprod being the function described in #807.

@xgrommx
Copy link
Contributor

xgrommx commented Oct 17, 2015

@Nupf I'm not sure that is what you want but try it:

      var groupedParent = parent.groupBy(x => x).flatMap(group => group.toArray());
      var groupedChild = child.groupBy(x => x).flatMap(group => group.toArray());


Rx.Observable.zip([groupedParent, groupedChild])
  .flatMap(groups => Rx.Observable.from(groups[0]).concatMap(x => Rx.Observable.from(groups[1]), (x, y) => [x, y]))
  .subscribe(x => console.log(x));

@niklasfasching
Copy link
Author

That works. Thanks!

Do you know why nothing is emitted in the original example? It doesn't make sense to me.

Rx.Observable.zip([groupedParent, groupedChild])
  .subscribe(groups => {
    groups[0].subscribe(x => console.log('zipped parent ' + x)); // -> nothing
    groups[1].subscribe(x => console.log('zipped child ' + x)); // -> emits
  });

@niklasfasching
Copy link
Author

Ok, so here's what I've got:

http://stackoverflow.com/questions/33193130/rxjs-zip-groups-created-using-groupby

The parent groups inside .zip doesn't emit values, because it has already emitted its values BEFORE it's subscribed to. Why is that?

That behavior seems wrong to me but apparently it should be expected stackoverflow question.

Something similar happens when using .combineLatest to combine the groups:
https://jsbin.com/qumiriroha/edit?js,console

var parent = Rx.Observable.from([1,2,3,4,5,6]).publish();
var child = parent.map(x => x).publish();

var groupedParent = parent.groupBy(x => x);
var groupedChild = child.groupBy(x => x);

Rx.Observable.combineLatest([groupedParent, groupedChild])
  .subscribe(groups => {
    groups[0].subscribe(x => console.log('zipped parent ' + x));  // emits everything but the first value
    groups[1].subscribe(x => console.log('zipped child ' + x)); // emits correctly
  });

child.connect();
parent.connect();

Is it even possible to achieve what I've tried in the first post?

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

2 participants