All files / lib diagnostics.effects.ts

87.5% Statements 21/24
100% Branches 2/2
78.57% Functions 11/14
87.5% Lines 21/24

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                        1x 4x   4x   4x   4x 4x   2x   2x 2x         4x 4x     2x 2x             4x   4x     1x           4x   4x     1x           4x 4x              
import { inject, Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { map, mergeMap, tap } from 'rxjs/operators';
 
import { diagnosticsAction } from './diagnostics.actions';
import { IDiagnosticsStateModel, TDiagnosticData } from './diagnostics.interface';
import { AppStaticDataService } from './services/static-data/static-data-api.service';
import { AppWebsocketApiService } from './services/websocket/websocket-api.service';
 
@Injectable({
  providedIn: 'root',
})
export class AppDiagnosticsEffects {
  private readonly actions$ = inject(Actions);
 
  private readonly wsApi = inject(AppWebsocketApiService);
 
  private readonly staticApi = inject(AppStaticDataService);
 
  public readonly connect$ = createEffect(() =>
    this.actions$.pipe(
      ofType(diagnosticsAction.connect.type),
      mergeMap(() => this.wsApi.connect<TDiagnosticData[]>()),
      map(event => {
        const payload: Pick<IDiagnosticsStateModel, 'events'> = { events: [event] };
        return diagnosticsAction.connected({ payload });
      }),
    ),
  );
 
  public readonly connected$ = createEffect(() =>
    this.actions$.pipe(
      ofType(diagnosticsAction.connected),
      map(({ payload }) => {
        const event = payload.events[0];
        return event.event === 'users'
          ? diagnosticsAction.userDataSuccess({ payload: event.data.reduce((acc: number, item) => acc + (item['value'] as number), 0) })
          : diagnosticsAction.dynamicDataSuccess({ payload: event.data });
      }),
    ),
  );
 
  public readonly startEvents$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(diagnosticsAction.startEvents.type),
        tap(() => {
          this.wsApi.startDiagEvents();
        }),
      ),
    { dispatch: false },
  );
 
  public readonly stopEvents$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(diagnosticsAction.stopEvents.type),
        tap(() => {
          this.wsApi.stopDiagEvents();
        }),
      ),
    { dispatch: false },
  );
 
  public readonly staticData$ = createEffect(() =>
    this.actions$.pipe(
      ofType(diagnosticsAction.staticData.type),
      mergeMap(() => this.staticApi.staticData()),
      map(payload => diagnosticsAction.staticDataSuccess({ payload })),
    ),
  );
}