All files / tools/ReFlex.TrackingServer/ClientApp/src/shared/services performance.service.ts

19.44% Statements 7/36
60% Branches 3/5
0% Functions 0/17
18.18% Lines 6/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 873x                 3x 3x 3x 3x         3x                                                                                                                                          
import { inject, Injectable } from '@angular/core';
import * as signalR from '@microsoft/signalr';
import { PerformanceData } from '@reflex/shared-types';
import {
  BehaviorSubject,
  from,
  fromEventPattern,
  Observable,
  using
} from 'rxjs';
import { concatMap, filter, share, skipWhile } from 'rxjs/operators';
import { LogService } from 'src/app/log/log.service';
import { BASE_URL } from 'src/shared/tokens/base-url.token';
 
@Injectable({
  providedIn: 'root'
})
export class PerformanceService {
 
  protected isConnected = new BehaviorSubject<boolean>(false);
  private readonly baseUrl = inject(BASE_URL);
  private readonly logService = inject(LogService);
 
  private readonly connection: signalR.HubConnection;
  private readonly startAfterConnected$: Observable<void>;
 
  private readonly performanceData$: Observable<PerformanceData>;
  private readonly isStarted = new BehaviorSubject<boolean>(false);
 
 
  public constructor() {
    this.connection = new signalR.HubConnectionBuilder()
      .withUrl(`${this.baseUrl}perfhub`)
      .build();
 
    // update connection status the rxjs way
    from(this.connection.start()).subscribe(
      () => this.isConnected.next(true),
      (error) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      }
    );
 
    this.performanceData$ = fromEventPattern<PerformanceData>(
      (handler) => this.connection.on('performanceData', handler),
      (handler) => this.connection.off('performanceData', handler)
    ).pipe(
      share(),
      filter((x) => x !== undefined)
    );
 
    // send 'startState' only after 'isConnected' emits true
    this.startAfterConnected$ = this.isConnected.pipe(
      skipWhile((value) => !value),
      concatMap(async () =>
        this.connection.send('startCollectingData').catch((error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }))
    );
  }
 
  /**
   * @return an Observable of Arrays of Point3 from the currently configured camera
   */
  public getData(): Observable<PerformanceData> {
    return using(
      () => {
        this.startAfterConnected$.subscribe(() => this.isStarted.next(true));
 
 
        return {
          // @todo: check if this is correct
          // eslint-disable-next-line @typescript-eslint/no-misused-promises
          unsubscribe: async () =>
            this.connection.send('stopCollectingData').catch((error) => {
              console.error(error);
              this.logService.sendErrorLog(`${error}`);
            })
        };
      },
      () => this.performanceData$
    );
  }
}