Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find `combineLatest` in RxJS 5.0

Tags:

rxjs

rxjs5

The following code it's causing me a Observable.combineLatest is not a function using RxJS 5.0:

let Observable = require('rxjs/Observable.js').Observable;
import 'rxjs/add/operator/combineLatest';

Observable
.combineLatest([player, spaceShip], (shotEvents, spaceShip) => ({
    x: spaceShip ? spaceShip.x : board.canvas.width / 2,
    timestamp: shotEvents.timestamp
}))

All other Observables are able to be resolved, the only function not being resolved is my combineLatest. I tried observables/combineLatest just for the sake of trying to no avail.

I'm compiling everything using webpack and babel, and the code is able to resolve scan, range, interval, map, and some others. Even flatMap using import 'rxjs/add/operator/mergeMap'; worked.

But not combineLatest

So if anyone has a working example it would be deeply appreciated. Couldn't find anything else in the docs besides a unit test that is basically the same thing (an array of observables and a function).

UPDATE APR 04 2018

On RxJs 5.5 use the following:

import { combineLatest } from 'rxjs/observable/combineLatest'

Moving forward (RxJs 6) use the following:

import { combineLatest } from 'rxjs'
like image 282
Claudiordgz Avatar asked Mar 14 '16 05:03

Claudiordgz


People also ask

What is combineLatest in RXJS?

combineLatest allows to merge several streams by taking the most recent value from each input observable and emitting those values to the observer as a combined output (usually as an array).

What is the difference between combineLatest and forkJoin?

forkJoin - When all observables complete, emit the last emitted value from each. combineLatest - When any observable emits a value, emit the latest value from each.

Is combineLatest synchronous?

When combineLatest subscribes to its sources, it prefetches 128 elements by default from them. This means that your first source will run to completion synchronously and the second subscription doesn't even happen until that completion.


3 Answers

I think #1722 is the relevant GitHub issue here.

I'm on a project using [email protected], [email protected], and [email protected]. The following works for me:

import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/combineLatest';  Observable.combineLatest(   source1,   source2 ).subscribe(sink); 
like image 188
deck Avatar answered Oct 05 '22 18:10

deck


To me this seems like a bug related to this issue.

Two potential workarounds:

  1. Instead of import 'rxjs/add/operator/combineLatest';, use import rxjs/rx. This will register all operators (including combineLatest) to Observable.
  2. Assign the imported function manually to the prototype:
    let Observable = require('rxjs/Observable.js').Observable;
    Observable.prototype.combineLatest = require('rxjs/add/operator/combineLatest');
like image 31
Peter Albert Avatar answered Oct 05 '22 20:10

Peter Albert


I'm on RXJS 5.5.6, to import combineLatest for direct use (not as an operator) I had to use:

import {combineLatest} from 'rxjs/observable/combineLatest'
like image 34
Miller Avatar answered Oct 05 '22 20:10

Miller