超时停止流rxjs Angular

问题描述 投票:0回答:1

嗨,即使在超时后,我也想继续获取数据,我尝试了其他方法,但没有成功(timeWith、repeatWhen 等)

这是我的代码。现在,任何帮助都会很好

目标:设备停止发送数据=

timeout
提醒用户,设备开始再次获取数据恢复流

  /**
   * Returns the current user location based on the EXTERNAL device (GPS)
   *
   * @returns An observable with the position
   */
  private useExternalGeolocationDevice(): Observable<Position> {
    return this.bluetooth.getBluetoothData().pipe(
      timeout(4000),
      takeUntil(this.destroyWatchers$),
      switchMap((data) => this.nmeaService.parseAndSetGnssData(data)),
      switchMap(() => this.nmeaService.getGnssData()),
      switchMap((gnssData) => {
        // If RMC packet is invalid
        if (
          gnssData.rmcPacket &&
          gnssData.rmcPacket.status !== NMEA_RMC_VALID
        ) {
          this.errorStream$?.next(NMEA_RMC_GPS_INVALID_MESSAGE);

          return EMPTY;
        }

        const position: Position = {
          timestamp: new Date(
            gnssData.rmcPacket?.datetime ?? gnssData.ggaPacket?.time
          ).getTime(),
          coords: {
            latitude:
              gnssData.rmcPacket?.latitude ?? gnssData.ggaPacket?.latitude,
            longitude:
              gnssData.rmcPacket?.longitude ?? gnssData.ggaPacket?.longitude,
            accuracy: gnssData.accuracy?.horizontal ?? 100,
            altitudeAccuracy: gnssData.accuracy?.vertical ?? 100,
            altitude: gnssData.ggaPacket?.altitudeMeters ?? null,
            speed: gnssData.rmcPacket?.speedKnots ?? null,
            heading: gnssData.rmcPacket?.trackTrue ?? null,
          },
        };

        // If non lat/lon throw error
        if (!position.coords.latitude || !position.coords.longitude) {
          this.errorStream$?.next(NMEA_SATELLITE_INVALID);

          return EMPTY;
        }

        // Save/emit last known position
        this.lastKnownPosition$.next(copy(position));

        return of(position);
      }),
      catchError((error) => {
        console.error('Timeout occurred:', error);

        return EMPTY;
      }),
      finalize(() => {
        this.isBluetoothWatcherRunning = false;
      })
    );
  }
angular rxjs rxjs5 rxjs6 rxjs-pipeable-operators
1个回答
0
投票

查看该操作员的文档这里

您可以使用

with
字段在超时触发时提供回调。该回调不一定需要抛出错误。相反,它可能会引发副作用,例如触发 toast 消息。

mySlowObservable$.pipe( 暂停({ 每个:5000, with: () => { 在此触发 toast 消息 }, }) )

© www.soinside.com 2019 - 2024. All rights reserved.