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

Add default combining behavior to combine_latest operator #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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