RxJS Version 3.1
This is a slight update to RxJS since 3.0 which contains a number of fixes and new features. This should be the last of the 3.x releases moving forward with 4.0 to include the large scheduler changes to improve performance as well as get rid of some more technical debt.
Some of the features included in this release include:
- Result Selectors now optional on
zip
andcombineLatest
- Changes to
zip
- Added
zipIterable
- Introduction of
Rx.Observable.wrap
- New TypeScript Definitions
- Bug fixes
Result Selectors Now Optional
In previous releases of RxJS, the zip
and combineLatest
took either an array of Observables or Promises and followed by a resultSelector
function which took the values projected from each sequence and allowed you to combine them any which way you wanted. With version 3.1, the resultSelector
has been made completely optional.
Previously, if you wanted to combine three sequences, you had to have a result selector as well, even if it were as trivial as adding all of the values to an array.
var source = Rx.Observable.zip(
Rx.Observable.of(1,2,3),
Rx.Observable.of(4,5,6),
Rx.Observable.of(7,8,9),
function (x, y, z) { return [x, y, z]; });
// => [1, 4, 7]
// => [2, 5, 8]
// => [3, 6, 9]
With the result selector being optional you can now omit the last parameter and the aforementioned behavior will be done automatically for you. This applies to all versions of zip
and combineLatest
whether it is on the prototype or the Observable itself.
var source = Rx.Observable.zip(
Rx.Observable.of(1,2,3),
Rx.Observable.of(4,5,6),
Rx.Observable.of(7,8,9));
// => [1, 4, 7]
// => [2, 5, 8]
// => [3, 6, 9]
Changes to zip
The zip
function is very useful and very flexible about its input. So much so, that we tried to allow for iterables such as Map
, Set
, and Array
, but making this a solid API was too difficult, so instead zip
now only accepts Observables and Promises so now you can have full parity between the method signatures of combineLatest
and zip
with either arguments or an array of Observables or Promises.
/* Arguments version */
var source = Rx.Observable.zip(
Rx.Observable.of(1,2,3),
Rx.Observable.of(4,5,6),
Rx.Observable.of(7,8,9)
);
/* Array version */
var source = Rx.Observable.zip([
Rx.Observable.of(1,2,3),
Rx.Observable.of(4,5,6),
Rx.Observable.of(7,8,9)
]);
Introducing zipIterable
As noted above there were some issues with zip
trying to accept iterables as well as Observable and Promises given that we accepted both arguments and arrays. To fix this, we have introduced zipIterable
which allows you to zip an Observable with any iterable whether it be an Array
, Map
, Set
or even Generator
.
var source = Rx.Observable.of(1,2,3).zipIterable(
[4, 5, 6],
[7, 8, 9]
);
// => [1, 4, 7]
// => [2, 5, 8]
// => [3, 6, 9]
With a generator it can be even more fun such as:
var source = Rx.Observable.of(1,2,3).zipIterable(
(function* () { yield 4; yield 5; yield 6; })();
(function* () { yield 7; yield 8; yield 9; })();
);
// => [1, 4, 7]
// => [2, 5, 8]
// => [3, 6, 9]
Introduction to Rx.Observable.wrap
In the previous release, we redid Rx.Observable.spawn
so that it could accept a number of yieldable arguments such as Observable, Promises, Arrays and Objects and returned an Observable. In this release we went a bit further by adding Rx.Observable.wrap
which creates an Observable from a function which now takes arguments.
var fn = Rx.Observable.wrap(function* (value) {
return [
yield Rx.Observable.just(value + 2).delay(2000),
yield Rx.Observable.just(value)
];
});
fn(1000).subscribe(next => console.log('Next %s', next));
// => [1002, 1000]
With some fixes with this release and wrap
we can create pretty complex objects and have it yield properly.
var fn = Rx.Observable.wrap(function* (v) {
return {
1: {
3: yield [
Rx.Observable.just(20).delay(5000), [
Rx.Observable.just(30).delay(2000), Rx.Observable.just(40).delay(3000)
]
]
},
2: yield Promise.resolve(v)
};
});
fn(1000).subscribe(next => console.log('Next %s', next));
Many thanks to @xgrommx for his help on this effort!
New TypeScript Definitions
Many people have a lot of interest in the TypeScript definitions for RxJS. Many thanks to @david-driscoll for stepping up to the plate and completely rewriting them from scratch!