Skip to content

GroupBy + Take Results in Incorrect Return Sequence #249

@thedillonb

Description

@thedillonb

Given the following:

#include "rxcpp/rx.hpp"

namespace rx=rxcpp;

int main()
{
  rx::observable<>::range(0, 10)
    .group_by([](auto i) { return 0; }, [](auto i) { return i; })
    .take(1)
    .subscribe([](auto g) {
      auto key = g.get_key();
      std::cout << "On Next: " << key << std::endl;
      g.subscribe([key](auto i) { std::cout << "Group=" << key << " Value=" << i << std::endl; }, [key]() { std::cout << "Group=" << key << " completed" << std::endl; });
    }, [](auto err){
      std::cout << "Error occured: " <<  std::endl;
    }, []() {
      std::cout << "Completed!" << std::endl;
    });
  return 0;
}

I'd expect to see the following output:

On Next: 0
Group=0 Value=0
Group=0 Value=1
Group=0 Value=2
Group=0 Value=3
Group=0 Value=4
Group=0 Value=5
Group=0 Value=6
Group=0 Value=7
Group=0 Value=8
Group=0 Value=9
Group=0 Value=10
Group=0 completed
Completed!

However, I am seeing:

On Next: 0
Completed!
Group=0 Value=0

This is incorrect as it seems to be Take(1)ing on the group's inner sequence instead of the outer observable sequence.

Other implementation's of Rx support the case that this implementation is incorrect. For example, RxJs 4 returns the correct results.

Rx.Observable.range(0, 10)
  .groupBy(x => 0, x => x)
  .take(1)
  .subscribe(x => {
      const key = x.key;
    x.subscribe(y => console.log('Group' + key + ' Value=' + y), err => console.error(err), () => console.log('Group=' + key + ' completed'));
  }, err => console.error(err), () => console.log('Completed!'));

returns

"Completed!"
"Group0 Value=0"
"Group0 Value=1"
"Group0 Value=2"
"Group0 Value=3"
"Group0 Value=4"
"Group0 Value=5"
"Group0 Value=6"
"Group0 Value=7"
"Group0 Value=8"
"Group0 Value=9"
"Group=0 completed"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions