Skip to content

Conversation

schmich
Copy link

@schmich schmich commented Jan 19, 2017

When no block is specified, the combine_latest operator should combine its two streams into an array (a pair of elements).

Currently, combine_latest errors when no block is specified:

require 'rx'
require 'pp'

left = Rx::Subject.new
right = Rx::Subject.new

lo = left.as_observable
ro = right.as_observable

lo.combine_latest(ro).subscribe { |x| pp x }

left.on_next(1);
right.on_next(1);
left.on_next(2);
right.on_next(2);

# Error output (truncated):
# rx-0.0.3/lib/rx/operators/multiple.rb:168:in `block (3 levels) in combine_latest': undefined method `call' for nil:NilClass (NoMethodError)

In the RxJS implementation, elements from both streams are combined into an array by default:

let left = new Rx.Subject();
let right = new Rx.Subject();

let lo = left.asObservable();
let ro = right.asObservable();

lo.combineLatest(ro).subscribe(x => console.log(x));

left.onNext(1);
right.onNext(1);
left.onNext(2);
right.onNext(2);

/*
As expected, this prints:
[1, 1]
[2, 1]
[2, 2]
*/

In RxRuby, this is also the behavior with the static version of combine_latest, i.e. Rx::Observable.combine_latest(lo, ro).subscribe { |x| pp x } works as expected (see implementation here)

This patch updates the instance version of combine_latest to match the static version and makes lo.combine_latest(ro).subscribe { |x| pp x } work as expected by combining the elements into an array.

Copy link
Contributor

@bittrance bittrance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants