Andrey P
Andrey P

Reputation: 61

migration rxjs 5 to 6 issues

i have some code with timer in angular 5

    this.timer$ =  
Observable.merge(
    this.timerStart$.mergeMap( () => Observable.interval(1000).takeUntil(this.timerStop$) ).map(() => 1),
    this.timerReset$.map(() => 0)
).scan((acc, n) => n === 0 ? 0 : acc + n )

Subscribing to timer$ property i'm getting stream which is controlled by tree other Subjects : timerStart$, this.timerStop$, timerReset$, just like that

this.timerStart$.next(1);

After moving to Angular 6 and Rxjs 6 this code is not valid. I didn't learn deep rxjs before, so translation may take hours. Will appreciate much if some rxjs experts could help convert this tricky construction to rxjs 6.

Upvotes: 1

Views: 566

Answers (1)

siva636
siva636

Reputation: 16441

Your updated code should be as follows:

     // note merge and interval are imported from rxjs not from rxjs/operators    
    import {merge, interval} from 'rxjs';
    import {mergeMap, map, takeUntil, scan} from 'rxjs/operators';

        this.timer$ =
        merge(
            this.timerStart$.pipe(mergeMap( () => interval(1000).pipe(takeUntil(this.timerStop$)) ), map(() => 1)),
            this.timerReset$.pipe(map(() => 0))
        ).pipe(scan((acc, n) => n === 0 ? 0 : acc + n ))

      // Without subscription, nothing will happen
      this.timer$.subscribe(x =>{});

Upvotes: 4

Related Questions