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

RxJS v4.1.0

Latest
Compare
Choose a tag to compare
@mattpodwysocki mattpodwysocki released this 07 Mar 21:05
· 111 commits to master since this release

We are happy to announce the release of RxJS version 4.1. With this release came a few new additions as well as a new system for pulling in what you want.

Some of the changes are the following:

  • Build What You Want with @rxjs/rx
  • Adding repeatWhen
  • Parity with RxJS v5 names
  • Other changes

Build What You Want with @rxjs/rx

One of the biggest requests with RxJS was to build only what you wanted. In previous attempts, we looked at a CLI to build what you wanted, but that was suboptimal experience. Instead, we have ported the existing code base to CommonJS format so that it works right out of the box with Node.js, or with your favorite bundler whether it is Browserify, Webpack, Rollup, etc.

By default, this brings in all of RxJS when you require the @rxjs/rx module:

const Rx = require('@rxjs/rx');

const subscription = Rx.Observable.from([1,2,3])
  .filter(x => x % 2 === 0)
  .map(x => x + 2)
  .subscribe(x => console.log(`The answer is ${x}`));
// => The answer is 4 

Now it is possible to bring in as much or as little as you want by only including the operators you want:

const fromArray = require('@rxjs/rx/observable/fromArray');
const filter = require('@rxjs/rx/observable/filter');
const map = require('@rxjs/rx/observable/map');

const source = fromArray([1,2,3]);

const filtered = filter(source, x => x % 2 === 0);

const mapped = map(filtered, x => x + 2);

const subscription = mapped.subscribe( x => console.log(`The answer is ${x}`) );
// => The answer is 4 

Not only can you bring in certain operators, but you can also add each one to the prototype if you want the nice chaining feature as well.

const Observable = require('@rxjs/rx/observable');

// Add class methods 
Observable.addToObject({
  fromArray: require('@rxjs/rx/observable/fromarray')
});

// Add instance methods 
Observable.addToPrototype({
  filter: require('@rxjs/rx/observable/filter'),
  map: require('@rxjs/rx/observable/map')
});

const subscription = Observable.fromArray([1,2,3])
  .filter(x => x % 2 === 0)
  .map(x => x + 2)
  .subscribe(x => console.log(`The answer is ${x}`));

In addition, we also added some distributions that you will find under our src/modular/dist folder which contains all of RxJS built in a UMD style as well as a "lite" version as well. This should give you the building blocks for creating your own builds of RxJS and taking the only the operators you need.

Adding repeatWhen

We often try to keep parity with other Rx versions such as RxJava. To that end, we've added repeatWhen which complements the retryWhen we've had for quite some time. Rather than buffering and replaying the sequence from the source Observable, the repeatWhen resubscribes to and mirrors the source Observable, but only conditionally based upon the Observable you return from the call.

Here is an example where we can repeat a sequence twice with a delay of 200ms in between time.

const source = Rx.Observable.just(42)
  .repeatWhen(function(notifications) {
    return notifications.scan((acc, x) => return acc + x, 0)
    .delay(200)
    .takeWhile(count => count < 2);
  });

var subscription = source.subscribe(
  x => console.log(`Next ${x}`,
  err => console.log(`Error ${err}`),
  () => console.log('Completed')
);

// => Next: 42
// 200 ms pass
// => Next: 42
// 200 ms pass
// => Completed

Parity with RxJS v5 names

Given the differences between RxJS v4 and RxJS v5, we've made the migration path easier by creating aliases for you such as the following:

// Prototype methods
Observable.prototype.race = Observable.prototype.amb;
Observable.prototype.mergeMap = Observable.prototype.flatMap;
Observable.prototype.switchMap = Observable.prototype.flatMapLatest;
Observable.prototype.exhaustMap = Observable.prototype.flatMapFirst;
Observable.prototype.exhaust = Observable.prototype.switchFirst;
Observable.prototype.publishReplay = Observable.prototype.replay;

// Object methods
Observable.bindCallback = Observable.fromCallback;
Observable.bindNodeCallback = Observable.fromNodeCallback;
Observable.race = Observable.amb;

Other Changes

  • Bug fixes such as to ConnectableObservable so that if the underlying Subject has been disposed, we will no longer attempt to resubscribe to it.
  • The startWith operator no longer uses Scheduler.currentThread and now uses Scheduler.immediate, as that caused issues with the backpressure operators such as pausable and pausableBuffered.
  • Documentation bug fixes