Search code examples
angularrxjsrxjs5rxjs6rxjs-pipeable-operators

Timeout stops stream rxjs Angular


Hi i want to keep getting data even after the timeout its reached i try some other way but no success (timeWith, repeatWhen ect)

here my code. now, any help would be good

goal: Device stops sending data = timeout alert user, device starts to get data again resume stream

  private gpsDevice(): Observable<Position> {
    return this.bluetooth.getBluetoothData().pipe(
      timeout(4000),
      switchMap((position) => {
       ...
        return of(position);
      }),
      catchError((error) => {
        console.error('Timeout occurred:', error);

        return EMPTY;
      }),
    );
  }

Solution

  • Take a look at the docs for that operator here.

    You can use the with field to provide a callback when the timeout fires. That callback does not necessarily need to throw an error. Instead it could fire off a side effect that triggers a toast message, for example.

    mySlowObservable$.pipe( timeout({ each: 5000, with: () => { trigger a toast message here }, }) )