Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating from RxJs 5 to 6: switchMap and Interval broken

I have a hard time understanding RxJs. The breaking changes between 5 and 6 are hard for me to understand.

I have the following code and the following issues.

  1. .interval method is no longer available from Observable
  2. .switchMap method is no longer available from .interval

I checked the changelog and the recommendations for fixing breaking changes and I wasn't able to sort out what I need to do. The way I understand it, the code I have is old and doesn't use the pipe operator but that's about all I've been able to figure out.

let polling = Observable.interval(2000)
.switchMap(() => this.http.get(this.videoStatusURL + this.taskID))
.subscribe(
  (data) => {              
    if (data["state"] === "SUCCESS") {
      //get final video here
      console.log("polling succeeded");
      this.downloadFinalVideo();
      polling.unsubscribe();
    }            
  },
  error => this.handleError(error));
like image 365
tshm001 Avatar asked Feb 03 '26 06:02

tshm001


1 Answers

See the section on pipe syntax in the migration doc, operators must be invoked by calling .pipe(), so you need to do something like

import { interval } from 'rxjs';
import { switchMap } from 'rxjs/operators';

let polling = interval(2000)
.pipe(switchMap(() => this.http.get(this.videoStatusURL + this.taskID)))
.subscribe(
  (data) => {              
    if (data["state"] === "SUCCESS") {
      //get final video here
      console.log("polling succeeded");
      this.downloadFinalVideo();
      polling.unsubscribe();
    }            
  },
  error => this.handleError(error));

Alternatively, you could install rxjs-compat, however this is just a compatibility layer and you should really be using the pipe syntax.

like image 115
kevmo314 Avatar answered Feb 05 '26 22:02

kevmo314