0

Im trying to update this code to version 6 but I cannot figure out how rework the flow, from reading and playing around I think I need to pipe results etc but I cant see how it can be done using the flow of merge, filter, timer, map, first, toPromise() that used to work. Any RxJS folks able to educate me or point me in the right direction ?

const chats: chats[] = <chats[]>await Observable.merge(
  this.chatService.$chats.filter(chats => chats.length > 0),
  Observable.timer(5000).map(x => { 
    throw 'Timeout'
  })
).first().toPromise()

if(chats.find( chat => chat.id === chatId )) {
  this.log.log(`Found active chat ${chatId}, navigating to it from push notification...`)                       
}
frido
  • 13,065
  • 5
  • 42
  • 56
Jipsum
  • 3
  • 5

1 Answers1

0

Try:

import { merge, timer } from 'rxjs';
import { filter, first, map, } from 'rxjs/operators';

const chats: chats[] = <chats[]> await merge(
  this.chatService.$chat.pipe(
    filter(chats => chats.length > 0),
  ),
  timer(5000).pipe(
    map(x => {
     throw 'Timeout';
   }),
  ),
 ).pipe(
  first(),
 ).toPromise();

 if(chats.find( chat => chat.id === chatId )) {
   this.log.log(`Found active chat ${chatId}, navigating to it from push notification...`)                       
 }

You have to pipe the operators instead of chaining them with ..

AliF50
  • 16,947
  • 1
  • 21
  • 37